forked from caicloud/nirvana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
65 lines (55 loc) · 1.76 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
/*
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"
)
// pathNode matches all rest path.
type pathNode struct {
handler
// key is the key for the rest path.
key string
}
// Target returns the matching target of the node.
func (n *pathNode) Target() string {
return ""
}
// Kind returns the kind of the router node.
func (n *pathNode) Kind() RouteKind {
return Path
}
// 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 *pathNode) Match(ctx context.Context, c Container, path string) (Executor, error) {
c.Set(n.key, path)
return n.handler.unionExecutor(ctx)
}
// Merge merges r to the current router. The type of r should be same
// as the current one.
func (n *pathNode) Merge(r Router) (Router, error) {
node, ok := r.(*pathNode)
if !ok {
return nil, UnknownRouterType.Error(r.Kind(), reflect.TypeOf(r).String())
}
if n.key != node.key {
return nil, UnmatchedRouterKey.Error(n.key, node.key)
}
if err := n.handler.Merge(&node.handler); err != nil {
return nil, err
}
return n, nil
}