-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.go
72 lines (59 loc) · 1.05 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
package main
import (
"encoding/json"
"sort"
)
type Ruleset map[string]Table
type Table map[string]*Chain
type Chain struct {
Unmanaged bool
Policy string
Rules RuleList
}
type RuleList []Rule
type Rule struct {
Priority int
Comment string
Content string
}
func (r Ruleset) Merge(other Ruleset) {
for t, cs := range other {
if _, ok := r[t]; !ok {
r[t] = make(Table)
}
tc := r[t]
for c, cc := range cs {
if _, ok := tc[c]; !ok {
tc[c] = cc
} else {
tc[c].Rules = append(tc[c].Rules, cc.Rules...)
if cc.Policy != "" {
tc[c].Policy = cc.Policy
}
}
}
}
}
func (r Ruleset) Dump() string {
b, _ := json.MarshalIndent(&r, "", " ")
return string(b)
}
func (r Ruleset) Sort() {
for _, ts := range r {
for _, cs := range ts {
cs.Rules.Sort()
}
}
}
func (rl RuleList) Sort() {
sort.Stable(rl)
}
func (rl RuleList) Len() int {
return len(rl)
}
func (rl RuleList) Less(i, j int) bool {
return rl[i].Priority < rl[j].Priority
}
func (rl RuleList) Swap(i, j int) {
rl[i], rl[j] = rl[j], rl[i]
}