Skip to content

Commit

Permalink
Add in-place option to shift transform. Closes #50
Browse files Browse the repository at this point in the history
* Add in-place option to shift transform

* Add shift in-place to readme

* Fix vestigial change
  • Loading branch information
ryanleary authored Aug 25, 2017
1 parent 2e12382 commit ea00ddb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
{
Expand Down
7 changes: 6 additions & 1 deletion transform/shift.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ".")
Expand Down
18 changes: 18 additions & 0 deletions transform/shift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}}]}`
Expand Down
1 change: 1 addition & 0 deletions transform/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("([^\\[\\]]+)\\[(.*?)\\]")
Expand Down

0 comments on commit ea00ddb

Please sign in to comment.