Skip to content

Commit

Permalink
Merge pull request #38 from JoshKCarroll/wildcard-fix
Browse files Browse the repository at this point in the history
Fix wildcard on empty array bug
  • Loading branch information
JoshuaC215 authored Jun 14, 2017
2 parents 0140453 + c46f198 commit aa42e91
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
4 changes: 3 additions & 1 deletion kazaam.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ func (k *Kazaam) TransformInPlace(data []byte) ([]byte, error) {
buffer.Write(transformedDataList[i])
buffer.WriteByte(',')
}
buffer.Write(transformedDataList[len(transformedDataList)-1])
if len(transformedDataList) > 0 {
buffer.Write(transformedDataList[len(transformedDataList)-1])
}
buffer.WriteByte(']')
data, err = jsonparser.Set(data, buffer.Bytes(), strings.Split(*specObj.Over, ".")...)
if err != nil {
Expand Down
16 changes: 16 additions & 0 deletions transform/shift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ func TestShiftWithWildcard(t *testing.T) {
}
}

func TestShiftWithWildcardEmptySlice(t *testing.T) {
spec := `{"outputArray": "docs[*].data.key"}`
jsonIn := `{"docs": []}`
jsonOut := `{"outputArray":[]}`

cfg := getConfig(spec, false)
kazaamOut, _ := getTransformTestWrapper(Shift, cfg, jsonIn)

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

func TestShiftWithMissingKey(t *testing.T) {
spec := `{"Rating": "rating.primary.missing_value","example.old": "rating.example"}`
jsonOut := `{"Rating":null,"example":{"old":{"value":3}}}`
Expand Down
4 changes: 3 additions & 1 deletion transform/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ func getJSONRaw(data []byte, path string, pathRequired bool) ([]byte, error) {
buffer.Write(results[i])
buffer.WriteByte(',')
}
buffer.Write(results[len(results)-1])
if len(results) > 0 {
buffer.Write(results[len(results)-1])
}
buffer.WriteByte(']')
return buffer.Bytes(), nil
}
Expand Down

0 comments on commit aa42e91

Please sign in to comment.