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
Show file tree
Hide file tree
Changes from all commits
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
935 changes: 888 additions & 47 deletions README.md

Large diffs are not rendered by default.

67 changes: 67 additions & 0 deletions converter/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package converter

import (
"errors"
"github.com/mbordner/kazaam/transform"
"go/constant"
"go/token"
)

type Add struct {
ConverterBase
}

func (c *Add) Convert(jsonData []byte, value []byte, args []byte) (newValue []byte, err error) {

var jsonValue, argsValue *transform.JSONValue
jsonValue, err = c.NewJSONValue(value)
if err != nil {
return
}

argsValue, err = c.NewJSONValue(args)
if err != nil {
return
}

if jsonValue.IsNumber() == false || argsValue.IsString() == false {
err = errors.New("invalid value or arguments for add converter")
return
}

numStrVal := argsValue.GetStringValue()
if numStrVal[0] == '.' {
numStrVal = "0" + numStrVal
}

// convert the string to number
argsValue, err = c.NewJSONValue([]byte(numStrVal))
if err != nil {
return
}

if argsValue.IsNumber() == false {
err = errors.New("arguments should be a number for add converter")
return
}

var left, right constant.Value

if jsonValue.GetType() == transform.JSONInt {
left = constant.MakeInt64(jsonValue.GetIntValue())
} else {
left = constant.MakeFloat64(jsonValue.GetFloatValue())
}

if argsValue.GetType() == transform.JSONInt {
right = constant.MakeInt64(argsValue.GetIntValue())
} else {
right = constant.MakeFloat64(argsValue.GetFloatValue())
}

result := constant.BinaryOp(left, token.ADD, right)

newValue = []byte(result.String())

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

import (
"github.com/mbordner/kazaam/registry"
"strconv"
"testing"
)

func TestAdd_Convert(t *testing.T) {

registry.RegisterConverter("add", &Add{})
c := registry.GetConverter("add")

table := []struct {
value string
arguments string
expected string
}{
{`5`, `1`, `6`,},
{`5`, `-1`, `4`,},
}

for _, test := range table {
v, e := c.Convert(getTestData(), []byte(test.value), []byte(strconv.Quote(test.arguments)))

if e != nil {
t.Error("error running convert function")
}

if string(v) != test.expected {
t.Error("unexpected result from convert")
t.Log("Expected: {}", test.expected)
t.Log("Actual: {}", string(v))
}
}

}
39 changes: 39 additions & 0 deletions converter/ceil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package converter

import (
"errors"
"github.com/mbordner/kazaam/transform"
"go/constant"
"math"
)

type Ceil struct {
ConverterBase
}

func (c *Ceil) Convert(jsonData []byte, value []byte, args []byte) (newValue []byte, err error) {

newValue = value

var jsonValue *transform.JSONValue
jsonValue, err = c.NewJSONValue(value)
if err != nil {
return
}
if jsonValue.IsNumber() == false {
err = errors.New("invalid value for ceil converter")
return
}

if jsonValue.GetType() == transform.JSONInt {
return
}

val := jsonValue.GetFloatValue()

val = math.Ceil(val)

newValue = []byte(constant.MakeInt64(int64(val)).String())

return
}
36 changes: 36 additions & 0 deletions converter/ceil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package converter

import (
"github.com/mbordner/kazaam/registry"
"strconv"
"testing"
)

func TestCeil_Convert(t *testing.T) {
registry.RegisterConverter("ceil", &Ceil{})
c := registry.GetConverter("ceil")

table := []struct {
value string
arguments string
expected string
}{
{`5.1`, ``, `6`,},
{`5.6`, ``, `6`,},
{`0.01`, ``, `1`},
}

for _, test := range table {
v, e := c.Convert(getTestData(), []byte(test.value), []byte(strconv.Quote(test.arguments)))

if e != nil {
t.Error("error running convert function")
}

if string(v) != test.expected {
t.Error("unexpected result from convert")
t.Log("Expected: {}", test.expected)
t.Log("Actual: {}", string(v))
}
}
}
67 changes: 67 additions & 0 deletions converter/div.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package converter

import (
"errors"
"github.com/mbordner/kazaam/transform"
"go/constant"
"go/token"
)

type Div struct {
ConverterBase
}

func (c *Div) Convert(jsonData []byte, value []byte, args []byte) (newValue []byte, err error) {

var jsonValue, argsValue *transform.JSONValue
jsonValue, err = c.NewJSONValue(value)
if err != nil {
return
}

argsValue, err = c.NewJSONValue(args)
if err != nil {
return
}

if jsonValue.IsNumber() == false || argsValue.IsString() == false {
err = errors.New("invalid value or arguments for div converter")
return
}

numStrVal := argsValue.GetStringValue()
if numStrVal[0] == '.' {
numStrVal = "0" + numStrVal
}

// convert the string to number
argsValue, err = c.NewJSONValue([]byte(numStrVal))
if err != nil {
return
}

if argsValue.IsNumber() == false {
err = errors.New("arguments should be a number for div converter")
return
}

var left, right constant.Value

if jsonValue.GetType() == transform.JSONInt {
left = constant.MakeInt64(jsonValue.GetIntValue())
} else {
left = constant.MakeFloat64(jsonValue.GetFloatValue())
}

if argsValue.GetType() == transform.JSONInt {
right = constant.MakeInt64(argsValue.GetIntValue())
} else {
right = constant.MakeFloat64(argsValue.GetFloatValue())
}

result := constant.BinaryOp(left, token.QUO, right)

newValue = []byte(result.String())

return
}
36 changes: 36 additions & 0 deletions converter/div_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package converter

import (
"github.com/mbordner/kazaam/registry"
"strconv"
"testing"
)

func TestDiv_Convert(t *testing.T) {
registry.RegisterConverter("div", &Div{})
c := registry.GetConverter("div")

table := []struct {
value string
arguments string
expected string
}{
{`10`, `2`, `5`,},
{`5`, `2`, `2.5`,},
{`5`, `.5`, `10`},
}

for _, test := range table {
v, e := c.Convert(getTestData(), []byte(test.value), []byte(strconv.Quote(test.arguments)))

if e != nil {
t.Error("error running convert function")
}

if string(v) != test.expected {
t.Error("unexpected result from convert")
t.Log("Expected: {}", test.expected)
t.Log("Actual: {}", string(v))
}
}
}
28 changes: 28 additions & 0 deletions converter/eqs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package converter

import (
"bytes"
"github.com/mbordner/kazaam/transform"
)

type Eqs struct {
ConverterBase
}

func (c *Eqs) Convert(jsonData []byte, value []byte, args []byte) (newValue []byte, err error) {

var argsValue *transform.JSONValue
argsValue, err = c.NewJSONValue(args)
if err != nil {
return
}

argsBytes := []byte(argsValue.GetStringValue())

if bytes.Equal(value, argsBytes) == true {
return []byte("true"), nil
}

return []byte("false"), nil

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

import (
"strconv"
"testing"
"github.com/mbordner/kazaam/registry"
)

func TestEqs_Convert(t *testing.T) {

registry.RegisterConverter("eqs", &Eqs{})
c := registry.GetConverter("eqs")

table := []struct {
value string
arguments string
expected string
}{
{`"The quick brown fox jumps over the lazy dog"`, `"The quick brown fox jumps over the lazy dog"`, `true`,},
{`42`,`42`,`true`,},
}

for _, test := range table {
v, e := c.Convert(getTestData(), []byte(test.value), []byte(strconv.Quote(test.arguments)))

if e != nil {
t.Error("error running convert function")
}

if string(v) != test.expected {
t.Error("unexpected result from convert")
t.Log("Expected: {}", test.expected)
t.Log("Actual: {}", string(v))
}
}

}
Loading