Skip to content

Commit 0e49988

Browse files
committed
tofieldset: Add tests to show it already allow duplicates
1 parent 042aa8e commit 0e49988

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

typed/tofieldset.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,31 @@ func (v *toFieldSetWalker) doScalar(t *schema.Scalar) ValidationErrors {
9494
}
9595

9696
func (v *toFieldSetWalker) visitListItems(t *schema.List, list value.List) (errs ValidationErrors) {
97+
// Keeps track of the PEs we've seen
98+
seen := fieldpath.MakePathElementSet(list.Length())
99+
// Keeps tracks of the PEs we've counted as duplicates
100+
duplicates := fieldpath.MakePathElementSet(list.Length())
97101
for i := 0; i < list.Length(); i++ {
98102
child := list.At(i)
99103
pe, _ := listItemToPathElement(v.allocator, v.schema, t, child)
104+
if seen.Has(pe) {
105+
if duplicates.Has(pe) {
106+
// do nothing
107+
} else {
108+
v.set.Insert(append(v.path, pe))
109+
duplicates.Insert(pe)
110+
}
111+
} else {
112+
seen.Insert(pe)
113+
}
114+
}
115+
116+
for i := 0; i < list.Length(); i++ {
117+
child := list.At(i)
118+
pe, _ := listItemToPathElement(v.allocator, v.schema, t, child)
119+
if duplicates.Has(pe) {
120+
continue
121+
}
100122
v2 := v.prepareDescent(pe, t.ElementType)
101123
v2.value = child
102124
errs = append(errs, v2.toFieldSet()...)

typed/toset_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,12 @@ var fieldsetCases = []fieldsetTestCase{{
155155
_P("setStr", _V("b")),
156156
_P("setStr", _V("c")),
157157
)},
158-
{`{"setBool":[true,false]}`, _NS(
158+
{`{"setStr":["a","b","c","a","b","c","c"]}`, _NS(
159+
_P("setStr", _V("a")),
160+
_P("setStr", _V("b")),
161+
_P("setStr", _V("c")),
162+
)},
163+
{`{"setBool":[true,false,true]}`, _NS(
159164
_P("setBool", _V(true)),
160165
_P("setBool", _V(false)),
161166
)},
@@ -244,6 +249,16 @@ var fieldsetCases = []fieldsetTestCase{{
244249
_P("list", _KBF("key", "b", "id", 1), "key"),
245250
_P("list", _KBF("key", "b", "id", 1), "id"),
246251
)},
252+
{`{"list":[{"key":"a","id":1,"nv":2},{"key":"a","id":2,"nv":3},{"key":"b","id":1},{"key":"a","id":2,"bv":true}]}`, _NS(
253+
_P("list", _KBF("key", "a", "id", 1)),
254+
_P("list", _KBF("key", "a", "id", 1), "key"),
255+
_P("list", _KBF("key", "a", "id", 1), "id"),
256+
_P("list", _KBF("key", "a", "id", 1), "nv"),
257+
_P("list", _KBF("key", "a", "id", 2)),
258+
_P("list", _KBF("key", "b", "id", 1)),
259+
_P("list", _KBF("key", "b", "id", 1), "key"),
260+
_P("list", _KBF("key", "b", "id", 1), "id"),
261+
)},
247262
{`{"atomicList":["a","a","a"]}`, _NS(_P("atomicList"))},
248263
},
249264
}}
@@ -257,9 +272,9 @@ func (tt fieldsetTestCase) test(t *testing.T) {
257272
v := v
258273
t.Run(fmt.Sprintf("%v-%v", tt.name, i), func(t *testing.T) {
259274
t.Parallel()
260-
tv, err := parser.Type(tt.rootTypeName).FromYAML(v.object)
275+
tv, err := parser.Type(tt.rootTypeName).FromYAML(v.object, typed.AllowDuplicates)
261276
if err != nil {
262-
t.Errorf("failed to parse object: %v", err)
277+
t.Fatalf("failed to parse object: %v", err)
263278
}
264279
fs, err := tv.ToFieldSet()
265280
if err != nil {

0 commit comments

Comments
 (0)