github.com/kubeshop/testkube@v1.17.23/pkg/tcl/expressionstcl/propertyaccessor.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 "fmt" 13 "strings" 14 15 "github.com/pkg/errors" 16 ) 17 18 type propertyAccessor struct { 19 value Expression 20 path []string 21 } 22 23 func newPropertyAccessor(value Expression, path string) Expression { 24 return &propertyAccessor{value: value, path: strings.Split(path, ".")} 25 } 26 27 func (s *propertyAccessor) Type() Type { 28 return TypeUnknown 29 } 30 31 func (s *propertyAccessor) String() string { 32 return fmt.Sprintf("%s.%s", s.value.SafeString(), strings.Join(s.path, ".")) 33 } 34 35 func (s *propertyAccessor) SafeString() string { 36 return s.String() 37 } 38 39 func (s *propertyAccessor) Template() string { 40 return "{{" + s.String() + "}}" 41 } 42 43 func (s *propertyAccessor) SafeResolve(m ...Machine) (v Expression, changed bool, err error) { 44 if s.value.Static() == nil { 45 s.value, changed, err = s.value.SafeResolve(m...) 46 if !changed || err != nil || s.value.Static() == nil { 47 return s, changed, err 48 } 49 } 50 current := s.value 51 for i := 0; i < len(s.path); i++ { 52 current, err = CallStdFunction("at", current, s.path[i]) 53 if err != nil { 54 return nil, changed, errors.Wrap(err, strings.Join(s.path[:i+1], ".")) 55 } 56 } 57 return current, true, nil 58 } 59 60 func (s *propertyAccessor) Resolve(m ...Machine) (v Expression, err error) { 61 return deepResolve(s, m...) 62 } 63 64 func (s *propertyAccessor) Static() StaticValue { 65 return nil 66 } 67 68 func (s *propertyAccessor) Accessors() map[string]struct{} { 69 return nil 70 } 71 72 func (s *propertyAccessor) Functions() map[string]struct{} { 73 return nil 74 }