@@ -25,12 +25,14 @@ import (
25
25
)
26
26
27
27
type mergingWalker struct {
28
- errorFormatter
29
28
lhs * value.Value
30
29
rhs * value.Value
31
30
schema * schema.Schema
32
31
typeRef schema.TypeRef
33
32
33
+ // Current path that we are merging
34
+ path fieldpath.Path
35
+
34
36
// How to merge. Called after schema validation for all leaf fields.
35
37
rule mergeRule
36
38
@@ -69,11 +71,11 @@ var (
69
71
func (w * mergingWalker ) merge () (errs ValidationErrors ) {
70
72
if w .lhs == nil && w .rhs == nil {
71
73
// check this condidition here instead of everywhere below.
72
- return w . errorf ("at least one of lhs and rhs must be provided" )
74
+ return errorf ("at least one of lhs and rhs must be provided" )
73
75
}
74
76
a , ok := w .schema .Resolve (w .typeRef )
75
77
if ! ok {
76
- return w . errorf ("schema error: no type found matching: %v" , * w .typeRef .NamedType )
78
+ return errorf ("schema error: no type found matching: %v" , * w .typeRef .NamedType )
77
79
}
78
80
79
81
alhs := deduceAtom (a , w .lhs )
@@ -107,8 +109,8 @@ func (w *mergingWalker) doLeaf() {
107
109
}
108
110
109
111
func (w * mergingWalker ) doScalar (t * schema.Scalar ) (errs ValidationErrors ) {
110
- errs = append (errs , w . validateScalar (t , w .lhs , "lhs: " )... )
111
- errs = append (errs , w . validateScalar (t , w .rhs , "rhs: " )... )
112
+ errs = append (errs , validateScalar (t , w .lhs , "lhs: " )... )
113
+ errs = append (errs , validateScalar (t , w .rhs , "rhs: " )... )
112
114
if len (errs ) > 0 {
113
115
return errs
114
116
}
@@ -132,7 +134,7 @@ func (w *mergingWalker) prepareDescent(pe fieldpath.PathElement, tr schema.TypeR
132
134
}
133
135
* w2 = * w
134
136
w2 .typeRef = tr
135
- w2 .errorFormatter . descend ( pe )
137
+ w2 .path = append ( w2 . path , pe )
136
138
w2 .lhs = nil
137
139
w2 .rhs = nil
138
140
w2 .out = nil
@@ -142,7 +144,7 @@ func (w *mergingWalker) prepareDescent(pe fieldpath.PathElement, tr schema.TypeR
142
144
func (w * mergingWalker ) finishDescent (w2 * mergingWalker ) {
143
145
// if the descent caused a realloc, ensure that we reuse the buffer
144
146
// for the next sibling.
145
- w .errorFormatter = w2 .errorFormatter . parent ()
147
+ w .path = w2 .path [: len ( w2 . path ) - 1 ]
146
148
* w .spareWalkers = append (* w .spareWalkers , w2 )
147
149
}
148
150
@@ -154,7 +156,7 @@ func (w *mergingWalker) derefMap(prefix string, v *value.Value, dest *map[string
154
156
}
155
157
m , err := mapValue (* v )
156
158
if err != nil {
157
- return w . prefixError ( prefix , err )
159
+ return errorf ( "%v: %v" , prefix , err )
158
160
}
159
161
* dest = m
160
162
return nil
@@ -174,14 +176,14 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs []interface{}) (
174
176
for i , child := range rhs {
175
177
pe , err := listItemToPathElement (t , i , child )
176
178
if err != nil {
177
- errs = append (errs , w . errorf ("rhs: element %v: %v" , i , err .Error ())... )
179
+ errs = append (errs , errorf ("rhs: element %v: %v" , i , err .Error ())... )
178
180
// If we can't construct the path element, we can't
179
181
// even report errors deeper in the schema, so bail on
180
182
// this element.
181
183
continue
182
184
}
183
185
if _ , ok := observedRHS .Get (pe ); ok {
184
- errs = append (errs , w . errorf ("rhs: duplicate entries for key %v" , pe .String ())... )
186
+ errs = append (errs , errorf ("rhs: duplicate entries for key %v" , pe .String ())... )
185
187
}
186
188
observedRHS .Insert (pe , child )
187
189
rhsOrder = append (rhsOrder , pe )
@@ -192,14 +194,14 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs []interface{}) (
192
194
for i , child := range lhs {
193
195
pe , err := listItemToPathElement (t , i , child )
194
196
if err != nil {
195
- errs = append (errs , w . errorf ("lhs: element %v: %v" , i , err .Error ())... )
197
+ errs = append (errs , errorf ("lhs: element %v: %v" , i , err .Error ())... )
196
198
// If we can't construct the path element, we can't
197
199
// even report errors deeper in the schema, so bail on
198
200
// this element.
199
201
continue
200
202
}
201
203
if observedLHS .Has (pe ) {
202
- errs = append (errs , w . errorf ("lhs: duplicate entries for key %v" , pe .String ())... )
204
+ errs = append (errs , errorf ("lhs: duplicate entries for key %v" , pe .String ())... )
203
205
continue
204
206
}
205
207
observedLHS .Insert (pe )
@@ -210,7 +212,7 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs []interface{}) (
210
212
w2 .rhs = & rchild
211
213
}
212
214
if newErrs := w2 .merge (); len (newErrs ) > 0 {
213
- errs = append (errs , newErrs ... )
215
+ errs = append (errs , newErrs .WithPrefix ( pe . String ()) . .. )
214
216
} else if w2 .out != nil {
215
217
out = append (out , * w2 .out )
216
218
}
@@ -225,7 +227,7 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs []interface{}) (
225
227
w2 := w .prepareDescent (pe , t .ElementType )
226
228
w2 .rhs = & value
227
229
if newErrs := w2 .merge (); len (newErrs ) > 0 {
228
- errs = append (errs , newErrs ... )
230
+ errs = append (errs , newErrs .WithPrefix ( pe . String ()) . .. )
229
231
} else if w2 .out != nil {
230
232
out = append (out , * w2 .out )
231
233
}
@@ -248,7 +250,7 @@ func (w *mergingWalker) derefList(prefix string, v *value.Value, dest *[]interfa
248
250
}
249
251
l , err := listValue (* v )
250
252
if err != nil {
251
- return w . prefixError ( prefix , err )
253
+ return errorf ( "%v: %v" , prefix , err )
252
254
}
253
255
* dest = l
254
256
return nil
@@ -283,11 +285,12 @@ func (w *mergingWalker) visitMapItem(t *schema.Map, out map[string]interface{},
283
285
if sf , ok := t .FindField (key ); ok {
284
286
fieldType = sf .Type
285
287
}
286
- w2 := w .prepareDescent (fieldpath.PathElement {FieldName : & key }, fieldType )
288
+ pe := fieldpath.PathElement {FieldName : & key }
289
+ w2 := w .prepareDescent (pe , fieldType )
287
290
w2 .lhs = lhs
288
291
w2 .rhs = rhs
289
292
if newErrs := w2 .merge (); len (newErrs ) > 0 {
290
- errs = append (errs , newErrs ... )
293
+ errs = append (errs , newErrs .WithPrefix ( pe . String ()) . .. )
291
294
} else if w2 .out != nil {
292
295
out [key ] = * w2 .out
293
296
}
0 commit comments