forked from k1LoW/runn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
126 lines (117 loc) · 2.64 KB
/
store.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package runn
const (
storeVarsKey = "vars"
storeStepsKey = "steps"
storeParentKey = "parent"
storeIncludedKey = "included"
storeCurrentKey = "current"
storePreviousKey = "previous"
storeFuncValue = "[func]"
storeStepRunKey = "run"
storeOutcomeKey = "outcome"
)
type store struct {
steps []map[string]interface{}
stepMapKeys []string
stepMap map[string]map[string]interface{}
vars map[string]interface{}
funcs map[string]interface{}
bindVars map[string]interface{}
parentVars map[string]interface{}
useMap bool // Use map syntax in `steps:`.
loopIndex *int
}
func (s *store) recordAsMapped(k string, v map[string]interface{}) {
if !s.useMap {
panic("recordAsMapped can only be used if useMap = true")
}
s.stepMap[k] = v
s.stepMapKeys = append(s.stepMapKeys, k)
}
func (s *store) recordAsListed(v map[string]interface{}) {
if s.useMap {
panic("recordAsMapped can only be used if useMap = false")
}
s.steps = append(s.steps, v)
}
func (s *store) previous() map[string]interface{} {
if !s.useMap {
if len(s.steps) < 2 {
return nil
}
return s.steps[len(s.steps)-2]
}
if len(s.stepMapKeys) < 2 {
return nil
}
pk := s.stepMapKeys[len(s.stepMapKeys)-2]
if v, ok := s.stepMap[pk]; ok {
return v
}
return nil
}
func (s *store) latest() map[string]interface{} {
if !s.useMap {
if len(s.steps) == 0 {
return nil
}
return s.steps[len(s.steps)-1]
}
if len(s.stepMapKeys) == 0 {
return nil
}
lk := s.stepMapKeys[len(s.stepMapKeys)-1]
if v, ok := s.stepMap[lk]; ok {
return v
}
return nil
}
func (s *store) toNormalizedMap() map[string]interface{} {
store := map[string]interface{}{}
for k := range s.funcs {
store[k] = storeFuncValue
}
store[storeVarsKey] = s.vars
if s.useMap {
store[storeStepsKey] = s.stepMap
} else {
store[storeStepsKey] = s.steps
}
for k, v := range s.bindVars {
store[k] = v
}
if s.loopIndex != nil {
store[loopCountVarKey] = *s.loopIndex
}
return store
}
func (s *store) toMap() map[string]interface{} {
store := map[string]interface{}{}
for k, v := range s.funcs {
store[k] = v
}
store[storeVarsKey] = s.vars
if s.useMap {
store[storeStepsKey] = s.stepMap
} else {
store[storeStepsKey] = s.steps
}
if s.parentVars != nil {
store[storeParentKey] = s.parentVars
}
for k, v := range s.bindVars {
store[k] = v
}
if s.loopIndex != nil {
store[loopCountVarKey] = *s.loopIndex
}
return store
}
func (s *store) clearSteps() {
s.steps = []map[string]interface{}{}
s.stepMapKeys = []string{}
s.stepMap = map[string]map[string]interface{}{}
// keep vars, bindVars
s.parentVars = map[string]interface{}{}
s.loopIndex = nil
}