Skip to content

Commit

Permalink
diff: copy map values
Browse files Browse the repository at this point in the history
Fixes kr#10
  • Loading branch information
bmizerany committed May 18, 2022
1 parent 4178042 commit ba4f47a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 9 additions & 2 deletions diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ func (d *differ) walk(e emitfer, av, bv reflect.Value, xformOk, wantType bool) {

for _, k := range sortedKeys(av, bv) {
esub := e.subf(t, "[%#v]", k)
ak := av.MapIndex(k)
bk := bv.MapIndex(k)
ak := maybeCopy(av.MapIndex(k))
bk := maybeCopy(bv.MapIndex(k))
esub.set(ak, bk)
if ak.IsValid() && bk.IsValid() {
d.walk(esub, ak, bk, true, false)
Expand Down Expand Up @@ -499,3 +499,10 @@ func stackDepth() int {
pc := make([]uintptr, 1000)
return runtime.Callers(0, pc)
}

func maybeCopy(v reflect.Value) reflect.Value {
if v.Kind() == reflect.Struct {
return reflect.ValueOf(v.Type()).Elem()
}
return v
}
4 changes: 4 additions & 0 deletions diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ func TestUnequal(t *testing.T) {
{"", "a"},
{make(chan int), make(chan int)},
{unsafe.Pointer(ptr(0)), unsafe.Pointer(ptr(0))},
{
map[int]struct{ F int }{0: {}},
map[int]struct{ F int }{0: {}},
},
}

for i, tt := range cases {
Expand Down

0 comments on commit ba4f47a

Please sign in to comment.