Skip to content

Commit 811c30e

Browse files
authored
Merge pull request #20 from inteon/bugfix_copy_array
BUGFIX: WithProperty and WithIndex alter sibling paths
2 parents fe06e7e + 964746f commit 811c30e

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

paths/path.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,15 @@ func Parse(pathString string) (Path, error) {
108108
}
109109

110110
func (p Path) WithProperty(part string) Path {
111-
return append(p, mapPathComponent(part))
111+
nePath := append(Path{}, p...)
112+
nePath = append(nePath, mapPathComponent(part))
113+
return nePath
112114
}
113115

114116
func (p Path) WithIndex(idx int) Path {
115-
return append(p, arrayPathComponent(idx))
117+
nePath := append(Path{}, p...)
118+
nePath = append(nePath, arrayPathComponent(idx))
119+
return nePath
116120
}
117121

118122
func (p Path) Property() pathComponent {

paths/path_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,33 @@ func TestParsePath(t *testing.T) {
7373
})
7474
}
7575
}
76+
77+
func TestWithProperty(t *testing.T) {
78+
rootPath := Path{}.WithProperty("foo").WithProperty("bar").WithProperty("aaaa")
79+
80+
path1 := rootPath.WithProperty("baz1")
81+
path2 := rootPath.WithProperty("baz2")
82+
83+
if path1.String() != "foo.bar.aaaa.baz1" {
84+
t.Errorf("path1.String() = %v, expected %v", path1.String(), "foo.bar.aaaa.baz1")
85+
}
86+
87+
if path2.String() != "foo.bar.aaaa.baz2" {
88+
t.Errorf("path2.String() = %v, expected %v", path2.String(), "foo.bar.aaaa.baz2")
89+
}
90+
}
91+
92+
func TestWithIndex(t *testing.T) {
93+
rootPath := Path{}.WithProperty("foo").WithProperty("bar").WithProperty("aaaa")
94+
95+
path1 := rootPath.WithIndex(0)
96+
path2 := rootPath.WithIndex(1)
97+
98+
if path1.String() != "foo.bar.aaaa[0]" {
99+
t.Errorf("path1.String() = %v, expected %v", path1.String(), "foo.bar.aaaa[0]")
100+
}
101+
102+
if path2.String() != "foo.bar.aaaa[1]" {
103+
t.Errorf("path2.String() = %v, expected %v", path2.String(), "foo.bar.aaaa[1]")
104+
}
105+
}

0 commit comments

Comments
 (0)