Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 11 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,14 @@ func TestInvalidMustCompilePanics(t *testing.T) {
}()
MustCompile("not a valid expression")
}

func TestFunctionToObject(t *testing.T) {
assert := assert.New(t)
var j = []byte(`{"name":"John","test":{"js":"{\"age\": 26, \"city\": \"BeiJin\"}"},"nested_json":"{\"age\": 25, \"city\": \"NewYork\"}"}`)
var d interface{}
err := json.Unmarshal(j, &d)
assert.Nil(err)
result, err := Search("[name,to_object(test.js).city,to_object(nested_json).city]", d)
assert.Nil(err)
assert.Equal([]interface{}{"John", "BeiJin", "NewYork"}, result)
}
17 changes: 17 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,13 @@ func newFunctionCaller() *functionCaller {
},
handler: jpfNotNull,
},
"to_object": {
name: "to_object",
arguments: []argSpec{
{types: []jpType{jpString}},
},
handler: jpfToObject,
},
}
return caller
}
Expand Down Expand Up @@ -839,3 +846,13 @@ func jpfNotNull(arguments []interface{}) (interface{}, error) {
}
return nil, nil
}

func jpfToObject(arguments []interface{}) (interface{}, error) {
v := arguments[0].(string)
var obj interface{}
err := json.Unmarshal([]byte(v), &obj)
if err != nil {
return nil, errors.New("invalid json string")
}
return obj, nil
}