forked from glennliao/apijson-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_func.go
More file actions
executable file
·86 lines (66 loc) · 1.75 KB
/
node_func.go
File metadata and controls
executable file
·86 lines (66 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package query
import (
"fmt"
"path/filepath"
"strings"
"github.com/glennliao/apijson-go/consts"
"github.com/glennliao/apijson-go/model"
"github.com/glennliao/apijson-go/util"
)
type funcNode struct {
node *Node
}
func newFuncNode(n *Node) *funcNode {
return &funcNode{node: n}
}
func (h *funcNode) parse() {
}
func (h *funcNode) fetch() {
}
func (h *funcNode) result() {
n := h.node
queryConfig := n.queryContext.queryConfig
functionName, paramKeys := util.ParseFunctionsStr(n.simpleReqVal)
_func := queryConfig.Func(functionName)
if _func == nil {
n.err = consts.NewValidReqErr("functions not exists: " + functionName)
return
}
// todo batch support
param := model.Map{}
for i, item := range _func.ParamList {
paramName := paramKeys[i]
if strings.HasPrefix(paramName, "/") { // 这里/开头是相对同级
dir := filepath.Dir(n.Path)
if dir == "." {
dir = ""
paramName = paramName[1:]
}
paramName = dir + paramName
}
refPath, paramName := util.ParseRefCol(paramName)
if refPath == n.Path { // 不能依赖自身
n.err = consts.NewValidReqErr(fmt.Sprintf("node cannot ref self: (%s)", refPath))
return
}
valNode := n.queryContext.pathNodes[refPath]
if valNode == nil {
h.node.err = consts.NewValidReqErr(fmt.Sprintf("param %s no found on %s", paramKeys[i], functionName))
return
}
if valNode.ret != nil {
switch valNode.ret.(type) {
case model.Map:
param[item.Name] = util.String(valNode.ret.(model.Map)[paramName])
case string:
param[item.Name] = valNode.ret.(string)
}
} else {
param[item.Name] = util.String(valNode.simpleReqVal)
}
}
n.ret, n.err = queryConfig.CallFunc(n.ctx, functionName, param)
}
func (h *funcNode) nodeType() int {
return NodeTypeStruct
}