1
0
mirror of https://github.com/TomWright/dasel.git synced 2022-05-22 02:32:45 +03:00
Files
dasel-data-selector/condition_equal.go
Tom Wright 9eb8a5ab6e Search selector (#43)
Implement search selector
2020-11-16 22:44:28 +00:00

47 lines
1.0 KiB
Go

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
}
// 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 fmt.Sprint(value.Interface()) == c.Value, nil
}
switch value.Kind() {
case reflect.Map, reflect.Slice:
subRootNode := New(value.Interface())
foundNode, err := subRootNode.Query(c.Key)
if err != nil {
var valueNotFound = &ValueNotFound{}
if errors.As(err, &valueNotFound) {
return false, nil
}
return false, fmt.Errorf("subquery failed: %w", err)
}
return fmt.Sprint(foundNode.InterfaceValue()) == c.Value, nil
}
return false, &UnhandledCheckType{Value: value.Kind().String()}
}