Function model describe as json
{
"name": "json",
"type": "flogo:function",
"version": "0.10.0",
"title": "Json Functions",
"description": "Json Functions",
"homepage": "https://github.com/prject-flogo/contrib/tree/master/function/json",
"functions": [
{
"name": "path",
"description": "Use JSONPath expression to get value from JSON object. Refer https://github.com/oliveagle/jsonpath for expression format.",
"example": "json.path(\"$.key\",$activity[xxx].data) => value",
"args": [
{
"name": "path",
"type": "string"
},
{
"name": "object",
"type": "any"
}
],
"return": {
"type": "any"
}
}
]
}
Note
category name. Take above example, the name must be jsoncategory name, Take above example, the name must be jsoncategory name as well, Take above example, the name must be jsonName methodfunc (fnPath) Name() string {
return "path"
}
name() method must match the name in descripor.jsonSig method should also match types that defined in descriptor.jsonpackage json
import (
"github.com/oliveagle/jsonpath"
"github.com/project-flogo/core/data"
"github.com/project-flogo/core/data/expression/function"
"strings"
)
func init() {
_ = function.Register(&fnPath{})
}
type fnPath struct {
}
// Name returns the name of the function
func (fnPath) Name() string {
return "path"
}
// Sig returns the function signature
func (fnPath) Sig() (paramTypes []data.Type, isVariadic bool) {
return []data.Type{data.TypeString, data.TypeAny}, false
}
// Eval executes the function
func (fnPath) Eval(params ...interface{}) (interface{}, error) {
expression := params[0].(string)
//tmp fix to take $loop as $. for now
if strings.HasPrefix(strings.TrimSpace(expression), "$loop.") {
expression = strings.Replace(expression, "$loop", "$", -1)
}
return jsonpath.JsonPathLookup(params[1], expression)
}