Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions typed/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,24 @@ func (w *removingWalker) doList(t *schema.List) (errs ValidationErrors) {
path, _ := fieldpath.MakePath(pe)
// save items on the path when we shouldExtract
// but ignore them when we are removing (i.e. !w.shouldExtract)
if w.toRemove.Has(path) {
if w.shouldExtract {
newItems = append(newItems, removeItemsWithSchema(item, w.toRemove, w.schema, t.ElementType, w.shouldExtract).Unstructured())
} else {
continue
isExactPathMatch := w.toRemove.Has(path)
isPrefixMatch := !w.toRemove.WithPrefix(pe).Empty()
if w.shouldExtract {
if isPrefixMatch {
item = removeItemsWithSchema(item, w.toRemove.WithPrefix(pe), w.schema, t.ElementType, w.shouldExtract)
}
if isExactPathMatch || isPrefixMatch {
newItems = append(newItems, item.Unstructured())
}
}
if subset := w.toRemove.WithPrefix(pe); !subset.Empty() {
item = removeItemsWithSchema(item, subset, w.schema, t.ElementType, w.shouldExtract)
} else {
// don't save items not on the path when we shouldExtract.
if w.shouldExtract {
if isExactPathMatch {
continue
}
if isPrefixMatch {
item = removeItemsWithSchema(item, w.toRemove.WithPrefix(pe), w.schema, t.ElementType, w.shouldExtract)
}
newItems = append(newItems, item.Unstructured())
}
newItems = append(newItems, item.Unstructured())
}
if len(newItems) > 0 {
w.out = newItems
Expand Down
22 changes: 22 additions & 0 deletions typed/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ var associativeAndAtomicSchema = `types:
- name: atomicMap
type:
namedType: myAtomicMap
- name: atomicElementList
type:
namedType: myAtomicElementList
- name: myList
list:
elementType:
Expand All @@ -117,9 +120,20 @@ var associativeAndAtomicSchema = `types:
- id
- name: myAtomicMap
map:
fields:
- name: id
type:
scalar: string
elementType:
scalar: string
elementRelationship: atomic
- name: myAtomicElementList
list:
elementType:
namedType: myAtomicMap
elementRelationship: associative
keys:
- id
- name: mySequence
list:
elementType:
Expand Down Expand Up @@ -972,6 +986,14 @@ var extractWithKeysCases = []extractWithKeysTestCase{{
"list": []interface{}{nil},
},
},
{
// extract atomic element from associative list
object: `{"atomicElementList":[{"id":"test-123","data":"value","extra":"field"}]}`,
set: _NS(
_P("atomicElementList", _KBF("id", "test-123")),
),
wantOutput: typed.YAMLObject(`{"atomicElementList":[{"id":"test-123","data":"value","extra":"field"}]}`),
},
},
}}

Expand Down