@@ -58,16 +58,6 @@ func (c *keyFuncCaller) call(elem any) (string, string) {
5858 return keyField , keyValue
5959}
6060
61- func advanceTrie (root * structtrie.Node , node * structtrie.Node , pathNode * structpath.PathNode ) * structtrie.Node {
62- if root == nil {
63- return nil
64- }
65- if node == nil {
66- node = root
67- }
68- return node .Child (pathNode )
69- }
70-
7161func keyFuncFor (node * structtrie.Node ) KeyFunc {
7262 if node == nil {
7363 return nil
@@ -123,15 +113,15 @@ func GetStructDiff(a, b any, sliceTrie *structtrie.Node) ([]Change, error) {
123113 return nil , fmt .Errorf ("type mismatch: %v vs %v" , v1 .Type (), v2 .Type ())
124114 }
125115
126- if err := diffValues (sliceTrie , sliceTrie , nil , v1 , v2 , & changes ); err != nil {
116+ if err := diffValues (sliceTrie , nil , v1 , v2 , & changes ); err != nil {
127117 return nil , err
128118 }
129119 return changes , nil
130120}
131121
132122// diffValues appends changes between v1 and v2 to the slice. path is the current
133123// JSON-style path (dot + brackets). At the root path is "".
134- func diffValues (trieRoot * structtrie. Node , trieNode * structtrie.Node , path * structpath.PathNode , v1 , v2 reflect.Value , changes * []Change ) error {
124+ func diffValues (trieNode * structtrie.Node , path * structpath.PathNode , v1 , v2 reflect.Value , changes * []Change ) error {
135125 if ! v1 .IsValid () {
136126 if ! v2 .IsValid () {
137127 return nil
@@ -174,26 +164,26 @@ func diffValues(trieRoot *structtrie.Node, trieNode *structtrie.Node, path *stru
174164
175165 switch kind {
176166 case reflect .Pointer :
177- return diffValues (trieRoot , trieNode , path , v1 .Elem (), v2 .Elem (), changes )
167+ return diffValues (trieNode , path , v1 .Elem (), v2 .Elem (), changes )
178168 case reflect .Struct :
179- return diffStruct (trieRoot , trieNode , path , v1 , v2 , changes )
169+ return diffStruct (trieNode , path , v1 , v2 , changes )
180170 case reflect .Slice , reflect .Array :
181171 if keyFunc := keyFuncFor (trieNode ); keyFunc != nil {
182- return diffSliceByKey (trieRoot , trieNode , path , v1 , v2 , keyFunc , changes )
172+ return diffSliceByKey (trieNode , path , v1 , v2 , keyFunc , changes )
183173 } else if v1 .Len () != v2 .Len () {
184174 * changes = append (* changes , Change {Path : path , Old : v1 .Interface (), New : v2 .Interface ()})
185175 } else {
186176 for i := range v1 .Len () {
187177 node := structpath .NewIndex (path , i )
188- nextTrie := advanceTrie ( trieRoot , trieNode , node )
189- if err := diffValues (trieRoot , nextTrie , node , v1 .Index (i ), v2 .Index (i ), changes ); err != nil {
178+ nextTrie := trieNode . Child ( node )
179+ if err := diffValues (nextTrie , node , v1 .Index (i ), v2 .Index (i ), changes ); err != nil {
190180 return err
191181 }
192182 }
193183 }
194184 case reflect .Map :
195185 if v1Type .Key ().Kind () == reflect .String {
196- return diffMapStringKey (trieRoot , trieNode , path , v1 , v2 , changes )
186+ return diffMapStringKey (trieNode , path , v1 , v2 , changes )
197187 } else {
198188 deepEqualValues (path , v1 , v2 , changes )
199189 }
@@ -209,7 +199,7 @@ func deepEqualValues(path *structpath.PathNode, v1, v2 reflect.Value, changes *[
209199 }
210200}
211201
212- func diffStruct (trieRoot * structtrie. Node , trieNode * structtrie.Node , path * structpath.PathNode , s1 , s2 reflect.Value , changes * []Change ) error {
202+ func diffStruct (trieNode * structtrie.Node , path * structpath.PathNode , s1 , s2 reflect.Value , changes * []Change ) error {
213203 t := s1 .Type ()
214204 forced1 := getForceSendFields (s1 )
215205 forced2 := getForceSendFields (s2 )
@@ -222,7 +212,7 @@ func diffStruct(trieRoot *structtrie.Node, trieNode *structtrie.Node, path *stru
222212
223213 // Continue traversing embedded structs. Do not add the key to the path though.
224214 if sf .Anonymous {
225- if err := diffValues (trieRoot , trieNode , path , s1 .Field (i ), s2 .Field (i ), changes ); err != nil {
215+ if err := diffValues (trieNode , path , s1 .Field (i ), s2 .Field (i ), changes ); err != nil {
226216 return err
227217 }
228218 continue
@@ -258,15 +248,15 @@ func diffStruct(trieRoot *structtrie.Node, trieNode *structtrie.Node, path *stru
258248 }
259249 }
260250
261- nextTrie := advanceTrie ( trieRoot , trieNode , node )
262- if err := diffValues (trieRoot , nextTrie , node , v1Field , v2Field , changes ); err != nil {
251+ nextTrie := trieNode . Child ( node )
252+ if err := diffValues (nextTrie , node , v1Field , v2Field , changes ); err != nil {
263253 return err
264254 }
265255 }
266256 return nil
267257}
268258
269- func diffMapStringKey (trieRoot * structtrie. Node , trieNode * structtrie.Node , path * structpath.PathNode , m1 , m2 reflect.Value , changes * []Change ) error {
259+ func diffMapStringKey (trieNode * structtrie.Node , path * structpath.PathNode , m1 , m2 reflect.Value , changes * []Change ) error {
270260 keySet := map [string ]reflect.Value {}
271261 for _ , k := range m1 .MapKeys () {
272262 // Key is always string at this point
@@ -289,8 +279,8 @@ func diffMapStringKey(trieRoot *structtrie.Node, trieNode *structtrie.Node, path
289279 v1 := m1 .MapIndex (k )
290280 v2 := m2 .MapIndex (k )
291281 node := structpath .NewStringKey (path , ks )
292- nextTrie := advanceTrie ( trieRoot , trieNode , node )
293- if err := diffValues (trieRoot , nextTrie , node , v1 , v2 , changes ); err != nil {
282+ nextTrie := trieNode . Child ( node )
283+ if err := diffValues (nextTrie , node , v1 , v2 , changes ); err != nil {
294284 return err
295285 }
296286 }
@@ -335,7 +325,7 @@ func validateKeyFuncElementType(seq reflect.Value, expected reflect.Type) error
335325// diffSliceByKey compares two slices using the provided key function.
336326// Elements are matched by their (keyField, keyValue) pairs instead of by index.
337327// Duplicate keys are allowed and matched in order.
338- func diffSliceByKey (trieRoot * structtrie. Node , trieNode * structtrie.Node , path * structpath.PathNode , v1 , v2 reflect.Value , keyFunc KeyFunc , changes * []Change ) error {
328+ func diffSliceByKey (trieNode * structtrie.Node , path * structpath.PathNode , v1 , v2 reflect.Value , keyFunc KeyFunc , changes * []Change ) error {
339329 caller , err := newKeyFuncCaller (keyFunc )
340330 if err != nil {
341331 return err
@@ -393,8 +383,8 @@ func diffSliceByKey(trieRoot *structtrie.Node, trieNode *structtrie.Node, path *
393383 minLen := min (len (list1 ), len (list2 ))
394384 for i := range minLen {
395385 node := structpath .NewKeyValue (path , keyField , keyValue )
396- nextTrie := advanceTrie ( trieRoot , trieNode , node )
397- if err := diffValues (trieRoot , nextTrie , node , list1 [i ].value , list2 [i ].value , changes ); err != nil {
386+ nextTrie := trieNode . Child ( node )
387+ if err := diffValues (nextTrie , node , list1 [i ].value , list2 [i ].value , changes ); err != nil {
398388 return err
399389 }
400390 }
0 commit comments