-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.v
77 lines (65 loc) · 1.39 KB
/
node.v
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
module very
import regex
struct Node {
mut:
val string
path string
param_name string
params map[string]string
term bool
depth int
is_group bool
children map[string]&Node
is_pattern bool
re ®ex.RE = unsafe { nil }
parent &Node = unsafe { nil }
mws []Handler
handler Handler = unsafe { nil }
term_count int
}
pub fn (mut t Node) new_child(val string, path string, handler Handler, term bool, is_group bool) &Node {
node := &Node{
val: val
path: path
term: term
depth: t.depth + 1
is_group: is_group
handler: handler
children: map[string]&Node{}
}
t.children[node.val] = node
return node
}
pub fn (mut t Node) set_pattern(is_pattern bool, segment string, param_name string) {
if is_pattern {
re := regex.regex_opt(segment) or { panic('register route node ${segment} failed: ${err}') }
t.param_name = param_name
t.is_pattern = is_pattern
t.re = &re
}
}
pub fn (t Node) parent() &Node {
return t.parent
}
pub fn (t Node) children() map[string]&Node {
return t.children.clone()
}
pub fn (t Node) terminating() bool {
return t.term
}
pub fn (t Node) val() string {
return t.val
}
pub fn (t Node) depth() int {
return t.depth
}
pub fn (t Node) handler_fn() Handler {
return t.handler
}
pub fn (t Node) str() string {
return 'Node{
path:\'${t.path}\',
pattern: ${t.is_pattern},
children: ${t.children}}
'
}