@@ -148,18 +148,15 @@ func (w *mergingWalker) finishDescent(w2 *mergingWalker) {
148148 * w .spareWalkers = append (* w .spareWalkers , w2 )
149149}
150150
151- func (w * mergingWalker ) derefMap (prefix string , v value.Value , dest * value.Map ) (errs ValidationErrors ) {
152- // taking dest as input so that it can be called as a one-liner with
153- // append.
151+ func (w * mergingWalker ) derefMap (prefix string , v value.Value ) (value.Map , ValidationErrors ) {
154152 if v == nil {
155- return nil
153+ return nil , nil
156154 }
157155 m , err := mapValue (v )
158156 if err != nil {
159- return errorf ("%v: %v" , prefix , err )
157+ return nil , errorf ("%v: %v" , prefix , err )
160158 }
161- * dest = m
162- return nil
159+ return m , nil
163160}
164161
165162func (w * mergingWalker ) visitListItems (t * schema.List , lhs , rhs value.List ) (errs ValidationErrors ) {
@@ -253,24 +250,26 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs value.List) (err
253250 return errs
254251}
255252
256- func (w * mergingWalker ) derefList (prefix string , v value.Value , dest * value.List ) (errs ValidationErrors ) {
257- // taking dest as input so that it can be called as a one-liner with
258- // append.
253+ func (w * mergingWalker ) derefList (prefix string , v value.Value ) (value.List , ValidationErrors ) {
259254 if v == nil {
260- return nil
255+ return nil , nil
261256 }
262257 l , err := listValue (v )
263258 if err != nil {
264- return errorf ("%v: %v" , prefix , err )
259+ return nil , errorf ("%v: %v" , prefix , err )
265260 }
266- * dest = l
267- return nil
261+ return l , nil
268262}
269263
270264func (w * mergingWalker ) doList (t * schema.List ) (errs ValidationErrors ) {
271- var lhs , rhs value.List
272- w .derefList ("lhs: " , w .lhs , & lhs )
273- w .derefList ("rhs: " , w .rhs , & rhs )
265+ lhs , _ := w .derefList ("lhs: " , w .lhs )
266+ if lhs != nil {
267+ defer lhs .Recycle ()
268+ }
269+ rhs , _ := w .derefList ("rhs: " , w .rhs )
270+ if rhs != nil {
271+ defer rhs .Recycle ()
272+ }
274273
275274 // If both lhs and rhs are empty/null, treat it as a
276275 // leaf: this helps preserve the empty/null
@@ -311,31 +310,10 @@ func (w *mergingWalker) visitMapItem(t *schema.Map, out map[string]interface{},
311310func (w * mergingWalker ) visitMapItems (t * schema.Map , lhs , rhs value.Map ) (errs ValidationErrors ) {
312311 out := map [string ]interface {}{}
313312
314- if lhs != nil {
315- lhs .Iterate (func (key string , val value.Value ) bool {
316- var rval value.Value
317- if rhs != nil {
318- if item , ok := rhs .Get (key ); ok {
319- rval = item
320- defer rval .Recycle ()
321- }
322- }
323- errs = append (errs , w .visitMapItem (t , out , key , val , rval )... )
324- return true
325- })
326- }
327-
328- if rhs != nil {
329- rhs .Iterate (func (key string , val value.Value ) bool {
330- if lhs != nil {
331- if lhs .Has (key ) {
332- return true
333- }
334- }
335- errs = append (errs , w .visitMapItem (t , out , key , nil , val )... )
336- return true
337- })
338- }
313+ value .MapZip (lhs , rhs , value .Unordered , func (key string , lhsValue , rhsValue value.Value ) bool {
314+ errs = append (errs , w .visitMapItem (t , out , key , lhsValue , rhsValue )... )
315+ return true
316+ })
339317 if len (out ) > 0 {
340318 i := interface {}(out )
341319 w .out = & i
@@ -345,14 +323,18 @@ func (w *mergingWalker) visitMapItems(t *schema.Map, lhs, rhs value.Map) (errs V
345323}
346324
347325func (w * mergingWalker ) doMap (t * schema.Map ) (errs ValidationErrors ) {
348- var lhs , rhs value.Map
349- w .derefMap ("lhs: " , w .lhs , & lhs )
350- w .derefMap ("rhs: " , w .rhs , & rhs )
351-
326+ lhs , _ := w .derefMap ("lhs: " , w .lhs )
327+ if lhs != nil {
328+ defer lhs .Recycle ()
329+ }
330+ rhs , _ := w .derefMap ("rhs: " , w .rhs )
331+ if rhs != nil {
332+ defer rhs .Recycle ()
333+ }
352334 // If both lhs and rhs are empty/null, treat it as a
353335 // leaf: this helps preserve the empty/null
354336 // distinction.
355- emptyPromoteToLeaf := (lhs == nil || lhs .Length () == 0 ) && (rhs == nil || rhs .Length () == 0 )
337+ emptyPromoteToLeaf := (lhs == nil || lhs .Empty () ) && (rhs == nil || rhs .Empty () )
356338
357339 if t .ElementRelationship == schema .Atomic || emptyPromoteToLeaf {
358340 w .doLeaf ()
0 commit comments