Skip to content

Commit

Permalink
Merge pull request #20 from inteon/bugfix_copy_array
Browse files Browse the repository at this point in the history
BUGFIX: WithProperty and WithIndex alter sibling paths
  • Loading branch information
jetstack-bot authored Jan 26, 2024
2 parents fe06e7e + 964746f commit 811c30e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
8 changes: 6 additions & 2 deletions paths/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,15 @@ func Parse(pathString string) (Path, error) {
}

func (p Path) WithProperty(part string) Path {
return append(p, mapPathComponent(part))
nePath := append(Path{}, p...)
nePath = append(nePath, mapPathComponent(part))
return nePath
}

func (p Path) WithIndex(idx int) Path {
return append(p, arrayPathComponent(idx))
nePath := append(Path{}, p...)
nePath = append(nePath, arrayPathComponent(idx))
return nePath
}

func (p Path) Property() pathComponent {
Expand Down
30 changes: 30 additions & 0 deletions paths/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,33 @@ func TestParsePath(t *testing.T) {
})
}
}

func TestWithProperty(t *testing.T) {
rootPath := Path{}.WithProperty("foo").WithProperty("bar").WithProperty("aaaa")

path1 := rootPath.WithProperty("baz1")
path2 := rootPath.WithProperty("baz2")

if path1.String() != "foo.bar.aaaa.baz1" {
t.Errorf("path1.String() = %v, expected %v", path1.String(), "foo.bar.aaaa.baz1")
}

if path2.String() != "foo.bar.aaaa.baz2" {
t.Errorf("path2.String() = %v, expected %v", path2.String(), "foo.bar.aaaa.baz2")
}
}

func TestWithIndex(t *testing.T) {
rootPath := Path{}.WithProperty("foo").WithProperty("bar").WithProperty("aaaa")

path1 := rootPath.WithIndex(0)
path2 := rootPath.WithIndex(1)

if path1.String() != "foo.bar.aaaa[0]" {
t.Errorf("path1.String() = %v, expected %v", path1.String(), "foo.bar.aaaa[0]")
}

if path2.String() != "foo.bar.aaaa[1]" {
t.Errorf("path2.String() = %v, expected %v", path2.String(), "foo.bar.aaaa[1]")
}
}

0 comments on commit 811c30e

Please sign in to comment.