diff --git a/README.md b/README.md index 996cf96..2b53bd5 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![Coverage Status](https://coveralls.io/repos/github/qntfy/kazaam/badge.svg?branch=master)](https://coveralls.io/github/qntfy/kazaam?branch=master) [![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE) [![GitHub release](https://img.shields.io/github/release/qntfy/kazaam.svg?maxAge=3600)](https://github.com/qntfy/kazaam/releases/latest) -[![Go Report Card](https://goreportcard.com/badge/github.com/qntfy/kazaam)](https://goreportcard.com/report/github.com/qntfy/kazaam) +[![Go Report Card](https://goreportcard.com/badge/github.com/qntfy/kazaam)](https://goreportcard.com/report/github.com/qntfy/kazaam) [![GoDoc](https://godoc.org/github.com/qntfy/kazaam?status.svg)](http://godoc.org/gopkg.in/qntfy/kazaam.v3) ## Description @@ -93,6 +93,9 @@ The shift transform also supports a `"require"` field. When set to `true`, Kazaam will throw an error if *any* of the paths in the source JSON are not present. +Finally, shift by default is destructive. For in-place operation, an optional `"inplace"` +field may be set. + ### Concat The concat transform allows the combination of fields and literal strings into a single string value. ```javascript @@ -284,8 +287,8 @@ would result in } ``` -For UUIDv3 & UUIDV5 are a bit more complex. These require a Name Space which is a valid UUID already, and a set of paths, which generate UUID's based on the value of that path. If that path doesn't exist in the incoming document, a default field will be used instead. **Note** both of these fields must be strings. -**Additionally** you can use the 4 predefined namespaces such as `DNS`, `URL`, `OID`, & `X500` in the name space field otherwise pass your own UUID. +For UUIDv3 & UUIDV5 are a bit more complex. These require a Name Space which is a valid UUID already, and a set of paths, which generate UUID's based on the value of that path. If that path doesn't exist in the incoming document, a default field will be used instead. **Note** both of these fields must be strings. +**Additionally** you can use the 4 predefined namespaces such as `DNS`, `URL`, `OID`, & `X500` in the name space field otherwise pass your own UUID. ```javascript { diff --git a/transform/shift.go b/transform/shift.go index 74fd550..74916ce 100644 --- a/transform/shift.go +++ b/transform/shift.go @@ -9,7 +9,12 @@ import ( // Shift moves values from one provided json path to another in raw []byte. func Shift(spec *Config, data []byte) ([]byte, error) { - outData := []byte(`{}`) + var outData []byte + if spec.InPlace { + outData = data + } else { + outData = []byte(`{}`) + } for k, v := range *spec.Spec { array := true outPath := strings.Split(k, ".") diff --git a/transform/shift_test.go b/transform/shift_test.go index 27d99b2..9f1e73c 100644 --- a/transform/shift_test.go +++ b/transform/shift_test.go @@ -18,6 +18,24 @@ func TestShift(t *testing.T) { } } +func TestShiftInPlace(t *testing.T) { + jsonOut := `{"rating":{"example":{"value":3},"primary":{"value":3}},"Rating":3,"example":{"old":{"value":3}}}` + spec := `{"Rating": "rating.primary.value", "example.old": "rating.example"}` + + cfg := getConfig(spec, false) + cfg.InPlace = true + kazaamOut, _ := getTransformTestWrapper(Shift, cfg, testJSONInput) + areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut)) + + if !areEqual { + t.Error("Transformed data does not match expectation.") + t.Log("Input: ", testJSONInput) + t.Log("Expected: ", jsonOut) + t.Log("Actual: ", string(kazaamOut)) + t.FailNow() + } +} + func TestShiftWithWildcard(t *testing.T) { spec := `{"outputArray": "docs[*].data.key"}` jsonIn := `{"docs": [{"data": {"key": "val1"}},{"data": {"key": "val2"}}]}` diff --git a/transform/util.go b/transform/util.go index 9915aae..ee14f47 100644 --- a/transform/util.go +++ b/transform/util.go @@ -37,6 +37,7 @@ func (s SpecError) Error() string { type Config struct { Spec *map[string]interface{} `json:"spec"` Require bool `json:"require,omitempty"` + InPlace bool `json:"inplace,omitempty"` } var jsonPathRe = regexp.MustCompile("([^\\[\\]]+)\\[(.*?)\\]")