Skip to content

Commit 68fbb4f

Browse files
author
jennybuckley
committed
Revert "Fix bug with removing non leaf items"
1 parent 8c17deb commit 68fbb4f

File tree

5 files changed

+29
-38
lines changed

5 files changed

+29
-38
lines changed

fieldpath/set.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,7 @@ 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-
subset = NewSet()
163-
}
164-
if s.Members.Has(pe) {
165-
subset.Insert(MakePathOrDie(""))
162+
return NewSet()
166163
}
167164
return subset
168165
}

merge/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,5 +176,5 @@ func (s *Updater) removeDisownedItems(merged, applied typed.TypedValue, lastSet
176176
if err != nil {
177177
return nil, fmt.Errorf("failed to create field set from applied config in last applied version: %v", err)
178178
}
179-
return merged.RemoveItems(lastSet.Set, appliedSet), nil
179+
return merged.RemoveItems(lastSet.Set.Difference(appliedSet)), nil
180180
}

typed/deduced.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,6 @@ func modified(lhs, rhs value.Value, path fieldpath.Path, set *fieldpath.Set) {
173173

174174
// RemoveItems does nothing because all lists in a deducedTypedValue are considered atomic,
175175
// and there are no maps because it is indistinguishable from a struct.
176-
func (dv deducedTypedValue) RemoveItems(_ *fieldpath.Set, _ *fieldpath.Set) TypedValue {
176+
func (dv deducedTypedValue) RemoveItems(_ *fieldpath.Set) TypedValue {
177177
return dv
178178
}

typed/remove.go

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,16 @@ import (
2020
)
2121

2222
type removingWalker struct {
23-
value *value.Value
24-
schema *schema.Schema
25-
lhs *fieldpath.Set
26-
rhs *fieldpath.Set
23+
value *value.Value
24+
schema *schema.Schema
25+
toRemove *fieldpath.Set
2726
}
2827

29-
func removeItemsWithSchema(value *value.Value, lhs *fieldpath.Set, rhs *fieldpath.Set, schema *schema.Schema, typeRef schema.TypeRef) {
28+
func removeItemsWithSchema(value *value.Value, toRemove *fieldpath.Set, schema *schema.Schema, typeRef schema.TypeRef) {
3029
w := &removingWalker{
31-
value: value,
32-
schema: schema,
33-
lhs: lhs,
34-
rhs: rhs,
30+
value: value,
31+
schema: schema,
32+
toRemove: toRemove,
3533
}
3634
resolveSchema(schema, typeRef, w)
3735
}
@@ -58,10 +56,8 @@ func (w *removingWalker) doStruct(t schema.Struct) ValidationErrors {
5856
for i, _ := range s.Items {
5957
item := s.Items[i]
6058
pe := fieldpath.PathElement{FieldName: &item.Name}
61-
subsetLHS := w.lhs.WithPrefix(pe)
62-
subsetRHS := w.rhs.WithPrefix(pe)
63-
if !subsetLHS.Empty() {
64-
removeItemsWithSchema(&s.Items[i].Value, subsetLHS, subsetRHS, w.schema, fieldTypes[item.Name])
59+
if subset := w.toRemove.WithPrefix(pe); !subset.Empty() {
60+
removeItemsWithSchema(&s.Items[i].Value, subset, w.schema, fieldTypes[item.Name])
6561
}
6662
}
6763
return nil
@@ -80,13 +76,12 @@ func (w *removingWalker) doList(t schema.List) (errs ValidationErrors) {
8076
item := l.Items[i]
8177
// Ignore error because we have already validated this list
8278
pe, _ := listItemToPathElement(t, i, item)
83-
subsetLHS := w.lhs.WithPrefix(pe)
84-
subsetRHS := w.rhs.WithPrefix(pe)
85-
if !subsetLHS.Empty() {
86-
if subsetRHS.Empty() {
87-
continue
88-
}
89-
removeItemsWithSchema(&l.Items[i], subsetLHS, subsetRHS, w.schema, t.ElementType)
79+
path, _ := fieldpath.MakePath(pe)
80+
if w.toRemove.Has(path) {
81+
continue
82+
}
83+
if subset := w.toRemove.WithPrefix(pe); !subset.Empty() {
84+
removeItemsWithSchema(&l.Items[i], subset, w.schema, t.ElementType)
9085
}
9186
newItems = append(newItems, l.Items[i])
9287
}
@@ -106,13 +101,12 @@ func (w *removingWalker) doMap(t schema.Map) ValidationErrors {
106101
for i, _ := range m.Items {
107102
item := m.Items[i]
108103
pe := fieldpath.PathElement{FieldName: &item.Name}
109-
subsetLHS := w.lhs.WithPrefix(pe)
110-
subsetRHS := w.rhs.WithPrefix(pe)
111-
if !subsetLHS.Empty() {
112-
if subsetRHS.Empty() {
113-
continue
114-
}
115-
removeItemsWithSchema(&m.Items[i].Value, subsetLHS, subsetRHS, w.schema, t.ElementType)
104+
path, _ := fieldpath.MakePath(pe)
105+
if w.toRemove.Has(path) {
106+
continue
107+
}
108+
if subset := w.toRemove.WithPrefix(pe); !subset.Empty() {
109+
removeItemsWithSchema(&m.Items[i].Value, subset, w.schema, t.ElementType)
116110
}
117111
newMap.Set(item.Name, m.Items[i].Value)
118112
}

typed/typed.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ type TypedValue interface {
5353
// match), or an error will be returned. Validation errors will be returned if
5454
// the objects don't conform to the schema.
5555
Compare(rhs TypedValue) (c *Comparison, err error)
56-
// RemoveItems removes each list or map item from the value that has children in lhs and not in rhs.
57-
RemoveItems(lhs *fieldpath.Set, rhs *fieldpath.Set) TypedValue
56+
// RemoveItems removes each provided list or map item from the value.
57+
RemoveItems(items *fieldpath.Set) TypedValue
5858
}
5959

6060
// AsTyped accepts a value and a type and returns a TypedValue. 'v' must have
@@ -161,9 +161,9 @@ func (tv typedValue) Compare(rhs TypedValue) (c *Comparison, err error) {
161161
return c, nil
162162
}
163163

164-
// RemoveItems removes each list or map item from the value that has children in lhs and not in rhs.
165-
func (tv typedValue) RemoveItems(lhs *fieldpath.Set, rhs *fieldpath.Set) TypedValue {
166-
removeItemsWithSchema(&tv.value, lhs, rhs, tv.schema, tv.typeRef)
164+
// RemoveItems removes each provided list or map item from the value.
165+
func (tv typedValue) RemoveItems(items *fieldpath.Set) TypedValue {
166+
removeItemsWithSchema(&tv.value, items, tv.schema, tv.typeRef)
167167
return tv
168168
}
169169

0 commit comments

Comments
 (0)