-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.go
83 lines (73 loc) · 1.38 KB
/
actions.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
package action
import (
"fmt"
"strings"
)
type Actions []*Action
func (a Actions) Size() int {
ret := 0
for _, x := range a {
ret += x.Size()
}
return ret
}
func (a Actions) Get(paths []string) (*Action, []string) {
if len(paths) == 0 {
return nil, nil
}
curr := a.Find(paths[0])
if curr == nil {
return nil, paths
}
if len(curr.Children) > 0 {
x, remaining := curr.Children.Get(paths[1:])
if x == nil {
return curr, paths[1:]
}
return x, remaining
}
return curr, paths[1:]
}
func (a Actions) Find(key string) *Action {
for _, act := range a {
if act.Key == key {
return act
}
}
return nil
}
func (a Actions) CleanKeys() {
for _, act := range a {
if !strings.HasPrefix(act.Key, "__") {
continue
}
proposed := strings.TrimPrefix(act.Key, "__")
match := a.Find(proposed)
idx := 1
for match != nil {
idx++
match = a.Find(fmt.Sprintf("%s-%d", proposed, idx))
}
if idx == 1 {
act.Key = proposed
} else {
act.Key = fmt.Sprintf("%s-%d", proposed, idx)
}
}
}
func (a Actions) Cleanup() {
a.cleanup()
}
func (a Actions) cleanup(path ...string) {
for _, kid := range a {
kid.Pkg = path
kid.Children.cleanup(append(append([]string{}, path...), kid.Key)...)
}
}
func (a Actions) Clone() Actions {
ret := make(Actions, 0, len(a))
for _, x := range a {
ret = append(ret, x.Clone(x.Pkg, x.Children.Clone()))
}
return ret
}