Skip to content

Commit

Permalink
Merge pull request #15 from ryanleary/14-add-extract
Browse files Browse the repository at this point in the history
Add extract feature
  • Loading branch information
ryanleary authored Nov 25, 2016
2 parents b5d2666 + bd640e4 commit 52bdc72
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
10 changes: 10 additions & 0 deletions kazaam.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func init() {
validSpecTypes = map[string]transformFunc{
"pass": transformPass,
"shift": transformShift,
"extract": transformExtract,
"default": transformDefault,
}
}
Expand Down Expand Up @@ -175,6 +176,15 @@ func transformDefault(spec *spec, data *simplejson.Json) (*simplejson.Json, erro
return data, nil
}

func transformExtract(spec *spec, data *simplejson.Json) (*simplejson.Json, error) {
outPath, ok := (*spec.Spec)["path"]
if !ok {
return nil, fmt.Errorf("Unable to get path")
}
outData, err := getJSONPath(data, outPath.(string))
return outData, err
}

func transformShift(spec *spec, data *simplejson.Json) (*simplejson.Json, error) {
outData := simplejson.New()
for k, v := range *spec.Spec {
Expand Down
34 changes: 34 additions & 0 deletions kazaam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,37 @@ func TestKazaamShiftNullSpecValue(t *testing.T) {
t.FailNow()
}
}

func TestKazaamExtractTransform(t *testing.T) {
spec := `[{"operation": "extract", "spec": {"path": "_source"}}]`
jsonIn := `{"data": {"id": true}, "_source": {"a": 123, "b": "str", "c": null, "d": true}}`

jsonOut := `{"a":123,"b":"str","c":null,"d":true}`

kazaamTransform, _ := kazaam.NewKazaam(spec)
kazaamOut, _ := kazaamTransform.TransformJSONStringToString(jsonIn)

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

func TestKazaamExtractTransformBadPath(t *testing.T) {
spec := `[{"operation": "extract", "spec": {"path": "test"}}]`
jsonIn := `{"data": {"id": true}, "_source": {"a": 123, "b": "str", "c": null, "d": true}}`

jsonOut := "null"

kazaamTransform, _ := kazaam.NewKazaam(spec)
kazaamOut, _ := kazaamTransform.TransformJSONStringToString(jsonIn)

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

0 comments on commit 52bdc72

Please sign in to comment.