Skip to content

Commit

Permalink
chore: Custom expression functions (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarabala1979 authored Feb 27, 2021
1 parent 7a9a483 commit 52727e4
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
108 changes: 108 additions & 0 deletions expr/function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package expr

import (
"encoding/json"
"fmt"
"reflect"
"strconv"

"github.com/oliveagle/jsonpath"
)

func GetExprEnvFunctionMap() map[string]interface{} {
return map[string]interface{}{
"asInt": AsInt,
"asFloat": AsFloat,
"string": AsStr,
"jsonpath": JsonPath,
}
}

func AsStr(val interface{}) interface{} {
return fmt.Sprintf("%v", val)
}

func JsonPath(jsonStr string, path string) interface{} {
var jsonMap interface{}
err := json.Unmarshal([]byte(jsonStr), &jsonMap)
if err != nil {
panic(err)
}
value, err := jsonpath.JsonPathLookup(jsonMap, path)
if err != nil {
panic(err)
}
return value
}

func AsInt(in interface{}) int64 {
switch i := in.(type) {
case float64:
return int64(i)
case float32:
return int64(i)
case int64:
return i
case int32:
return int64(i)
case int16:
return int64(i)
case int8:
return int64(i)
case int:
return int64(i)
case uint64:
return int64(i)
case uint32:
return int64(i)
case uint16:
return int64(i)
case uint8:
return int64(i)
case uint:
return int64(i)
case string:
inAsInt, err := strconv.ParseInt(i, 10, 64)
if err == nil {
return inAsInt
}
panic(err)
}
panic(fmt.Sprintf("asInt() not supported on %v %v", reflect.TypeOf(in), in))
}

func AsFloat(in interface{}) float64 {
switch i := in.(type) {
case float64:
return i
case float32:
return float64(i)
case int64:
return float64(i)
case int32:
return float64(i)
case int16:
return float64(i)
case int8:
return float64(i)
case int:
return float64(i)
case uint64:
return float64(i)
case uint32:
return float64(i)
case uint16:
return float64(i)
case uint8:
return float64(i)
case uint:
return float64(i)
case string:
inAsFloat, err := strconv.ParseFloat(i, 64)
if err == nil {
return inAsFloat
}
panic(err)
}
panic(fmt.Sprintf("asFloat() not supported on %v %v", reflect.TypeOf(in), in))
}
31 changes: 31 additions & 0 deletions expr/function_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package expr

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestAsFloat(t *testing.T) {
assert.Equal(t, int64(1), AsInt(int32(1)))
assert.Equal(t, int64(1), AsInt(int64(1)))
assert.Equal(t, int64(1), AsInt("1"))
assert.Panics(t, func() { AsInt("1.56") })
}

func TestAsFloat2(t *testing.T) {
assert.Equal(t, 1.24, AsFloat(1.24))
assert.Equal(t, 1.65, AsFloat("1.65"))
}

func TestAsStr(t *testing.T) {
assert.Equal(t, "1.24", AsStr(1.24))
assert.Equal(t, "1", AsStr(1))
}

func TestJsonPath(t *testing.T) {
simpleJson := "{\"employee\":{\"name\":\"sonoo\",\"salary\":56000,\"married\":true}}"
arrayJson := "{\"employees\":[{\"name\":\"Shyam\",\"email\":\"[email protected]\"},{\"name\":\"Bob\",\"email\":\"[email protected]\"}," +
"{\"name\":\"Jai\",\"email\":\"[email protected]\"}]}"
assert.Equal(t, "sonoo", JsonPath(simpleJson, "$.employee.name"))
assert.Equal(t, "Bob", JsonPath(arrayJson, "$.employees[1].name"))
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/golang/protobuf v1.3.3
github.com/grpc-ecosystem/grpc-gateway v1.14.6
github.com/minio/minio-go/v7 v7.0.2
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.6.0
github.com/smartystreets/goconvey v1.6.4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8m
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852 h1:Yl0tPBa8QPjGmesFh1D0rDy+q1Twx6FyU7VWHi8wZbI=
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852/go.mod h1:eqOVx5Vwu4gd2mmMZvVZsgIqNSaW3xxRThUJ0k/TPk4=
github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
Expand Down

0 comments on commit 52727e4

Please sign in to comment.