forked from mbordner/kazaam
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conditional.go
76 lines (63 loc) · 2.01 KB
/
conditional.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
package transform
import (
"fmt"
)
type TransformFunc func(spec *Config, data []byte) ([]byte, error)
// Shift moves values from one provided json path to another in raw []byte.
func Conditional(spec *Config, data []byte) ([]byte, error) {
var outData []byte
if spec.InPlace {
outData = data
} else {
outData = []byte(`{}`)
}
defaultSpecTypes := map[string]TransformFunc{
"pass": Pass,
"shift": Shift,
"extract": Extract,
"default": Default,
"delete": Delete,
"concat": Concat,
"coalesce": Coalesce,
"timestamp": Timestamp,
"uuid": UUID,
"steps": Steps,
"merge": Merge,
"conditional": Conditional,
}
if conditional, ok := (*spec.Spec)["conditional"]; ok {
if !ok {
return nil, ParseError(fmt.Sprintln("Warn: Missing conditional"))
}
conditionals := conditional.(map[string]interface{})
if len(conditionals) >= 2 {
if condition, then := conditionals["condition"], conditionals["then"]; condition == nil && then == nil {
return nil, ParseError(fmt.Sprintln("Warn: Condition should have at least 2 sections, condition and then"))
}
}
condition := fmt.Sprintf("%v", conditionals["condition"])
be, err := NewBasicExpr(data, condition)
if err != nil {
return nil, ParseError(fmt.Sprintf("Warn: Condition %s failed to compile %v", condition, err))
}
evaluation, _ := be.Eval()
var operation map[string]interface{}
if evaluation {
operation = conditionals["then"].(map[string]interface{})
} else {
operation = conditionals["else"].(map[string]interface{})
}
var dataOutput []byte = data
for key, value := range operation {
function := defaultSpecTypes[key]
specFunction := value.(map[string]interface{})
config := Config{Spec: &specFunction, Require: spec.Require, InPlace: true}
dataOutput, err = function(&config, dataOutput)
if err != nil {
return nil, ParseError(fmt.Sprintf("Warn: Error executing %s operation %v", key, err))
}
}
outData = dataOutput
}
return outData, nil
}