Skip to content

Commit 3b54170

Browse files
authored
Merge pull request #302 from mrIncompetent/fix-empty-struct-extraction
Fix ExtractItems to preserve empty maps and lists
2 parents 789a688 + 92b1eeb commit 3b54170

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

typed/remove.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ func (w *removingWalker) doList(t *schema.List) (errs ValidationErrors) {
5858
defer w.allocator.Free(l)
5959
// If list is null or empty just return
6060
if l == nil || l.Length() == 0 {
61+
// For extraction, we just return the value as is (which is nil or empty). For extraction the difference matters.
62+
if w.shouldExtract {
63+
w.out = w.value.Unstructured()
64+
}
6165
return nil
6266
}
6367

@@ -113,6 +117,10 @@ func (w *removingWalker) doMap(t *schema.Map) ValidationErrors {
113117
}
114118
// If map is null or empty just return
115119
if m == nil || m.Empty() {
120+
// For extraction, we just return the value as is (which is nil or empty). For extraction the difference matters.
121+
if w.shouldExtract {
122+
w.out = w.value.Unstructured()
123+
}
116124
return nil
117125
}
118126

typed/remove_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,38 @@ var removeCases = []removeTestCase{{
675675
),
676676
`{"mapOfMapsRecursive":{"a":{"b":null}}}`,
677677
`{"mapOfMapsRecursive": {"a":{"b":{"c":null}}}}`,
678+
}, {
679+
// empty list
680+
`{"listOfLists": []}`,
681+
_NS(
682+
_P("listOfLists"),
683+
),
684+
``,
685+
`{"listOfLists": []}`,
686+
}, {
687+
// null list
688+
`{"listOfLists": null}`,
689+
_NS(
690+
_P("listOfLists"),
691+
),
692+
``,
693+
`{"listOfLists": null}`,
694+
}, {
695+
// empty map
696+
`{"mapOfMaps": {}}`,
697+
_NS(
698+
_P("mapOfMaps"),
699+
),
700+
``,
701+
`{"mapOfMaps": {}}`,
702+
}, {
703+
// nil map
704+
`{"mapOfMaps": null}`,
705+
_NS(
706+
_P("mapOfMaps"),
707+
),
708+
``,
709+
`{"mapOfMaps": null}`,
678710
}},
679711
}}
680712

0 commit comments

Comments
 (0)