google / google/go-cmp

Unexpected behavior when comparing slices of maps

Open
#383 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
4.7k
Forks
243
PR merge metrics
No merged PRs in 30d

Description

version: `go-cmp@v0.7.0`, `go@v1.21.3`
When I was comparing slices of maps defined as:
```go
a := []any{map[string]any{"a": 1, "b": 2, "c": 3}}
b := []any{map[string]any{"a": 2, "b": 4, "c": 3}}
fmt.Println(cmp.Diff(a,b))
```
The result is:
```
[]any{
map[string]any{
- "a": int(1)
- "a": int(2)
- "b": int(2)
- "b": int(4)
"c": int(3)
},
}
```
Which means it traversed inside the map entries.

However, when I was doing this:
```go
a := []any{map[string]any{"a": 1, "b": 2, "c": 3}}
b := []any{map[string]any{"a": 2, "b": 4, "c": 6}}
fmt.Println(cmp.Diff(a,b))
```
The result become:
```
[]any{
- map[string]any{"a": int(1), "b": int(2), "c": int(3)},
+ map[string]any{"a": int(2), "b": int(4), "c": int(6)},
}
```

I've tested several tests, and it seems that it stops traversing only when the map have three or more differences. Is it a bug, or am I doing anything wrong? Since I'm using go-cmp to handle deserialized objects, this case will affect the path filter.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.