-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
func_more_than.go
88 lines (71 loc) · 1.67 KB
/
func_more_than.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
package dasel
import (
"fmt"
"github.com/tomwright/dasel/v2/util"
"reflect"
"sort"
)
var MoreThanFunc = BasicFunction{
name: "moreThan",
runFn: func(c *Context, s *Step, args []string) (Values, error) {
if err := requireExactlyXArgs("moreThan", args, 2); err != nil {
return nil, err
}
input := s.inputs()
type comparison struct {
selector string
value string
}
comparisons := make([]comparison, 0)
currentComparison := comparison{}
for i, v := range args {
switch i % 2 {
case 0:
currentComparison.selector = v
case 1:
currentComparison.value = v
comparisons = append(comparisons, currentComparison)
currentComparison = comparison{}
}
}
runComparison := func(value Value, cmp comparison) (bool, error) {
gotValues, err := c.subSelect(value, cmp.selector)
if err != nil {
return false, err
}
if len(gotValues) > 1 {
return false, fmt.Errorf("equal expects selector to return a single value")
}
if len(gotValues) == 0 {
return false, nil
}
gotValue := util.ToString(gotValues[0].Interface())
// The values are equal
if gotValue == cmp.value {
return false, nil
}
sortedVals := []string{gotValue, cmp.value}
sort.Strings(sortedVals)
if sortedVals[0] == cmp.value {
return true, nil
}
return false, nil
}
res := make(Values, 0)
for _, val := range input {
valPassed := true
for _, cmp := range comparisons {
pass, err := runComparison(val, cmp)
if err != nil {
return nil, err
}
if !pass {
valPassed = false
break
}
}
res = append(res, Value{Value: reflect.ValueOf(valPassed)})
}
return res, nil
},
}