/
types.go
87 lines (69 loc) · 1.35 KB
/
types.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
package cot
import (
_ "embed"
"sort"
"strings"
)
//go:embed types
var strTypes string
var (
types = make(map[string]*CotType)
Root = new(CotType)
)
type CotType struct {
Code string `json:"code"`
Name string `json:"name"`
Next []*CotType `json:"next"`
}
func init() {
for _, s := range strings.Split(strTypes, "\n") {
ss := strings.Trim(s, " \n\r\t")
if ss == "" {
continue
}
n := strings.Split(ss, ";")
types[n[0]] = &CotType{
Code: n[0],
Name: n[1],
Next: nil,
}
}
for _, ct := range types {
n := strings.Split(ct.Code, "-")
if len(n) == 1 {
Root.Next = append(Root.Next, ct)
continue
}
found := false
for i := len(n) - 1; i > 0; i-- {
t1 := strings.Join(n[:i], "-")
if ct1, ok := types[t1]; ok {
found = true
ct1.Next = append(ct1.Next, ct)
break
}
}
if !found {
Root.Next = append(Root.Next, ct)
}
}
for _, ct := range types {
if ct.Next != nil {
sort.SliceStable(ct.Next, func(i, j int) bool {
return strings.Compare(ct.Next[i].Code, ct.Next[j].Code) < 0
})
}
}
sort.SliceStable(Root.Next, func(i, j int) bool {
return strings.Compare(Root.Next[i].Code, Root.Next[j].Code) < 0
})
}
func (t *CotType) Level() int {
if t == nil {
return 0
}
return strings.Count(t.Code, "-") + 1
}
func GetNext(s string) []*CotType {
return types[s].Next
}