-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Custom expression functions (#37)
- Loading branch information
1 parent
7a9a483
commit 52727e4
Showing
4 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters