github.com/kubeshop/testkube@v1.17.23/pkg/tcl/expressionstcl/negative.go (about)

     1  // Copyright 2024 Testkube.
     2  //
     3  // Licensed as a Testkube Pro file under the Testkube Community
     4  // License (the "License"); you may not use this file except in compliance with
     5  // the License. You may obtain a copy of the License at
     6  //
     7  //     https://github.com/kubeshop/testkube/blob/main/licenses/TCL.txt
     8  
     9  package expressionstcl
    10  
    11  import "fmt"
    12  
    13  type negative struct {
    14  	expr Expression
    15  }
    16  
    17  func newNegative(expr Expression) Expression {
    18  	if expr == nil {
    19  		expr = None
    20  	}
    21  	return &negative{expr: expr}
    22  }
    23  
    24  func (s *negative) Type() Type {
    25  	return TypeBool
    26  }
    27  
    28  func (s *negative) String() string {
    29  	return fmt.Sprintf("!%s", s.expr.SafeString())
    30  }
    31  
    32  func (s *negative) SafeString() string {
    33  	return s.String()
    34  }
    35  
    36  func (s *negative) Template() string {
    37  	return "{{" + s.String() + "}}"
    38  }
    39  
    40  func (s *negative) SafeResolve(m ...Machine) (v Expression, changed bool, err error) {
    41  	s.expr, changed, err = s.expr.SafeResolve(m...)
    42  	if err != nil {
    43  		return nil, changed, err
    44  	}
    45  	st := s.expr.Static()
    46  	if st == nil {
    47  		return s, changed, nil
    48  	}
    49  
    50  	vv, err := st.BoolValue()
    51  	if err != nil {
    52  		return nil, changed, err
    53  	}
    54  	return NewValue(!vv), changed, nil
    55  }
    56  
    57  func (s *negative) Resolve(m ...Machine) (v Expression, err error) {
    58  	return deepResolve(s, m...)
    59  }
    60  
    61  func (s *negative) Static() StaticValue {
    62  	// FIXME: it should get environment to call
    63  	return nil
    64  }
    65  
    66  func (s *negative) Accessors() map[string]struct{} {
    67  	return s.expr.Accessors()
    68  }
    69  
    70  func (s *negative) Functions() map[string]struct{} {
    71  	return s.expr.Functions()
    72  }