-
Notifications
You must be signed in to change notification settings - Fork 7
/
path.go
119 lines (106 loc) · 2.44 KB
/
path.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package roc
import (
"fmt"
"strings"
)
// Path ROC调用的路径
type Path struct {
strs []string
pos int
objType ObjType
objID string
}
// O 根据目标ROC的类型及ID,构造一个ROC调用路径
func O(objType ObjType, objID string) *Path {
res := &Path{}
res.objType = objType
res.objID = objID
return res
}
// kstr的格式必须为 ROC 远程对象调用那样定义的格式,如:
// kstrDecode 对象类型[对象的键]
func kstrDecode(kstr string) (ObjType, string) {
t := ""
key := ""
inkey := false
for _, k := range kstr {
if k == '[' {
inkey = true
} else if k == ']' {
inkey = false
} else if k == '.' {
break
} else {
if key == "" && !inkey {
t = t + fmt.Sprintf("%c", k)
} else if t != "" && inkey {
key = key + fmt.Sprintf("%c", k)
} else {
return "", ""
}
}
}
return ObjType(t), key
}
// NewPath 根据调用路径字符串,构造一个ROC调用路径
func NewPath(pathstr string) *Path {
res := &Path{}
strs := strings.Split(pathstr, ".")
if len(strs) < 1 {
return res
}
t, id := kstrDecode(strs[0])
res.objType = ObjType(t)
res.objID = id
res.strs = strs[1:]
return res
}
// GetObjType 获取ROC调用路径的目标ROC对象类型
func (path *Path) GetObjType() ObjType {
return path.objType
}
// GetObjID 获取ROC调用路径的目标ROC对象ID
func (path *Path) GetObjID() string {
return path.objID
}
// GetPos 获取ROC调用路径当前行进到的位置
func (path *Path) GetPos() int {
return path.pos
}
// Get 获取当前ROC调用路径指定位置的值
func (path *Path) Get(pos int) string {
if pos < 0 || pos >= len(path.strs) {
return ""
}
return path.strs[pos]
}
// Move 移动当前ROC调用路径到下一个位置,并返回该位置的值
func (path *Path) Move() string {
if path.pos >= len(path.strs) {
return ""
}
res := path.strs[path.pos]
path.pos++
return res
}
// F 添加一个ROC调用路径的函数段,一个ROC调用可以携带多个函数名,中间以 . 号连接
func (path *Path) F(funcName string) *Path {
res := path
if res == nil {
res = &Path{}
}
res.strs = append(res.strs, funcName)
return res
}
// String 获取当前ROC调用路径的字符串描述
func (path *Path) String() string {
res := string(path.objType) + "[" + string(path.objID) + "]"
for _, v := range path.strs {
res += "." + v
}
return res
}
// Reset 重置当前ROC调用路径的位置
func (path *Path) Reset() {
path.pos = 0
}