forked from glennliao/apijson-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_ref.go
executable file
·80 lines (66 loc) · 1.52 KB
/
node_ref.go
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
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"
)
const (
totalInList = "[]/total"
)
type RefNode struct {
node *Node
}
func newRefNode(n *Node) *RefNode {
return &RefNode{node: n}
}
func (r *RefNode) parse() {
n := r.node
refStr := n.simpleReqVal
if strings.HasPrefix(refStr, "/") { // 这里/开头是相对同级
refStr = filepath.Dir(n.Path) + refStr
}
refPath, refCol := util.ParseRefCol(refStr)
if refPath == n.Path { // 不能依赖自身
n.err = consts.NewValidReqErr(fmt.Sprintf("node cannot ref self: (%s)", refPath))
return
}
refNode := n.queryContext.pathNodes[refPath]
if refNode == nil {
n.err = consts.NewValidReqErr(fmt.Sprintf(" node %s is nil, but ref by %s", refPath, n.Path))
return
}
n.refKeyMap = make(map[string]NodeRef)
if strings.HasSuffix(n.simpleReqVal, totalInList) {
setNeedTotal(refNode)
}
n.refKeyMap[n.Key] = NodeRef{
column: refCol,
node: refNode,
}
}
func (r *RefNode) fetch() {
n := r.node
for _, refNode := range n.refKeyMap {
if strings.HasSuffix(refNode.column, consts.Total) {
n.total = refNode.node.total
} else {
refRet := refNode.node.ret
switch refRet.(type) {
case model.Map:
n.ret = refRet.(model.Map)[refNode.column]
}
}
}
}
func (r *RefNode) result() {
n := r.node
if strings.HasSuffix(n.simpleReqVal, totalInList) {
n.ret = n.total
}
}
func (r *RefNode) nodeType() int {
return NodeTypeRef
}