Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/float converter #95

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3151198
Adding JSON Path Parameters, Conditional Expressions and Converters
Jan 1, 2019
79f7072
Merge pull request #1 from mbordner/feature/value-conditional-and-con…
mbordner Jan 3, 2019
65c3bec
updating internal package paths to point to forked repos
Jan 3, 2019
e8a41f7
Merge pull request #2 from mbordner/feature/updating-imports-for-prod…
mbordner Jan 3, 2019
6a3b2d2
regex was not working on the actual string value
mbordner Jan 22, 2019
9332c3c
Merge pull request #3 from mbordner/feature/bug-fix-regex
mbordner Jan 22, 2019
bb64bb6
adding in a few new converters, and a new transform
mbordner Jan 23, 2019
c8785a4
Merge pull request #4 from mbordner/feature/len-split-ordered-shift
mbordner Jan 23, 2019
a25ba10
bug fix for numbers with leading zeros
mbordner Jan 23, 2019
6ff1129
Merge pull request #5 from mbordner/feature/buf-fix-ston
mbordner Jan 23, 2019
aa53e96
added not converter
mbordner Jan 23, 2019
7b09ed5
Merge pull request #6 from mbordner/feature/not-converter
mbordner Jan 23, 2019
2649db1
adding ability to define the regex config as an array of configs
mbordner Mar 26, 2019
78aa110
Merge pull request #7 from mbordner/feature/regex-array-of-config-sup…
mbordner Mar 26, 2019
456c681
bug fix
mbordner Mar 26, 2019
94b9d29
Merge pull request #8 from mbordner/feature/regex-array-of-config-sup…
mbordner Mar 26, 2019
f41e5a9
adding merge transform
mbordner May 23, 2019
061e4de
Merge pull request #9 from mbordner/feature/merge-transform
mbordner May 23, 2019
4e207ad
adding split and join converters
mbordner Jun 5, 2019
65152a4
Merge pull request #10 from mbordner/feature/join-split-converters
mbordner Jun 5, 2019
a5130aa
fixing bug with array paths and conditional params
mbordner Jun 5, 2019
c88735b
Merge pull request #11 from mbordner/feature/bug-fix-array-conditiona…
mbordner Jun 5, 2019
096461e
Merge branch 'master' into feature/bug-fix-array-conditional-paths
mbordner Jun 5, 2019
69c8c33
bug fix on conditional arrays
mbordner Jun 5, 2019
1da974e
Merge pull request #12 from mbordner/feature/bug-fix-array-conditiona…
mbordner Jun 5, 2019
4bd48c6
adding in support to get the length of array values
mbordner Jun 6, 2019
8b84c3f
Merge pull request #13 from mbordner/feature/len-to-support-arrays
mbordner Jun 6, 2019
b5cf60d
adding float converter
mbordner Jun 6, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
adding merge transform
mbordner committed May 23, 2019
commit f41e5a96d3c13a21f08432079c0191b74d48b53a
72 changes: 71 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ API Documentation is available at http://godoc.org/gopkg.in/qntfy/kazaam.v3.

## Features
Kazaam is primarily designed to be used as a library for transforming arbitrary JSON.
It ships with ten built-in transform types, and nineteen built-in converter types,
It ships with eleven built-in transform types, and nineteen built-in converter types,
described below, which provide significant flexibility in reshaping JSON data.

Also included when you `go get` Kazaam, is a binary implementation, `kazaam` that can be used for
@@ -54,6 +54,7 @@ Kazaam currently supports the following built-in transforms:
- default
- pass
- delete
- merge

### Shift
The shift transform is the current Kazaam workhorse used for remapping of fields. It supports a `"require"` field that when
@@ -521,6 +522,75 @@ would result in
A pass transform, as the name implies, passes the input data unchanged to the output. This is used internally
when a null transform spec is specified, but may also be useful for testing.

### Merge
A merge transform will take multiple arrays and join them in to an array of objects joining them by keys. The arrays should be equal length.

```json
{
"operation": "merge",
"spec": {
"merge1": [
{
"name": "prop_1",
"array": "array_a"
},
{
"name": "prop_2",
"array": "array_b"
},
{
"name": "prop_3",
"array": "array_c"
}
]
}
}
```

executed on a json message with format:
```json
{
"array_a": [
"a_1",
"a_2",
"a_3"
],
"array_b": [
"b_1",
"b_2",
"b_3"
],
"array_c": [
"c_1",
"c_2",
"c_3"
]
}
```

would result in:
```json
{
"merge1": [
{
"prop_1": "a_1",
"prop_2": "b_1",
"prop_3": "c_1"
},
{
"prop_1": "a_2",
"prop_2": "b_2",
"prop_3": "c_2"
},
{
"prop_1": "a_3",
"prop_2": "b_3",
"prop_3": "c_3"
}
]
}
```



## Converter Specification Support
1 change: 1 addition & 0 deletions kazaam.go
Original file line number Diff line number Diff line change
@@ -41,6 +41,7 @@ func init() {
"timestamp": transform.Timestamp,
"uuid": transform.UUID,
"steps": transform.Steps,
"merge": transform.Merge,
}

defaultConverters = map[string]registry.Converter{
86 changes: 86 additions & 0 deletions transform/merge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package transform

import (
"encoding/json"
)

// Merge joins multiple array values into array of objects with property names containing their matching array. Arrays
// should be the same length.
func Merge(spec *Config, data []byte) ([]byte, error) {
var outData []byte

if spec.InPlace {
outData = data
} else {
outData = []byte(`{}`)
}

// iterate through the spec
for k, v := range *spec.Spec {

// map[prop_name] = [ values... ]
arrayVals := make(map[string][]interface{})
outVals := make([]map[string]interface{}, 0)

mergeSpec, ok := v.([]interface{})
if !ok {
return nil, SpecError("Invalid Spec for Merge")
}

l := 0

for i, v := range mergeSpec {
arraySpec := v.(map[string]interface{})
var name, array string
name, ok = arraySpec["name"].(string)
if !ok {
return nil, SpecError("Array spec missing name for Merge")
}
array, ok = arraySpec["array"].(string)
if !ok {
return nil, SpecError("Array spec missing array for Merge")
}

var dataForV []byte
var err error

dataForV, err = getJSONRaw(data, array, true)
if err != nil {
return nil, err
}

var arrayValues []interface{}
err = json.Unmarshal(dataForV, &arrayValues)

arrayVals[name] = arrayValues
if i == 0 {
l = len(arrayValues)
} else if l != len(arrayValues) {
return nil, SpecError("Arrays must be the same length for Merge")
}
}

for i := 0; i < l; i++ {
m := make(map[string]interface{})
for k, v := range arrayVals {
m[k] = v[0]
arrayVals[k] = v[1:]
}
outVals = append(outVals, m)
}

dataForV, err := json.Marshal(outVals)
if err != nil {
return nil, err
}

outData, err = setJSONRaw(outData, dataForV, k)
if err != nil {
return nil, err
}

}

return outData, nil

}
37 changes: 37 additions & 0 deletions transform/merge_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package transform

import "testing"

func TestMerge(t *testing.T) {
spec := `{"merge1":[{"name":"prop_1","array":"array_a"},{"name":"prop_2","array":"array_b"},{"name":"prop_3","array":"array_c"}]}`
jsonIn := `{"array_a":["a_1","a_2","a_3"],"array_b":["b_1","b_2","b_3"],"array_c":["c_1","c_2","c_3"]}`
jsonOut := `{"merge1":[{"prop_1":"a_1","prop_2":"b_1","prop_3":"c_1"},{"prop_1":"a_2","prop_2":"b_2","prop_3":"c_2"},{"prop_1":"a_3","prop_2":"b_3","prop_3":"c_3"}]}`

cfg := getConfig(spec, false)
kazaamOut, _ := getTransformTestWrapper(Merge, cfg, jsonIn)
areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut))

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

func TestMergeSingleArray(t *testing.T) {
spec := `{"merge1":[{"name":"prop_1","array":"array_a"}]}`
jsonIn := `{"array_a":["a_1","a_2","a_3"]}`
jsonOut := `{"merge1":[{"prop_1":"a_1"},{"prop_1":"a_2"},{"prop_1":"a_3"}]}`

cfg := getConfig(spec, false)
kazaamOut, _ := getTransformTestWrapper(Merge, cfg, jsonIn)
areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut))

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