github.com/tomwright/dasel@v1.27.3/condition_equal.go (about)

     1  package dasel
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"reflect"
     7  )
     8  
     9  // EqualCondition lets you check for an exact match.
    10  type EqualCondition struct {
    11  	// Key is the key of the value to check against.
    12  	Key string
    13  	// Value is the value we are looking for.
    14  	Value string
    15  	// Not is true if this is a not equal check.
    16  	Not bool
    17  }
    18  
    19  func (c EqualCondition) check(a interface{}, b interface{}) (bool, error) {
    20  	var res = fmt.Sprint(a) == b
    21  	if c.Not {
    22  		res = !res
    23  	}
    24  	return res, nil
    25  }
    26  
    27  // Check checks to see if other contains the required key value pair.
    28  func (c EqualCondition) Check(other reflect.Value) (bool, error) {
    29  	if !other.IsValid() {
    30  		return false, &UnhandledCheckType{Value: nil}
    31  	}
    32  
    33  	value := unwrapValue(other)
    34  
    35  	if c.Key == "value" || c.Key == "." {
    36  		return c.check(value.Interface(), c.Value)
    37  	}
    38  
    39  	subRootNode := New(value.Interface())
    40  	foundNode, err := subRootNode.Query(c.Key)
    41  	if err != nil {
    42  		var valueNotFound = &ValueNotFound{}
    43  		if errors.As(err, &valueNotFound) {
    44  			return false, nil
    45  		}
    46  		var unsupportedType = &UnsupportedTypeForSelector{}
    47  		if errors.As(err, &unsupportedType) {
    48  			return false, nil
    49  		}
    50  
    51  		return false, fmt.Errorf("subquery failed: %w", err)
    52  	}
    53  	return c.check(foundNode.InterfaceValue(), c.Value)
    54  }