/
dispatch.go
66 lines (54 loc) · 1.14 KB
/
dispatch.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
package cocaine12
import (
"fmt"
)
type dispatchType int
const (
recursiveDispatch dispatchType = iota
emptyDispatch
otherDispatch
)
type dispatchMap map[uint64]dispatchItem
func (d *dispatchMap) Methods() []string {
var methods = make([]string, 0, len(*d))
for _, v := range *d {
methods = append(methods, v.Name)
}
return methods
}
func (d *dispatchMap) MethodByName(name string) (uint64, error) {
for i, v := range *d {
if v.Name == name {
return i, nil
}
}
return 0, fmt.Errorf("no `%s` method", name)
}
type dispatchItem struct {
Name string
Downstream *streamDescription
Upstream *streamDescription
}
type StreamDescriptionItem struct {
Name string
Description *streamDescription
}
type streamDescription map[uint64]*StreamDescriptionItem
func (s *streamDescription) MethodByName(name string) (uint64, error) {
for i, v := range *s {
if v.Name == name {
return i, nil
}
}
return 0, fmt.Errorf("no `%s` method", name)
}
func (s *streamDescription) Type() dispatchType {
switch {
case s == nil:
return recursiveDispatch
case len(*s) == 0:
return emptyDispatch
default:
return otherDispatch
}
}