Skip to content

Commit 66711bf

Browse files
authored
Merge pull request #250 from apelisse/update-pathelement-map-values
fieldpath: Allow map to be updated
2 parents 52ca48f + af4df06 commit 66711bf

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

Diff for: fieldpath/pathelementmap.go

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func (spev sortedPathElementValues) Less(i, j int) bool {
5353
func (spev sortedPathElementValues) Swap(i, j int) { spev[i], spev[j] = spev[j], spev[i] }
5454

5555
// Insert adds the pathelement and associated value in the map.
56+
// If insert is called twice with the same PathElement, the value is replaced.
5657
func (s *PathElementValueMap) Insert(pe PathElement, v value.Value) {
5758
loc := sort.Search(len(s.members), func(i int) bool {
5859
return !s.members[i].PathElement.Less(pe)
@@ -62,6 +63,7 @@ func (s *PathElementValueMap) Insert(pe PathElement, v value.Value) {
6263
return
6364
}
6465
if s.members[loc].PathElement.Equals(pe) {
66+
s.members[loc].Value = v
6567
return
6668
}
6769
s.members = append(s.members, pathElementValue{})

Diff for: fieldpath/pathelementmap_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,11 @@ func TestPathElementValueMap(t *testing.T) {
4747
} else if !value.Equals(val, value.NewValueInterface(2)) {
4848
t.Fatalf("Unexpected value found: %#v", val)
4949
}
50+
51+
m.Insert(PathElement{FieldName: strptr("carrot")}, value.NewValueInterface("fork"))
52+
if val, ok := m.Get(PathElement{FieldName: strptr("carrot")}); !ok {
53+
t.Fatal("Missing path-element in map")
54+
} else if !value.Equals(val, value.NewValueInterface("fork")) {
55+
t.Fatalf("Unexpected value found: %#v", val)
56+
}
5057
}

0 commit comments

Comments
 (0)