Skip to content

Commit bef085d

Browse files
author
Antoine Pelisse
committed
Change Remove to not operate in place
1 parent e8b9d44 commit bef085d

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

typed/remove.go

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,29 @@ import (
2020
)
2121

2222
type removingWalker struct {
23-
value *value.Value
23+
value value.Value
24+
out interface{}
2425
schema *schema.Schema
2526
toRemove *fieldpath.Set
2627
}
2728

28-
func removeItemsWithSchema(value *value.Value, toRemove *fieldpath.Set, schema *schema.Schema, typeRef schema.TypeRef) {
29+
func removeItemsWithSchema(val value.Value, toRemove *fieldpath.Set, schema *schema.Schema, typeRef schema.TypeRef) value.Value {
2930
w := &removingWalker{
30-
value: value,
31+
value: val,
3132
schema: schema,
3233
toRemove: toRemove,
3334
}
34-
resolveSchema(schema, typeRef, value, w)
35+
resolveSchema(schema, typeRef, &val, w)
36+
return value.ValueInterface{Value: w.out}
3537
}
3638

37-
// doLeaf should be called on leaves before descending into children, if there
38-
// will be a descent. It modifies w.inLeaf.
39-
func (w *removingWalker) doLeaf() ValidationErrors { return nil }
40-
41-
func (w *removingWalker) doScalar(t *schema.Scalar) ValidationErrors { return nil }
39+
func (w *removingWalker) doScalar(t *schema.Scalar) ValidationErrors {
40+
w.out = w.value.Interface()
41+
return nil
42+
}
4243

4344
func (w *removingWalker) doList(t *schema.List) (errs ValidationErrors) {
44-
l := (*w.value).List()
45+
l := w.value.List()
4546

4647
// If list is null, empty, or atomic just return
4748
if l == nil || l.Length() == 0 || t.ElementRelationship == schema.Atomic {
@@ -58,23 +59,18 @@ func (w *removingWalker) doList(t *schema.List) (errs ValidationErrors) {
5859
continue
5960
}
6061
if subset := w.toRemove.WithPrefix(pe); !subset.Empty() {
61-
val := value.Value(item)
62-
removeItemsWithSchema(&val, subset, w.schema, t.ElementType)
63-
item = val
64-
62+
item = removeItemsWithSchema(item, subset, w.schema, t.ElementType)
6563
}
66-
newItems = append(newItems, item)
64+
newItems = append(newItems, item.Interface())
6765
}
6866
if len(newItems) > 0 {
69-
*w.value = value.Value(value.ValueInterface{Value: newItems})
70-
} else {
71-
*w.value = nil
67+
w.out = newItems
7268
}
7369
return nil
7470
}
7571

7672
func (w *removingWalker) doMap(t *schema.Map) ValidationErrors {
77-
m := (*w.value).Map()
73+
m := w.value.Map()
7874

7975
// If map is null, empty, or atomic just return
8076
if m == nil || m.Length() == 0 || t.ElementRelationship == schema.Atomic {
@@ -99,15 +95,13 @@ func (w *removingWalker) doMap(t *schema.Map) ValidationErrors {
9995
}
10096
}
10197
if subset := w.toRemove.WithPrefix(pe); !subset.Empty() {
102-
removeItemsWithSchema(&val, subset, w.schema, fieldType)
98+
val = removeItemsWithSchema(val, subset, w.schema, fieldType)
10399
}
104-
newMap[k] = val
100+
newMap[k] = val.Interface()
105101
return true
106102
})
107103
if len(newMap) > 0 {
108-
*w.value = value.Value(value.ValueInterface{Value: newMap})
109-
} else {
110-
*w.value = nil
104+
w.out = newMap
111105
}
112106
return nil
113107
}

typed/typed.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ func (tv TypedValue) Compare(rhs *TypedValue) (c *Comparison, err error) {
140140

141141
// RemoveItems removes each provided list or map item from the value.
142142
func (tv TypedValue) RemoveItems(items *fieldpath.Set) *TypedValue {
143-
tv.value = tv.value.Copy()
144-
removeItemsWithSchema(&tv.value, items, tv.schema, tv.typeRef)
143+
tv.value = removeItemsWithSchema(tv.value, items, tv.schema, tv.typeRef)
145144
return &tv
146145
}
147146

0 commit comments

Comments
 (0)