diff --git a/fieldpath/pathelementmap.go b/fieldpath/pathelementmap.go index 9b14ca58..2611a228 100644 --- a/fieldpath/pathelementmap.go +++ b/fieldpath/pathelementmap.go @@ -53,6 +53,7 @@ func (spev sortedPathElementValues) Less(i, j int) bool { func (spev sortedPathElementValues) Swap(i, j int) { spev[i], spev[j] = spev[j], spev[i] } // Insert adds the pathelement and associated value in the map. +// If insert is called twice with the same PathElement, the value is replaced. func (s *PathElementValueMap) Insert(pe PathElement, v value.Value) { loc := sort.Search(len(s.members), func(i int) bool { return !s.members[i].PathElement.Less(pe) @@ -62,6 +63,7 @@ func (s *PathElementValueMap) Insert(pe PathElement, v value.Value) { return } if s.members[loc].PathElement.Equals(pe) { + s.members[loc].Value = v return } s.members = append(s.members, pathElementValue{}) diff --git a/fieldpath/pathelementmap_test.go b/fieldpath/pathelementmap_test.go index cbabf50f..5685c7ca 100644 --- a/fieldpath/pathelementmap_test.go +++ b/fieldpath/pathelementmap_test.go @@ -47,4 +47,11 @@ func TestPathElementValueMap(t *testing.T) { } else if !value.Equals(val, value.NewValueInterface(2)) { t.Fatalf("Unexpected value found: %#v", val) } + + m.Insert(PathElement{FieldName: strptr("carrot")}, value.NewValueInterface("fork")) + if val, ok := m.Get(PathElement{FieldName: strptr("carrot")}); !ok { + t.Fatal("Missing path-element in map") + } else if !value.Equals(val, value.NewValueInterface("fork")) { + t.Fatalf("Unexpected value found: %#v", val) + } }