Skip to content

Commit 6a0452e

Browse files
authored
fixed bug when node id contains : (#116)
* fix list determination, do not look for `:` in element node id * Added test, also extended previous to make sure that `GetDataPoint` also works properly
1 parent 38e5c1a commit 6a0452e

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

execute.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,14 +502,21 @@ func executorFindInsertionPoints(resultLock *sync.Mutex, targetPoints []string,
502502
return oldBranch, nil
503503
}
504504

505+
func isListElement(path string) bool {
506+
if hashLocation:=strings.Index(path,"#"); hashLocation>0 {
507+
path = path[:hashLocation]
508+
}
509+
return strings.Contains(path, ":")
510+
}
511+
505512
func executorExtractValue(source map[string]interface{}, resultLock *sync.Mutex, path []string) (interface{}, error) {
506513
// a pointer to the objects we are modifying
507514
var recent interface{} = source
508515
log.Debug("Pulling ", path, " from ", source)
509516

510517
for i, point := range path[:] {
511518
// if the point designates an element in the list
512-
if strings.Contains(point, ":") {
519+
if isListElement(point) {
513520
pointData, err := executorGetPointData(point)
514521
if err != nil {
515522
return nil, err

execute_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,6 +1942,8 @@ func TestExecutorGetPointData(t *testing.T) {
19421942
{"foo:2", &extractorPointData{Field: "foo", Index: 2, ID: ""}},
19431943
{"foo#3", &extractorPointData{Field: "foo", Index: -1, ID: "3"}},
19441944
{"foo:2#3", &extractorPointData{Field: "foo", Index: 2, ID: "3"}},
1945+
{"foo#Thing:1337", &extractorPointData{Field: "foo", Index: -1, ID: "Thing:1337"}},
1946+
{"foo:2#Thing:1337", &extractorPointData{Field: "foo", Index: 2, ID: "Thing:1337"}},
19451947
}
19461948

19471949
for _, row := range table {
@@ -2074,3 +2076,22 @@ func TestFindInsertionPoint_stitchIntoObject(t *testing.T) {
20742076
func TestFindInsertionPoint_handlesNullObjects(t *testing.T) {
20752077
t.Skip("Not yet implemented")
20762078
}
2079+
2080+
func TestSingleObjectWithColonInID(t *testing.T) {
2081+
var source = make(map[string]interface{})
2082+
_ = json.Unmarshal([]byte(
2083+
// language=JSON
2084+
`{"hello": {"id": "Thing:1337", "firstName": "Foo", "lastName": "bar"}}`),
2085+
&source,
2086+
)
2087+
2088+
value, err := executorExtractValue(source, &sync.Mutex{}, []string{"hello#Thing:1337"})
2089+
if err != nil {
2090+
t.Error(err.Error())
2091+
return
2092+
}
2093+
2094+
assert.Equal(t, map[string]interface{}{
2095+
"id": "Thing:1337", "firstName": "Foo", "lastName": "bar",
2096+
}, value)
2097+
}

0 commit comments

Comments
 (0)