-
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathcondition_equal.go
More file actions
54 lines (46 loc) · 1.21 KB
/
condition_equal.go
File metadata and controls
54 lines (46 loc) · 1.21 KB
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
package dasel
import (
"errors"
"fmt"
"reflect"
)
// EqualCondition lets you check for an exact match.
type EqualCondition struct {
// Key is the key of the value to check against.
Key string
// Value is the value we are looking for.
Value string
// Not is true if this is a not equal check.
Not bool
}
func (c EqualCondition) check(a interface{}, b interface{}) (bool, error) {
var res = fmt.Sprint(a) == b
if c.Not {
res = !res
}
return res, nil
}
// Check checks to see if other contains the required key value pair.
func (c EqualCondition) Check(other reflect.Value) (bool, error) {
if !other.IsValid() {
return false, &UnhandledCheckType{Value: nil}
}
value := unwrapValue(other)
if c.Key == "value" || c.Key == "." {
return c.check(value.Interface(), c.Value)
}
subRootNode := New(value.Interface())
foundNode, err := subRootNode.Query(c.Key)
if err != nil {
var valueNotFound = &ValueNotFound{}
if errors.As(err, &valueNotFound) {
return false, nil
}
var unsupportedType = &UnsupportedTypeForSelector{}
if errors.As(err, &unsupportedType) {
return false, nil
}
return false, fmt.Errorf("subquery failed: %w", err)
}
return c.check(foundNode.InterfaceValue(), c.Value)
}