-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial setup for delete transform * Finish Delete transform and add more tests
- Loading branch information
1 parent
e7eee7f
commit 8e6c80a
Showing
7 changed files
with
281 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package transform | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// Delete deletes keys in-place from the provided data if they exist | ||
// keys are specified in an array under "keys" in the spec. | ||
func Delete(spec *Config, data []byte) ([]byte, error) { | ||
paths, pathsOk := (*spec.Spec)["paths"] | ||
if !pathsOk { | ||
return nil, SpecError("Unable to get paths to delete") | ||
} | ||
pathSlice, sliceOk := paths.([]interface{}) | ||
if !sliceOk { | ||
return nil, SpecError(fmt.Sprintf("paths should be a slice of strings: %v", paths)) | ||
} | ||
for _, pItem := range pathSlice { | ||
path, ok := pItem.(string) | ||
if !ok { | ||
return nil, SpecError(fmt.Sprintf("Error processing %v: path should be a string", pItem)) | ||
} | ||
|
||
var err error | ||
data, err = delJSONRaw(data, path, spec.Require) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return data, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package transform | ||
|
||
import "testing" | ||
|
||
func TestDelete(t *testing.T) { | ||
spec := `{"paths": ["rating.example"]}` | ||
jsonOut := `{"rating":{"primary":{"value":3}}}` | ||
|
||
cfg := getConfig(spec, false) | ||
kazaamOut, err := getTransformTestWrapper(Delete, cfg, testJSONInput) | ||
|
||
if err != nil { | ||
t.Error("Error in transform (simplejson).") | ||
t.Log("Error: ", err.Error()) | ||
t.FailNow() | ||
} | ||
|
||
areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) | ||
if !areEqual { | ||
t.Error("Transformed data does not match expectation.") | ||
t.Log("Expected: ", jsonOut) | ||
t.Log("Actual: ", string(kazaamOut)) | ||
t.FailNow() | ||
} | ||
} | ||
|
||
func TestDeleteSpecErrorNoPathsKey(t *testing.T) { | ||
spec := `{"pathz": ["a.path"]}` | ||
expectedErr := "Unable to get paths to delete" | ||
|
||
cfg := getConfig(spec, false) | ||
_, err := getTransformTestWrapper(Delete, cfg, testJSONInput) | ||
|
||
if err == nil { | ||
t.Error("Should have generated error for invalid paths") | ||
t.Log("Spec: ", spec) | ||
t.FailNow() | ||
} | ||
e, ok := err.(SpecError) | ||
if !ok { | ||
t.Error("Unexpected error type") | ||
t.FailNow() | ||
} | ||
|
||
if e.Error() != expectedErr { | ||
t.Error("Unexpected error details") | ||
t.Log("Expected: ", expectedErr) | ||
t.Log("Actual: ", e.Error()) | ||
t.FailNow() | ||
} | ||
} | ||
|
||
func TestDeleteSpecErrorInvalidPaths(t *testing.T) { | ||
spec := `{"paths": false}` | ||
expectedErr := "paths should be a slice of strings: false" | ||
|
||
cfg := getConfig(spec, false) | ||
_, err := getTransformTestWrapper(Delete, cfg, testJSONInput) | ||
|
||
if err == nil { | ||
t.Error("Should have generated error for invalid paths") | ||
t.Log("Spec: ", spec) | ||
t.FailNow() | ||
} | ||
e, ok := err.(SpecError) | ||
if !ok { | ||
t.Error("Unexpected error type") | ||
t.FailNow() | ||
} | ||
|
||
if e.Error() != expectedErr { | ||
t.Error("Unexpected error details") | ||
t.Log("Expected: ", expectedErr) | ||
t.Log("Actual: ", e.Error()) | ||
t.FailNow() | ||
} | ||
} | ||
|
||
func TestDeleteSpecErrorInvalidPathItem(t *testing.T) { | ||
spec := `{"paths": ["foo", 42]}` | ||
expectedErr := "Error processing 42: path should be a string" | ||
|
||
cfg := getConfig(spec, false) | ||
_, err := getTransformTestWrapper(Delete, cfg, testJSONInput) | ||
|
||
if err == nil { | ||
t.Error("Should have generated error for invalid paths") | ||
t.Log("Spec: ", spec) | ||
t.FailNow() | ||
} | ||
e, ok := err.(SpecError) | ||
if !ok { | ||
t.Error("Unexpected error type") | ||
t.FailNow() | ||
} | ||
|
||
if e.Error() != expectedErr { | ||
t.Error("Unexpected error details") | ||
t.Log("Expected: ", expectedErr) | ||
t.Log("Actual: ", e.Error()) | ||
t.FailNow() | ||
} | ||
} | ||
|
||
func TestDeleteSpecErrorWildcardNotSupported(t *testing.T) { | ||
spec := `{"paths": ["ratings[*].value"]}` | ||
jsonIn := `{"ratings: [{"value": 3, "user": "rick"}, {"value": 7, "user": "jerry"}]}` | ||
expectedErr := "Array wildcard not supported for this operation." | ||
|
||
cfg := getConfig(spec, false) | ||
_, err := getTransformTestWrapper(Delete, cfg, jsonIn) | ||
|
||
if err == nil { | ||
t.Error("Should have generated error for invalid paths") | ||
t.Log("Spec: ", spec) | ||
t.FailNow() | ||
} | ||
e, ok := err.(SpecError) | ||
if !ok { | ||
t.Error("Unexpected error type") | ||
t.FailNow() | ||
} | ||
|
||
if e.Error() != expectedErr { | ||
t.Error("Unexpected error details") | ||
t.Log("Expected: ", expectedErr) | ||
t.Log("Actual: ", e.Error()) | ||
t.FailNow() | ||
} | ||
} | ||
|
||
func TestDeleteWithRequire(t *testing.T) { | ||
spec := `{"paths": ["rating.examplez"]}` | ||
|
||
cfg := getConfig(spec, true) | ||
_, err := getTransformTestWrapper(Delete, cfg, testJSONInput) | ||
|
||
if err == nil { | ||
t.Error("Should have generated error for invalid paths") | ||
t.Log("Spec: ", spec) | ||
t.FailNow() | ||
} | ||
_, ok := err.(RequireError) | ||
if !ok { | ||
t.Error("Unexpected error type") | ||
t.Error(err.Error()) | ||
t.FailNow() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters