Skip to content

Commit

Permalink
Merge pull request #250 from apelisse/update-pathelement-map-values
Browse files Browse the repository at this point in the history
fieldpath: Allow map to be updated
  • Loading branch information
k8s-ci-robot authored Oct 6, 2023
2 parents 52ca48f + af4df06 commit 66711bf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions fieldpath/pathelementmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{})
Expand Down
7 changes: 7 additions & 0 deletions fieldpath/pathelementmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 66711bf

Please sign in to comment.