-
Notifications
You must be signed in to change notification settings - Fork 195
/
conditions.go
65 lines (54 loc) · 1.69 KB
/
conditions.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
package intrinsics
// condition evaluates a condition
func condition(name string, input interface{}, template interface{}, options *ProcessorOptions) interface{} {
if v, ok := input.(string); ok {
if v, ok := retrieveCondition(input, template); ok {
return v
}
if c := getCondition(v, template); c != nil {
res := search(c, template, options)
// replace the value in the template so the value can be reused
setCondition(v, res, template)
return res
}
}
return nil
}
func setCondition(name string, val interface{}, template interface{}) {
if template, ok := template.(map[string]interface{}); ok {
// Check there is a conditions section
if uconditions, ok := template["Conditions"]; ok {
// Check the conditions section is a map
if conditions, ok := uconditions.(map[string]interface{}); ok {
// Check there is a condition with the same name as the condition
if _, ok := conditions[name]; ok {
conditions[name] = val
}
}
}
}
}
func getCondition(name string, template interface{}) interface{} {
if template, ok := template.(map[string]interface{}); ok {
// Check there is a conditions section
if uconditions, ok := template["Conditions"]; ok {
// Check the conditions section is a map
if conditions, ok := uconditions.(map[string]interface{}); ok {
// Check there is a condition with the same name as the condition
if ucondition, ok := conditions[name]; ok {
return ucondition
}
}
}
}
return nil
}
func retrieveCondition(cName interface{}, template interface{}) (value bool, found bool) {
switch v := cName.(type) {
case string:
value, found = getCondition(v, template).(bool)
case bool:
value, found = v, true
}
return
}