-
Notifications
You must be signed in to change notification settings - Fork 1
/
callpath.go
162 lines (135 loc) · 2.73 KB
/
callpath.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package callpath
import (
"errors"
"git.golaxy.org/core/util/types"
"git.golaxy.org/core/util/uid"
"strings"
)
const (
Service = "S"
Runtime = "R"
Entity = "E"
Client = "C"
)
var (
Sep = byte('>')
)
type CallPath struct {
Category string
ExcludeSrc bool
EntityId uid.Id
Plugin string
Component string
Method string
}
func (cp CallPath) Encode() (string, error) {
var sb strings.Builder
switch cp.Category {
case Service:
sb.WriteString(cp.Category)
sb.WriteByte(Sep)
sb.WriteByte(types.Bool2Int[byte](cp.ExcludeSrc))
sb.WriteByte(Sep)
sb.WriteString(cp.Plugin)
sb.WriteByte(Sep)
sb.WriteString(cp.Method)
return sb.String(), nil
case Runtime:
sb.WriteString(cp.Category)
sb.WriteByte(Sep)
sb.WriteByte(types.Bool2Int[byte](cp.ExcludeSrc))
sb.WriteByte(Sep)
sb.WriteString(cp.EntityId.String())
sb.WriteByte(Sep)
sb.WriteString(cp.Plugin)
sb.WriteByte(Sep)
sb.WriteString(cp.Method)
return sb.String(), nil
case Entity:
sb.WriteString(cp.Category)
sb.WriteByte(Sep)
sb.WriteByte(types.Bool2Int[byte](cp.ExcludeSrc))
sb.WriteByte(Sep)
sb.WriteString(cp.EntityId.String())
sb.WriteByte(Sep)
sb.WriteString(cp.Component)
sb.WriteByte(Sep)
sb.WriteString(cp.Method)
return sb.String(), nil
case Client:
sb.WriteString(cp.Category)
sb.WriteByte(Sep)
sb.WriteString(cp.EntityId.String())
sb.WriteByte(Sep)
sb.WriteString(cp.Method)
return sb.String(), nil
default:
return "", errors.New("rpc: invalid action")
}
}
func (cp CallPath) String() string {
str, _ := cp.Encode()
return str
}
func Parse(path string) (CallPath, error) {
var cp CallPath
loop:
for i := 0; ; i++ {
idx := strings.IndexByte(path, Sep)
if idx < 0 {
if path == "" {
break
}
idx = len(path)
}
field := path[:idx]
switch i {
case 0:
cp.Category = field
switch cp.Category {
case Service, Runtime, Entity, Client:
default:
return CallPath{}, errors.New("rpc: invalid Category")
}
case 1:
switch cp.Category {
case Service, Runtime, Entity:
if len(field) <= 0 {
return CallPath{}, errors.New("rpc: invalid ExcludeSrc")
}
cp.ExcludeSrc = types.Int2Bool[byte](field[0])
case Client:
cp.EntityId = uid.From(field)
}
case 2:
switch cp.Category {
case Service, Runtime, Entity:
cp.EntityId = uid.From(field)
case Client:
cp.Method = field
}
case 3:
switch cp.Category {
case Service:
cp.Method = field
case Runtime:
cp.Plugin = field
case Entity:
cp.Component = field
}
case 4:
switch cp.Category {
case Runtime, Entity:
cp.Method = field
}
default:
break loop
}
if idx < len(path) {
path = path[idx+1:]
continue
}
break
}
return cp, nil
}