Skip to content

Commit 1ba3aa6

Browse files
authored
Merge pull request #295 from mrIncompetent/fix-extract-items-duplication
Fix ExtractItems duplication with WithAppendKeyFields
2 parents 3b54170 + e4bed54 commit 1ba3aa6

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

typed/remove.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,24 @@ func (w *removingWalker) doList(t *schema.List) (errs ValidationErrors) {
8484
path, _ := fieldpath.MakePath(pe)
8585
// save items on the path when we shouldExtract
8686
// but ignore them when we are removing (i.e. !w.shouldExtract)
87-
if w.toRemove.Has(path) {
88-
if w.shouldExtract {
89-
newItems = append(newItems, removeItemsWithSchema(item, w.toRemove, w.schema, t.ElementType, w.shouldExtract).Unstructured())
90-
} else {
91-
continue
87+
isExactPathMatch := w.toRemove.Has(path)
88+
isPrefixMatch := !w.toRemove.WithPrefix(pe).Empty()
89+
if w.shouldExtract {
90+
if isPrefixMatch {
91+
item = removeItemsWithSchema(item, w.toRemove.WithPrefix(pe), w.schema, t.ElementType, w.shouldExtract)
92+
}
93+
if isExactPathMatch || isPrefixMatch {
94+
newItems = append(newItems, item.Unstructured())
9295
}
93-
}
94-
if subset := w.toRemove.WithPrefix(pe); !subset.Empty() {
95-
item = removeItemsWithSchema(item, subset, w.schema, t.ElementType, w.shouldExtract)
9696
} else {
97-
// don't save items not on the path when we shouldExtract.
98-
if w.shouldExtract {
97+
if isExactPathMatch {
9998
continue
10099
}
100+
if isPrefixMatch {
101+
item = removeItemsWithSchema(item, w.toRemove.WithPrefix(pe), w.schema, t.ElementType, w.shouldExtract)
102+
}
103+
newItems = append(newItems, item.Unstructured())
101104
}
102-
newItems = append(newItems, item.Unstructured())
103105
}
104106
if len(newItems) > 0 {
105107
w.out = newItems

typed/remove_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ var associativeAndAtomicSchema = `types:
107107
- name: atomicMap
108108
type:
109109
namedType: myAtomicMap
110+
- name: atomicElementList
111+
type:
112+
namedType: myAtomicElementList
110113
- name: myList
111114
list:
112115
elementType:
@@ -117,9 +120,20 @@ var associativeAndAtomicSchema = `types:
117120
- id
118121
- name: myAtomicMap
119122
map:
123+
fields:
124+
- name: id
125+
type:
126+
scalar: string
120127
elementType:
121128
scalar: string
122129
elementRelationship: atomic
130+
- name: myAtomicElementList
131+
list:
132+
elementType:
133+
namedType: myAtomicMap
134+
elementRelationship: associative
135+
keys:
136+
- id
123137
- name: mySequence
124138
list:
125139
elementType:
@@ -972,6 +986,14 @@ var extractWithKeysCases = []extractWithKeysTestCase{{
972986
"list": []interface{}{nil},
973987
},
974988
},
989+
{
990+
// extract atomic element from associative list
991+
object: `{"atomicElementList":[{"id":"test-123","data":"value","extra":"field"}]}`,
992+
set: _NS(
993+
_P("atomicElementList", _KBF("id", "test-123")),
994+
),
995+
wantOutput: typed.YAMLObject(`{"atomicElementList":[{"id":"test-123","data":"value","extra":"field"}]}`),
996+
},
975997
},
976998
}}
977999

0 commit comments

Comments
 (0)