Skip to content

Commit e202664

Browse files
committed
add deepEqualValues; diffMap -> diffMapStringKey
1 parent 03c8f36 commit e202664

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

libs/structdiff/diff.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,18 @@ func diffValues(path *pathNode, v1, v2 reflect.Value, changes *[]Change) {
116116
}
117117
case reflect.Map:
118118
if v1Type.Key().Kind() == reflect.String {
119-
diffMap(path, v1, v2, changes)
119+
diffMapStringKey(path, v1, v2, changes)
120120
} else {
121-
if !reflect.DeepEqual(v1.Interface(), v2.Interface()) {
122-
*changes = append(*changes, Change{Field: path.String(), Old: v1.Interface(), New: v2.Interface()})
123-
}
124-
}
125-
default: // primitives, interfaces, etc.
126-
if !reflect.DeepEqual(v1.Interface(), v2.Interface()) {
127-
*changes = append(*changes, Change{Field: path.String(), Old: v1.Interface(), New: v2.Interface()})
121+
deepEqualValues(path, v1, v2, changes)
128122
}
123+
default:
124+
deepEqualValues(path, v1, v2, changes)
125+
}
126+
}
127+
128+
func deepEqualValues(path *pathNode, v1, v2 reflect.Value, changes *[]Change) {
129+
if !reflect.DeepEqual(v1.Interface(), v2.Interface()) {
130+
*changes = append(*changes, Change{Field: path.String(), Old: v1.Interface(), New: v2.Interface()})
129131
}
130132
}
131133

@@ -173,14 +175,15 @@ func diffStruct(path *pathNode, s1, s2 reflect.Value, changes *[]Change) {
173175
}
174176
}
175177

176-
func diffMap(path *pathNode, m1, m2 reflect.Value, changes *[]Change) {
178+
func diffMapStringKey(path *pathNode, m1, m2 reflect.Value, changes *[]Change) {
177179
keySet := map[string]reflect.Value{}
178180
for _, k := range m1.MapKeys() {
179-
ks := fmt.Sprint(k.Interface())
181+
// Key is always string at this point
182+
ks := k.Interface().(string)
180183
keySet[ks] = k
181184
}
182185
for _, k := range m2.MapKeys() {
183-
ks := fmt.Sprint(k.Interface())
186+
ks := k.Interface().(string)
184187
keySet[ks] = k
185188
}
186189

0 commit comments

Comments
 (0)