forked from caicloud/nirvana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
117 lines (107 loc) · 3.09 KB
/
string.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
/*
Copyright 2017 Caicloud Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package router
import (
"context"
"reflect"
"strings"
)
// stringNode describes a string router node.
type stringNode struct {
handler
children
// prefix is the fixed string to match path.
prefix string
}
// Target returns the matching target of the node.
func (n *stringNode) Target() string {
return n.prefix
}
// Kind returns the kind of the router node.
func (n *stringNode) Kind() RouteKind {
return String
}
// Match find an executor matched by path.
// The context contains information to inspect executor.
// The container can save key-value pair from the path.
// If the router is the leaf node to match the path, it will return
// the first executor which Inspect() returns true.
func (n *stringNode) Match(ctx context.Context, c Container, path string) (Executor, error) {
if n.prefix != "" && !strings.HasPrefix(path, n.prefix) {
// No match
return nil, RouterNotFound.Error()
}
if len(n.prefix) < len(path) {
// Match prefix
executor, err := n.children.Match(ctx, c, path[len(n.prefix):])
if err != nil {
return nil, err
}
return n.handler.pack(executor)
}
// Match self
return n.handler.unionExecutor(ctx)
}
// Merge merges r to the current router. The type of r should be same
// as the current one or it panics.
func (n *stringNode) Merge(r Router) (Router, error) {
node, ok := r.(*stringNode)
if !ok {
return nil, UnknownRouterType.Error(r.Kind(), reflect.TypeOf(r).String())
}
commonPrefix := 0
for commonPrefix < len(n.prefix) && commonPrefix < len(node.prefix) {
if n.prefix[commonPrefix] != node.prefix[commonPrefix] {
break
}
commonPrefix++
}
if commonPrefix <= 0 {
return nil, NoCommonPrefix.Error()
}
switch {
case commonPrefix == len(n.prefix) && commonPrefix == len(node.prefix):
if err := n.handler.Merge(&node.handler); err != nil {
return nil, err
}
if err := n.children.merge(&node.children); err != nil {
return nil, err
}
case commonPrefix == len(n.prefix):
node.prefix = node.prefix[commonPrefix:]
if err := n.addRouter(node); err != nil {
return nil, err
}
case commonPrefix == len(node.prefix):
copy := *n
copy.prefix = copy.prefix[commonPrefix:]
*n = *node
if err := n.addRouter(©); err != nil {
return nil, err
}
default:
copy := *n
copy.prefix = copy.prefix[commonPrefix:]
node.prefix = node.prefix[commonPrefix:]
n.handler = handler{}
n.children = children{}
n.prefix = n.prefix[:commonPrefix]
if err := n.addRouter(©); err != nil {
return nil, err
}
if err := n.addRouter(node); err != nil {
return nil, err
}
}
return n, nil
}