Skip to content

Commit 7666d3d

Browse files
authored
Merge pull request #65 from jennybuckley/fix-remove-node
Fix bug with removing non leaf items
2 parents 3f6c1e0 + aa9dd4e commit 7666d3d

File tree

7 files changed

+581
-29
lines changed

7 files changed

+581
-29
lines changed

fieldpath/set.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,10 @@ func (s *Set) iteratePrefix(prefix Path, f func(Path)) {
159159
func (s *Set) WithPrefix(pe PathElement) *Set {
160160
subset, ok := s.Children.Get(pe)
161161
if !ok {
162-
return NewSet()
162+
subset = NewSet()
163+
}
164+
if s.Members.Has(pe) {
165+
subset.Insert(MakePathOrDie(""))
163166
}
164167
return subset
165168
}

merge/key_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package merge_test
18+
19+
import (
20+
"testing"
21+
22+
"sigs.k8s.io/structured-merge-diff/fieldpath"
23+
. "sigs.k8s.io/structured-merge-diff/internal/fixture"
24+
"sigs.k8s.io/structured-merge-diff/typed"
25+
)
26+
27+
var associativeListParser = func() typed.ParseableType {
28+
parser, err := typed.NewParser(`types:
29+
- name: type
30+
struct:
31+
fields:
32+
- name: list
33+
type:
34+
namedType: associativeList
35+
- name: associativeList
36+
list:
37+
elementType:
38+
namedType: myElement
39+
elementRelationship: associative
40+
keys:
41+
- name
42+
- name: myElement
43+
struct:
44+
fields:
45+
- name: name
46+
type:
47+
scalar: string
48+
- name: value
49+
type:
50+
scalar: numeric
51+
`)
52+
if err != nil {
53+
panic(err)
54+
}
55+
return parser.Type("type")
56+
}()
57+
58+
func TestUpdateAssociativeLists(t *testing.T) {
59+
tests := map[string]TestCase{
60+
"removing_obsolete_applied_structs": {
61+
Ops: []Operation{
62+
Apply{
63+
Manager: "default",
64+
Object: `
65+
list:
66+
- name: a
67+
value: 1
68+
`,
69+
APIVersion: "v1",
70+
},
71+
Apply{
72+
Manager: "default",
73+
Object: `
74+
list:
75+
- name: b
76+
value: 2
77+
`,
78+
APIVersion: "v1",
79+
},
80+
},
81+
Object: `
82+
list:
83+
- name: b
84+
value: 2
85+
`,
86+
Managed: fieldpath.ManagedFields{
87+
"default": &fieldpath.VersionedSet{
88+
Set: _NS(
89+
_P("list", _KBF("name", _SV("b")), "name"),
90+
_P("list", _KBF("name", _SV("b")), "value"),
91+
),
92+
APIVersion: "v1",
93+
},
94+
},
95+
},
96+
}
97+
98+
for name, test := range tests {
99+
t.Run(name, func(t *testing.T) {
100+
if err := test.Test(associativeListParser); err != nil {
101+
t.Fatal(err)
102+
}
103+
})
104+
}
105+
}

0 commit comments

Comments
 (0)