Skip to content

Commit 476102f

Browse files
authored
Merge pull request #246 from apelisse/mild-cleanups
Mild cleanups
2 parents 3f658af + 4d85fbb commit 476102f

File tree

8 files changed

+25
-25
lines changed

8 files changed

+25
-25
lines changed

internal/testdata/schema.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ types:
8787
list:
8888
elementType:
8989
scalar: string
90+
elementRelationship: atomic
9091
- name: untyped
9192
map:
9293
fields:
9394
- name: elementRelationship
9495
type:
9596
scalar: string
96-

schema/schemaschema.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ var SchemaSchemaYAML = `types:
145145
list:
146146
elementType:
147147
scalar: string
148+
elementRelationship: atomic
148149
- name: untyped
149150
map:
150151
fields:

typed/compare.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (w *compareWalker) visitListItems(t *schema.List, lhs, rhs value.List) (err
216216
lValues := fieldpath.MakePathElementValueMap(lLen)
217217
for i := 0; i < lLen; i++ {
218218
child := lhs.At(i)
219-
pe, err := listItemToPathElement(w.allocator, w.schema, t, i, child)
219+
pe, err := listItemToPathElement(w.allocator, w.schema, t, child)
220220
if err != nil {
221221
errs = append(errs, errorf("element %v: %v", i, err.Error())...)
222222
// If we can't construct the path element, we can't
@@ -234,7 +234,7 @@ func (w *compareWalker) visitListItems(t *schema.List, lhs, rhs value.List) (err
234234
rValues := fieldpath.MakePathElementValueMap(rLen)
235235
for i := 0; i < rLen; i++ {
236236
rValue := rhs.At(i)
237-
pe, err := listItemToPathElement(w.allocator, w.schema, t, i, rValue)
237+
pe, err := listItemToPathElement(w.allocator, w.schema, t, rValue)
238238
if err != nil {
239239
errs = append(errs, errorf("element %v: %v", i, err.Error())...)
240240
// If we can't construct the path element, we can't
@@ -247,15 +247,15 @@ func (w *compareWalker) visitListItems(t *schema.List, lhs, rhs value.List) (err
247247
continue
248248
}
249249
rValues.Insert(pe, rValue)
250-
// We can merge with nil if lValue is not present.
250+
// We can compare with nil if lValue is not present.
251251
lValue, _ := lValues.Get(pe)
252-
errs = append(errs, w.mergeListItem(t, pe, lValue, rValue)...)
252+
errs = append(errs, w.compareListItem(t, pe, lValue, rValue)...)
253253
}
254254

255255
// Add items from left that are not in right.
256256
for i := 0; i < lLen; i++ {
257257
lValue := lhs.At(i)
258-
pe, err := listItemToPathElement(w.allocator, w.schema, t, i, lValue)
258+
pe, err := listItemToPathElement(w.allocator, w.schema, t, lValue)
259259
if err != nil {
260260
errs = append(errs, errorf("element %v: %v", i, err.Error())...)
261261
// If we can't construct the path element, we can't
@@ -266,7 +266,7 @@ func (w *compareWalker) visitListItems(t *schema.List, lhs, rhs value.List) (err
266266
if _, found := rValues.Get(pe); found {
267267
continue
268268
}
269-
errs = append(errs, w.mergeListItem(t, pe, lValue, nil)...)
269+
errs = append(errs, w.compareListItem(t, pe, lValue, nil)...)
270270
}
271271

272272
return
@@ -282,7 +282,7 @@ func (w *compareWalker) indexListPathElements(t *schema.List, list value.List) (
282282
pes := make([]fieldpath.PathElement, 0, length)
283283
for i := 0; i < length; i++ {
284284
child := list.At(i)
285-
pe, err := listItemToPathElement(w.allocator, w.schema, t, i, child)
285+
pe, err := listItemToPathElement(w.allocator, w.schema, t, child)
286286
if err != nil {
287287
errs = append(errs, errorf("element %v: %v", i, err.Error())...)
288288
// If we can't construct the path element, we can't
@@ -300,7 +300,7 @@ func (w *compareWalker) indexListPathElements(t *schema.List, list value.List) (
300300
return pes, observed, errs
301301
}
302302

303-
func (w *compareWalker) mergeListItem(t *schema.List, pe fieldpath.PathElement, lChild, rChild value.Value) ValidationErrors {
303+
func (w *compareWalker) compareListItem(t *schema.List, pe fieldpath.PathElement, lChild, rChild value.Value) ValidationErrors {
304304
w2 := w.prepareDescent(pe, t.ElementType, w.comparison)
305305
w2.lhs = lChild
306306
w2.rhs = rChild

typed/helpers.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func getAssociativeKeyDefault(s *schema.Schema, list *schema.List, fieldName str
197197
return field.Default, nil
198198
}
199199

200-
func keyedAssociativeListItemToPathElement(a value.Allocator, s *schema.Schema, list *schema.List, index int, child value.Value) (fieldpath.PathElement, error) {
200+
func keyedAssociativeListItemToPathElement(a value.Allocator, s *schema.Schema, list *schema.List, child value.Value) (fieldpath.PathElement, error) {
201201
pe := fieldpath.PathElement{}
202202
if child.IsNull() {
203203
// null entries are illegal.
@@ -225,7 +225,7 @@ func keyedAssociativeListItemToPathElement(a value.Allocator, s *schema.Schema,
225225
return pe, nil
226226
}
227227

228-
func setItemToPathElement(list *schema.List, index int, child value.Value) (fieldpath.PathElement, error) {
228+
func setItemToPathElement(child value.Value) (fieldpath.PathElement, error) {
229229
pe := fieldpath.PathElement{}
230230
switch {
231231
case child.IsMap():
@@ -245,16 +245,15 @@ func setItemToPathElement(list *schema.List, index int, child value.Value) (fiel
245245
}
246246
}
247247

248-
func listItemToPathElement(a value.Allocator, s *schema.Schema, list *schema.List, index int, child value.Value) (fieldpath.PathElement, error) {
249-
if list.ElementRelationship == schema.Associative {
250-
if len(list.Keys) > 0 {
251-
return keyedAssociativeListItemToPathElement(a, s, list, index, child)
252-
}
248+
func listItemToPathElement(a value.Allocator, s *schema.Schema, list *schema.List, child value.Value) (fieldpath.PathElement, error) {
249+
if list.ElementRelationship != schema.Associative {
250+
return fieldpath.PathElement{}, errors.New("invalid indexing of non-associative list")
251+
}
253252

254-
// If there's no keys, then we must be a set of primitives.
255-
return setItemToPathElement(list, index, child)
253+
if len(list.Keys) > 0 {
254+
return keyedAssociativeListItemToPathElement(a, s, list, child)
256255
}
257256

258-
// Use the index as a key for atomic lists.
259-
return fieldpath.PathElement{Index: &index}, nil
257+
// If there's no keys, then we must be a set of primitives.
258+
return setItemToPathElement(child)
260259
}

typed/merge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ func (w *mergingWalker) indexListPathElements(t *schema.List, list value.List) (
282282
pes := make([]fieldpath.PathElement, 0, length)
283283
for i := 0; i < length; i++ {
284284
child := list.At(i)
285-
pe, err := listItemToPathElement(w.allocator, w.schema, t, i, child)
285+
pe, err := listItemToPathElement(w.allocator, w.schema, t, child)
286286
if err != nil {
287287
errs = append(errs, errorf("element %v: %v", i, err.Error())...)
288288
// If we can't construct the path element, we can't

typed/remove.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ func (w *removingWalker) doList(t *schema.List) (errs ValidationErrors) {
7474
iter := l.RangeUsing(w.allocator)
7575
defer w.allocator.Free(iter)
7676
for iter.Next() {
77-
i, item := iter.Item()
77+
_, item := iter.Item()
7878
// Ignore error because we have already validated this list
79-
pe, _ := listItemToPathElement(w.allocator, w.schema, t, i, item)
79+
pe, _ := listItemToPathElement(w.allocator, w.schema, t, item)
8080
path, _ := fieldpath.MakePath(pe)
8181
// save items on the path when we shouldExtract
8282
// but ignore them when we are removing (i.e. !w.shouldExtract)

typed/tofieldset.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (v *toFieldSetWalker) doScalar(t *schema.Scalar) ValidationErrors {
9696
func (v *toFieldSetWalker) visitListItems(t *schema.List, list value.List) (errs ValidationErrors) {
9797
for i := 0; i < list.Length(); i++ {
9898
child := list.At(i)
99-
pe, _ := listItemToPathElement(v.allocator, v.schema, t, i, child)
99+
pe, _ := listItemToPathElement(v.allocator, v.schema, t, child)
100100
v2 := v.prepareDescent(pe, t.ElementType)
101101
v2.value = child
102102
errs = append(errs, v2.toFieldSet()...)

typed/validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (v *validatingObjectWalker) visitListItems(t *schema.List, list value.List)
129129
pe.Index = &i
130130
} else {
131131
var err error
132-
pe, err = listItemToPathElement(v.allocator, v.schema, t, i, child)
132+
pe, err = listItemToPathElement(v.allocator, v.schema, t, child)
133133
if err != nil {
134134
errs = append(errs, errorf("element %v: %v", i, err.Error())...)
135135
// If we can't construct the path element, we can't

0 commit comments

Comments
 (0)