Newer
Older
package runner
import (
"fmt"
"gopkg.in/yaml.v3"
)
// Variable ...
type Variable struct {
Name string
Value interface{}
}
type Variables []Variable
func (v Variables) Get(path string) (interface{}, bool) {
sp := strings.Split(path, ".")
head := sp[0]
tail := sp[1:]
for _, variable := range v {
if variable.Name == head {
if len(tail) == 0 {
return variable.Value, true
}
return lookupVariable(variable.Value, strings.Join(tail, "."))
}
}
return nil, false
}
func (v Variables) GetD(path string, defaultValue interface{}) interface{} {
ret, ok := v.Get(path)
if ok {
return ret
}
return defaultValue
}
func (v *Variables) UnmarshalYAML(node *yaml.Node) error {
if err := node.Decode((*[]Variable)(v)); err == nil {
return nil
}
if node.Kind != yaml.MappingNode {
return fmt.Errorf("expects a mapping, got: %d", node.Kind)
}
*v = make(Variables, 0, len(node.Content)/2)
var next Variable
for i, content := range node.Content {
if i%2 == 0 {
if err := content.Decode(&next.Name); err != nil {
return err
}
} else {
if err := content.Decode(&next.Value); err != nil {
return err
}
*v = append(*v, next)
}
}
return nil
}
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
127
128
129
130
131
132
func (v *Variables) Overlay(variables ...Variable) {
var newVariables Variables
for _, inputVar := range variables {
path := strings.Split(inputVar.Name, ".")
head := path[0]
tail := path[1:]
for i := range *v {
if (*v)[i].Name == head {
if len(tail) == 0 {
(*v)[i].Value = inputVar.Value
} else {
setVariable((*v)[i].Value, tail, inputVar.Value)
}
continue
}
}
newVariables = append(newVariables, inputVar)
}
*v = append(*v, newVariables...)
}
func setVariable(v interface{}, path []string, value interface{}) {
head := path[0]
tail := path[1:]
hasTail := len(tail) != 0
switch t := v.(type) {
case map[string]interface{}:
if hasTail {
nextValue, ok := t[head]
if !ok {
return
}
setVariable(nextValue, tail, value)
} else {
t[head] = value
}
case map[interface{}]interface{}:
if hasTail {
nextValue, ok := t[head]
if !ok {
return
}
setVariable(nextValue, tail, value)
} else {
t[head] = value
}
case []interface{}:
index, err := strconv.Atoi(head)
if err != nil {
return
}
if index >= len(t) || index < 0 {
return
}
if hasTail {
setVariable(t[index], tail, value)
} else {
t[index] = value
}
default:
}
}
func lookupVariable(variables interface{}, name string) (interface{}, bool) {
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
path := strings.Split(name, ".")
var v interface{} = variables
var ok bool
for _, key := range path {
v, ok = lookupVariableHelper(v, key)
if !ok {
return nil, false
}
}
return v, true
}
func lookupVariableHelper(v interface{}, key string) (interface{}, bool) {
switch t := v.(type) {
case map[string]interface{}:
ret, ok := t[key]
return ret, ok
case map[interface{}]interface{}:
ret, ok := t[key]
return ret, ok
case []interface{}:
index, err := strconv.Atoi(key)
if err != nil {
return nil, false
}
if index >= len(t) || index < 0 {
return nil, false
}
return t[index], true
default:
return nil, false
}
}