Skip to content

Commit

Permalink
Fix array wildcarding when path required
Browse files Browse the repository at this point in the history
  • Loading branch information
chilland committed Feb 24, 2017
1 parent c12da03 commit 3dd5fc1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
7 changes: 3 additions & 4 deletions kazaam.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,12 @@ func getJSONPath(j *simplejson.Json, path string, pathRequired bool) (*simplejso
if arrayKeyStr == "*" {
// get the array
if pathRequired == true {
jin, exists := jin.CheckGet(objKey)
_, exists := jin.CheckGet(objKey)
if exists != true {
return jin, &KazaamError{ErrMsg: fmt.Sprintf("Path does not exist"), ErrType: RequireError}
return nil, &KazaamError{ErrMsg: fmt.Sprintf("Path does not exist"), ErrType: RequireError}
}
} else {
jin = jin.Get(objKey)
}
jin = jin.Get(objKey)
arrayLength := len(jin.MustArray())
// construct the remainder of the jsonPath
newPath := strings.Join(objectKeys[element+1:], ".")
Expand Down
16 changes: 16 additions & 0 deletions kazaam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,22 @@ func TestKazaamShiftTransformMissingKey(t *testing.T) {
}
}

func TestKazaamDeepShiftExistsRequire(t *testing.T) {
testJSONInput := `{"rating":{"example":[{"array":[{"value":3}]},{"another":"object"}]}}`
spec := `[{"operation": "shift", "spec": {"example_res":"rating.example[0].array[*].value"},"require": true}]`
jsonOut := `{"example_res":[3]}`

transform, _ := kazaam.NewKazaam(spec)
kazaamOut, _ := transform.TransformJSONStringToString(testJSONInput)

if kazaamOut != jsonOut {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
t.FailNow()
}
}

func TestKazaamShiftTransformRequireShallowPath(t *testing.T) {
spec := `[{"operation": "shift","spec": {"Rating": "not_a_field"},"require": true}]`

Expand Down

0 comments on commit 3dd5fc1

Please sign in to comment.