-
Notifications
You must be signed in to change notification settings - Fork 4
/
endpoint.go
190 lines (178 loc) · 4.39 KB
/
endpoint.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package webapi
import (
"container/list"
"errors"
"strconv"
"strings"
)
type (
//endpoint Endpoint and its sub-endpoints
endpoint struct {
prior *endpoint
val interface{}
nodes map[string]*endpoint
Fallback func(string, int) (string, error)
}
//search keyword
keyword struct {
text string
times int
}
//search stack (actual working object)
stack struct {
current *keyword
node *endpoint
history *list.List //[]*keyword
queue *list.List //[]*keyword
args *list.List
lower bool
fallback func(string, int) (string, error)
}
)
func (n *endpoint) setVal(value interface{}, path ...string) (err error) {
if n.nodes == nil {
n.nodes = map[string]*endpoint{}
}
if len(path) == 0 {
if n.val == nil {
n.val = value
return
}
return errors.New("the endpoint is already existed")
}
name := path[0]
if _, existed := n.nodes[name]; !existed {
n.nodes[name] = &endpoint{prior: n}
}
return n.nodes[name].setVal(value, path[1:]...)
}
//SetValue Add value to endpoint
func (n *endpoint) Add(path string, value interface{}) (err error) {
if n.nodes == nil {
n.nodes = map[string]*endpoint{}
}
if strings.Contains(path, "{digits}") || strings.Contains(path, "{float}") || strings.Contains(path, "{string}") || strings.Contains(path, "{bool}") {
err = n.setVal(value, strings.Split(path, "/")[1:]...)
if err != nil {
err = errors.New("the endpoint " + path + " is already existed")
}
} else {
_, existed := n.nodes[path]
if existed {
err = errors.New("the endpoint " + path + " is already existed")
} else {
n.nodes[path] = &endpoint{
val: value,
}
}
}
return
}
//Search Get the endpoint value via keyword list
func (n endpoint) search(lower bool, path ...string) (value interface{}, args []string) {
if len(path) == 0 {
path = []string{""}
}
var queue = list.New()
for _, p := range path[1:] {
queue.PushBack(&keyword{text: p})
}
return (&stack{
current: &keyword{text: path[0]},
history: list.New(),
args: list.New(),
queue: queue,
node: &n,
lower: lower,
fallback: n.Fallback,
}).search()
}
func (n endpoint) Search(path string, lower bool) (value interface{}, args []string) {
if n.nodes == nil {
return nil, nil
}
var testPath = path
if lower {
testPath = strings.ToLower(testPath)
}
if obj, existed := n.nodes[testPath]; existed {
return obj.val, nil
}
return n.search(lower, strings.Split(path, "/")[1:]...)
}
func (stack *stack) search() (value interface{}, args []string) {
if stack.fallback == nil {
stack.fallback = defaultFallback
}
var key = stack.current.text
if stack.lower {
key = strings.ToLower(key)
}
var err error
for stack.current.times++; stack.current.times > 1; stack.current.times++ {
key, err = stack.fallback(stack.current.text, stack.current.times-1)
if err != nil || len(key) > 0 {
break
}
}
if err != nil {
if stack.history.Len() == 0 || stack.node.prior == nil {
return nil, nil
}
stack.back()
}
if node, existed := stack.node.nodes[key]; existed {
if stack.queue.Len() == 0 {
params := []string{}
for stack.args.Front() != nil {
if arg := stack.args.Remove(stack.args.Front()).(string); len(arg) > 0 {
params = append(params, arg)
}
}
if stack.current.times > 1 {
params = append(params, stack.current.text)
}
return node.val, params
}
stack.next(node)
}
return stack.search()
}
func (stack *stack) next(node *endpoint) {
stack.node = node
stack.history.PushFront(stack.current)
var arg string
if stack.current.times > 1 {
arg = stack.current.text
}
stack.args.PushBack(arg)
stack.current = stack.queue.Remove(stack.queue.Front()).(*keyword)
}
func (stack *stack) back() {
stack.node = stack.node.prior
// stack.current.times = 0
stack.queue.PushFront(stack.current)
stack.args.Remove(stack.args.Back())
stack.current = stack.history.Remove(stack.history.Back()).(*keyword)
}
func defaultFallback(value string, times int) (string, error) {
switch times {
case 1:
digit, isDigit := strconv.ParseInt(value, 10, 64)
decimal, isDecimal := strconv.ParseFloat(value, 64)
if isDigit == nil && float64(digit) == decimal {
return "{digits}", nil
}
if isDecimal == nil {
return `{float}`, nil
}
if strings.ToLower(value) == "true" || strings.ToLower(value) == "false" {
return `{bool}`, nil
}
return "", nil
case 2:
return `{string}`, nil
default:
return "", errors.New("")
}
}