Skip to content

Commit

Permalink
POC
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzielenski committed May 23, 2024
1 parent cf09e71 commit d7c26a3
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 2 deletions.
20 changes: 18 additions & 2 deletions typed/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"sigs.k8s.io/structured-merge-diff/v4/value"
)

var REMOVEKEEPEMPTYCOLLECTIONS = false

type removingWalker struct {
value value.Value
out interface{}
Expand Down Expand Up @@ -58,6 +60,9 @@ func (w *removingWalker) doList(t *schema.List) (errs ValidationErrors) {
defer w.allocator.Free(l)
// If list is null or empty just return
if l == nil || l.Length() == 0 {
if REMOVEKEEPEMPTYCOLLECTIONS {
w.out = w.value.Unstructured()
}
return nil
}

Expand All @@ -66,6 +71,10 @@ func (w *removingWalker) doList(t *schema.List) (errs ValidationErrors) {
if t.ElementRelationship == schema.Atomic {
if w.shouldExtract {
w.out = w.value.Unstructured()
} else if !w.toRemove.Has(fieldpath.Path{}) && REMOVEKEEPEMPTYCOLLECTIONS {
// If the atomic list itself wasn't being removed, then it
// is being set to empty list (we only get here if a prefix of this fieldPath is in toRemove)
w.out = []interface{}{}
}
return nil
}
Expand Down Expand Up @@ -97,7 +106,7 @@ func (w *removingWalker) doList(t *schema.List) (errs ValidationErrors) {
}
newItems = append(newItems, item.Unstructured())
}
if len(newItems) > 0 {
if len(newItems) > 0 || REMOVEKEEPEMPTYCOLLECTIONS {
w.out = newItems
}
return nil
Expand All @@ -113,6 +122,9 @@ func (w *removingWalker) doMap(t *schema.Map) ValidationErrors {
}
// If map is null or empty just return
if m == nil || m.Empty() {
if REMOVEKEEPEMPTYCOLLECTIONS {
w.out = w.value
}
return nil
}

Expand All @@ -121,6 +133,10 @@ func (w *removingWalker) doMap(t *schema.Map) ValidationErrors {
if t.ElementRelationship == schema.Atomic {
if w.shouldExtract {
w.out = w.value.Unstructured()
} else if !w.toRemove.Has(fieldpath.Path{}) && REMOVEKEEPEMPTYCOLLECTIONS {
// If the atomic map itself wasn't being removed, then it
// is being set to empty map (we only get here if a prefix of this fieldPath is in toRemove)
w.out = map[string]interface{}{}
}
return nil
}
Expand Down Expand Up @@ -158,7 +174,7 @@ func (w *removingWalker) doMap(t *schema.Map) ValidationErrors {
newMap[k] = val.Unstructured()
return true
})
if len(newMap) > 0 {
if len(newMap) > 0 || REMOVEKEEPEMPTYCOLLECTIONS {
w.out = newMap
}
return nil
Expand Down
141 changes: 141 additions & 0 deletions typed/update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package typed_test

import (
"testing"

"sigs.k8s.io/structured-merge-diff/v4/internal/fixture"
"sigs.k8s.io/structured-merge-diff/v4/typed"
)

var updateParser = func() fixture.Parser {
parser, err := typed.NewParser(`
types:
- name: nestedOptionalFields
map:
fields:
- name: nestedList
type:
list:
elementRelationship: associative
keys:
- name
elementType:
map:
fields:
- name: name
type:
scalar: string
- name: value
type:
scalar: numeric
- name: nestedMap
type:
map:
elementType:
scalar: numeric
- name: nested
type:
map:
fields:
- name: numeric
type:
scalar: numeric
- name: string
type:
scalar: string
`)
if err != nil {
panic(err)
}
return fixture.SameVersionParser{T: parser.Type("nestedOptionalFields")}
}()

func TestUpdate(t *testing.T) {
tests := map[string]fixture.TestCase{
"delete_nested_fields_struct": {
Ops: []fixture.Operation{
fixture.Apply{
Manager: "default",
APIVersion: "v1",
Object: `
nested:
numeric: 1
string: my string
`,
},
fixture.Apply{
Manager: "default",
APIVersion: "v1",
Object: `
nested: {}
`,
},
},
APIVersion: `v1`,
Object: `{nested: {}}`,
},
"delete_nested_fields_list": {
Ops: []fixture.Operation{
fixture.Apply{
Manager: "default",
APIVersion: "v1",
Object: `
nestedList:
- name: first
- name: second
`,
},
fixture.Apply{
Manager: "default",
APIVersion: "v1",
Object: `
nestedList: []
`,
},
},
APIVersion: `v1`,
Object: `{nestedList: []}`,
},
"delete_nested_fields_map": {
Ops: []fixture.Operation{
fixture.Apply{
Manager: "default",
APIVersion: "v1",
Object: `
nestedMap:
first: 1
second: 2
`,
},
fixture.Apply{
Manager: "default",
APIVersion: "v1",
Object: `
nestedMap: {}
`,
},
},
APIVersion: `v1`,
Object: `{nestedMap: {}}`,
},
}

for name, tc := range tests {
tc2 := tc
t.Run(name, func(t *testing.T) {
typed.REMOVEKEEPEMPTYCOLLECTIONS = true
if err := tc.Test(updateParser); err != nil {
t.Fatal(err)
}
})

t.Run(name+"Nil", func(t *testing.T) {
typed.REMOVEKEEPEMPTYCOLLECTIONS = false
if err := tc2.Test(updateParser); err != nil {
t.Fatal(err)
}
})
}

}

0 comments on commit d7c26a3

Please sign in to comment.