-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
func_and.go
53 lines (43 loc) · 987 Bytes
/
func_and.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
package dasel
import (
"fmt"
"reflect"
)
var AndFunc = BasicFunction{
name: "and",
runFn: func(c *Context, s *Step, args []string) (Values, error) {
if err := requireXOrMoreArgs("and", args, 1); err != nil {
return nil, err
}
input := s.inputs()
runComparison := func(value Value, selector string) (bool, error) {
gotValues, err := c.subSelect(value, selector)
if err != nil {
return false, err
}
if len(gotValues) > 1 {
return false, fmt.Errorf("and expects selector to return a single value")
}
if len(gotValues) == 0 {
return false, nil
}
return IsTruthy(gotValues[0]), nil
}
res := make(Values, 0)
for _, val := range input {
valPassed := true
for _, cmp := range args {
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
},
}