github.com/kubeshop/testkube@v1.17.23/pkg/tcl/expressionstcl/static.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 (
    12  	"encoding/json"
    13  	"strings"
    14  )
    15  
    16  type static struct {
    17  	value interface{}
    18  }
    19  
    20  var none *static
    21  var None StaticValue = none
    22  
    23  func NewValue(value interface{}) StaticValue {
    24  	if value == noneValue {
    25  		return None
    26  	}
    27  	return &static{value: value}
    28  }
    29  
    30  func NewStringValue(value interface{}) StaticValue {
    31  	v, _ := toString(value)
    32  	return NewValue(v)
    33  }
    34  
    35  func (s *static) Type() Type {
    36  	if s == nil {
    37  		return TypeUnknown
    38  	}
    39  	switch s.value.(type) {
    40  	case int64:
    41  		return TypeInt64
    42  	case float64:
    43  		return TypeFloat64
    44  	case string:
    45  		return TypeString
    46  	case bool:
    47  		return TypeBool
    48  	default:
    49  		return TypeUnknown
    50  	}
    51  }
    52  
    53  func (s *static) String() string {
    54  	if s.IsNone() {
    55  		return "null"
    56  	}
    57  	b, _ := json.Marshal(s.value)
    58  	if len(b) == 0 {
    59  		return "null"
    60  	}
    61  	r := string(b)
    62  	if s.IsMap() && r == "null" {
    63  		return "{}"
    64  	}
    65  	if s.IsSlice() && r == "null" {
    66  		return "[]"
    67  	}
    68  	return r
    69  }
    70  
    71  func (s *static) SafeString() string {
    72  	return s.String()
    73  }
    74  
    75  func (s *static) Template() string {
    76  	if s.IsNone() {
    77  		return ""
    78  	}
    79  	v, _ := s.StringValue()
    80  	return strings.ReplaceAll(v, "{{", "{{\"{{\"}}")
    81  }
    82  
    83  func (s *static) SafeResolve(_ ...Machine) (Expression, bool, error) {
    84  	return s, false, nil
    85  }
    86  
    87  func (s *static) Resolve(_ ...Machine) (Expression, error) {
    88  	return s, nil
    89  }
    90  
    91  func (s *static) Static() StaticValue {
    92  	return s
    93  }
    94  
    95  func (s *static) IsNone() bool {
    96  	return s == nil
    97  }
    98  
    99  func (s *static) IsString() bool {
   100  	return !s.IsNone() && isString(s.value)
   101  }
   102  
   103  func (s *static) IsBool() bool {
   104  	return !s.IsNone() && isBool(s.value)
   105  }
   106  
   107  func (s *static) IsInt() bool {
   108  	return !s.IsNone() && isInt(s.value)
   109  }
   110  
   111  func (s *static) IsNumber() bool {
   112  	return !s.IsNone() && isNumber(s.value)
   113  }
   114  
   115  func (s *static) IsMap() bool {
   116  	return !s.IsNone() && (isMap(s.value) || isStruct(s.value))
   117  }
   118  
   119  func (s *static) IsSlice() bool {
   120  	return !s.IsNone() && isSlice(s.value)
   121  }
   122  
   123  func (s *static) Value() interface{} {
   124  	if s.IsNone() {
   125  		return noneValue
   126  	}
   127  	return s.value
   128  }
   129  
   130  func (s *static) StringValue() (string, error) {
   131  	return toString(s.Value())
   132  }
   133  
   134  func (s *static) BoolValue() (bool, error) {
   135  	return toBool(s.Value())
   136  }
   137  
   138  func (s *static) IntValue() (int64, error) {
   139  	return toInt(s.Value())
   140  }
   141  
   142  func (s *static) FloatValue() (float64, error) {
   143  	return toFloat(s.Value())
   144  }
   145  
   146  func (s *static) MapValue() (map[string]interface{}, error) {
   147  	return toMap(s.Value())
   148  }
   149  
   150  func (s *static) SliceValue() ([]interface{}, error) {
   151  	return toSlice(s.Value())
   152  }
   153  
   154  func (s *static) Accessors() map[string]struct{} {
   155  	return nil
   156  }
   157  
   158  func (s *static) Functions() map[string]struct{} {
   159  	return nil
   160  }