Skip to content

Commit f39bb88

Browse files
committed
exports ParseAstToJsonObject and ParseJsonObjectToAst
1 parent 3129bdd commit f39bb88

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

defBox.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
)
77

88
var DefBox = &Sandbox{map[string]*BoxFunc{
9+
// (if, conditionExp, successExp, failExp)
910
"if": ToLazySandboxFun(func(args []interface{}, attachment interface{}, pcpServer *PcpServer) (interface{}, error) {
1011
if len(args) < 2 || len(args) > 3 {
1112
return nil, errors.New("if grammer error. if must have at least 2 params, at most 3 params. eg: [\"if\", true, 1, 0], [\"if\", true, 1]")

pcpServer.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ func (pcpServer *PcpServer) Execute(source string, attachment interface{}) (inte
3030

3131
// @param arr. Pure Json Object.
3232
func (pcpServer *PcpServer) ExecuteJsonObj(arr interface{}, attachment interface{}) (interface{}, error) {
33-
ast := parseAst(arr)
34-
return pcpServer.ExecuteAst(ast, attachment)
33+
return pcpServer.ExecuteAst(ParseJsonObjectToAst(arr), attachment)
3534
}
3635

3736
func (p *PcpServer) ExecuteAst(ast interface{}, attachment interface{}) (interface{}, error) {
@@ -47,6 +46,7 @@ func (p *PcpServer) ExecuteAst(ast interface{}, attachment interface{}) (interfa
4746

4847
var err error = nil
4948

49+
// resolve params in concurrent way
5050
for i, param := range funNode.params {
5151
wg.Add(1)
5252
go func(i int, param interface{}) {
@@ -78,7 +78,8 @@ func (p *PcpServer) ExecuteAst(ast interface{}, attachment interface{}) (interfa
7878
return ast, nil
7979
}
8080

81-
func parseAst(source interface{}) interface{} {
81+
// convert source object to ast
82+
func ParseJsonObjectToAst(source interface{}) interface{} {
8283
switch arr := source.(type) {
8384
case []interface{}:
8485
if len(arr) == 0 {
@@ -87,13 +88,13 @@ func parseAst(source interface{}) interface{} {
8788

8889
switch head := arr[0].(type) {
8990
case string:
90-
if head == "'" {
91+
if head == "'" { // escape parsing array
9192
return arr[1:]
9293
} else {
9394
var params []interface{}
9495

9596
for i := 1; i < len(arr); i++ {
96-
params = append(params, parseAst(arr[i]))
97+
params = append(params, ParseJsonObjectToAst(arr[i]))
9798
}
9899

99100
return FunNode{head, params}
@@ -106,6 +107,25 @@ func parseAst(source interface{}) interface{} {
106107
}
107108
}
108109

110+
// convert ast to source object
111+
func ParseAstToJsonObject(ast interface{}) interface{} {
112+
switch funNode := ast.(type) {
113+
114+
case FunNode:
115+
list := []interface{}{funNode.funName}
116+
for _, param := range funNode.params {
117+
list = append(list, ParseAstToJsonObject(param))
118+
}
119+
return list
120+
121+
case []interface{}:
122+
return append([]interface{}{"'"}, funNode...)
123+
124+
default:
125+
return funNode
126+
}
127+
}
128+
109129
// NewPcpServer merge sandbox with default sandbox
110130
func NewPcpServer(sandbox *Sandbox) *PcpServer {
111131
return &PcpServer{sandbox}

pcpServer_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,32 @@ func TestNotEqualFunction(t *testing.T) {
259259
runPcpCall(t, pcpServer, `["!=", "a", "A"]`, true)
260260
runPcpCallExpectError(t, pcpServer, `["!=", 1]`)
261261
}
262+
263+
func runParseAstJson(t *testing.T, source string) {
264+
var arr interface{}
265+
err := json.Unmarshal([]byte(source), &arr)
266+
if err != nil {
267+
t.Errorf(err.Error())
268+
}
269+
ast := ParseJsonObjectToAst(arr)
270+
271+
bs, err := json.Marshal(ParseAstToJsonObject(ast))
272+
if err != nil {
273+
t.Errorf(err.Error())
274+
}
275+
276+
assertEqual(t, string(bs), source, "")
277+
}
278+
279+
func TestParseAstToJsonObject(t *testing.T) {
280+
runParseAstJson(t, "1")
281+
runParseAstJson(t, `"hello"`)
282+
runParseAstJson(t, `true`)
283+
runParseAstJson(t, `null`)
284+
runParseAstJson(t, `{}`)
285+
runParseAstJson(t, `["'",1,2,3]`) // [1,2,3] <=> [',1,2,3]
286+
runParseAstJson(t, `["if",true,["get",3]]`)
287+
runParseAstJson(t, `["'",true,["'",3]]`)
288+
runParseAstJson(t, `["if",true,["'","ok"]]`)
289+
runParseAstJson(t, `{"a":[1,2,3]}`)
290+
}

0 commit comments

Comments
 (0)