Skip to content

Commit

Permalink
Merge pull request #30 from ryanleary/fix-transform-in-place
Browse files Browse the repository at this point in the history
Add Transform and TransformInPlace
  • Loading branch information
chilland committed Feb 25, 2017
2 parents 02112f3 + 5596db3 commit dc02009
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
22 changes: 16 additions & 6 deletions kazaam.go
Expand Up @@ -167,11 +167,20 @@ func transformErrorType(err error) error {

// Transform takes the *simplejson.Json `data`, transforms it according
// to the loaded spec, and returns the modified *simplejson.Json object.
func (k *Kazaam) Transform(data *simplejson.Json) (*simplejson.Json, error) {
d := simplejson.New()
d.SetPath(nil, data.Interface())
err := k.TransformInPlace(d)
return d, err
}

// TransformInPlace takes the *simplejson.Json `data`, transforms it according
// to the loaded spec, and modifies the *simplejson.Json object.
//
// Note: this is a destructive operation: the transformation is done in place.
// You must perform a deep copy of the data prior to calling Transform if
// the original JSON object must be retained.
func (k *Kazaam) Transform(data *simplejson.Json) (*simplejson.Json, error) {
func (k *Kazaam) TransformInPlace(data *simplejson.Json) error {
var err error
for _, specObj := range k.specJSON {
//spec := specObj.Get("spec")
Expand All @@ -185,7 +194,7 @@ func (k *Kazaam) Transform(data *simplejson.Json) (*simplejson.Json, error) {
jsonValue.SetPath(nil, x)
intErr := k.getTransform(&specObj)(specObj.Config, jsonValue)
if intErr != nil {
return nil, transformErrorType(err)
return transformErrorType(err)
}
transformedDataList = append(transformedDataList, jsonValue.Interface())
}
Expand All @@ -196,7 +205,7 @@ func (k *Kazaam) Transform(data *simplejson.Json) (*simplejson.Json, error) {
err = transformErrorType(err)
}
}
return data, err
return err
}

// TransformJSONStringToString loads the JSON string `data`, transforms
Expand All @@ -207,11 +216,11 @@ func (k *Kazaam) TransformJSONStringToString(data string) (string, error) {
if err != nil {
return "", err
}
outputJSON, err := k.Transform(d)
err = k.TransformInPlace(d)
if err != nil {
return "", err
}
jsonString, err := outputJSON.MarshalJSON()
jsonString, err := d.MarshalJSON()
if err != nil {
return "", err
}
Expand All @@ -229,5 +238,6 @@ func (k *Kazaam) TransformJSONString(data string) (*simplejson.Json, error) {
if err != nil {
return nil, err
}
return k.Transform(d)
k.TransformInPlace(d)
return d, nil
}
33 changes: 21 additions & 12 deletions kazaam_int_test.go
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/qntfy/kazaam/transform"
)

const testJSONInput = `{"rating": {"primary": {"value": 3}, "example": {"value": 3}}}`
const testJSONInput = `{"rating":{"example":{"value":3},"primary":{"value":3}}}`

func TestKazaamBadInput(t *testing.T) {
jsonOut := ``
Expand Down Expand Up @@ -242,18 +242,27 @@ func TestMissingRequiredField(t *testing.T) {
}
}

func TestNullRequiredField(t *testing.T) {
jsonIn := `{"meta": {"image_cache": null}, "doc": "example"}`
spec := `[
{"operation": "shift", "spec": {"results": "meta.image_cache[0].results[*]"}, "require": true}
]`

kazaamTransform, _ := kazaam.NewKazaam(spec)
k, err := kazaamTransform.TransformJSONStringToString(jsonIn)
func TestKazaamNoModify(t *testing.T) {
spec := `[{"operation": "shift","spec": {"Rating": "rating.primary.value","example.old": "rating.example"}}]`
msgOut := `{"Rating":3,"example":{"old":{"value":3}}}`
tf, _ := kazaam.NewKazaam(spec)
data, _ := simplejson.NewJson([]byte(testJSONInput))
k, _ := tf.Transform(data)

jsonOut, _ := k.MarshalJSON()
jsonOutStr := string(jsonOut)

if jsonOutStr != msgOut || jsonOutStr == testJSONInput {
t.Error("Unexpected transformation result")
t.Error("Actual:", jsonOutStr)
t.Error("Expected:", msgOut)
}

if err == nil {
t.Error("Should have generated error for null image_cache value")
t.Error(k)
b, _ := data.MarshalJSON()
if string(b) != testJSONInput {
t.Error("Unexpected modification")
t.Error("Actual:", string(b))
t.Error("Expected:", testJSONInput)
}
}

Expand Down
7 changes: 4 additions & 3 deletions transform/util.go
Expand Up @@ -54,9 +54,10 @@ func getJSONPath(j *simplejson.Json, path string, pathRequired bool) (*simplejso
// get the array
var exists bool
jin, exists = jin.CheckGet(objKey)
if pathRequired && !exists {
return nil, RequireError("Path does not exist")
//return nil, &Error{ErrMsg: fmt.Sprintf("Path does not exist"), ErrType: RequireError}
if !exists {
if pathRequired {
return jin, RequireError("Path does not exist")
}
}
arrayLength := len(jin.MustArray())
// construct the remainder of the jsonPath
Expand Down

0 comments on commit dc02009

Please sign in to comment.