github.com/yandex/pandora@v0.5.32/components/providers/scenario/vs/vs_variables.go (about)

     1  package vs
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/yandex/pandora/components/providers/scenario/templater"
     7  )
     8  
     9  type VariableSourceVariables struct {
    10  	Name      string
    11  	Variables map[string]any
    12  }
    13  
    14  func (v *VariableSourceVariables) GetName() string {
    15  	return v.Name
    16  }
    17  
    18  func (v *VariableSourceVariables) GetVariables() any {
    19  	return v.Variables
    20  }
    21  
    22  func (v *VariableSourceVariables) Init() error {
    23  	return v.recursiveCompute(v.Variables)
    24  }
    25  
    26  func (v *VariableSourceVariables) recursiveCompute(input map[string]any) error {
    27  	var err error
    28  	for key, val := range input {
    29  		switch value := val.(type) {
    30  		case string:
    31  			input[key], err = v.execTemplateFunc(value)
    32  			if err != nil {
    33  				return fmt.Errorf("recursiveCompute for %s err: %w", key, err)
    34  			}
    35  		case map[string]any:
    36  			err := v.recursiveCompute(value)
    37  			if err != nil {
    38  				return fmt.Errorf("recursiveCompute for %s err: %w", key, err)
    39  			}
    40  		case map[string]string:
    41  			for k, vv := range value {
    42  				value[k], err = v.execTemplateFunc(vv)
    43  				if err != nil {
    44  					return fmt.Errorf("recursiveCompute for %s err: %w", key, err)
    45  				}
    46  			}
    47  			input[key] = value
    48  		case []string:
    49  			for i, vv := range value {
    50  				value[i], err = v.execTemplateFunc(vv)
    51  				if err != nil {
    52  					return fmt.Errorf("recursiveCompute for %s err: %w", key, err)
    53  				}
    54  			}
    55  			input[key] = value
    56  		}
    57  	}
    58  	return nil
    59  }
    60  
    61  func (v *VariableSourceVariables) execTemplateFunc(in string) (string, error) {
    62  	fun, args := templater.ParseFunc(in)
    63  	if fun == nil {
    64  		return in, nil
    65  	}
    66  	value, err := templater.ExecTemplateFunc(fun, args)
    67  	if err != nil {
    68  		return "", err
    69  	}
    70  	return value, nil
    71  }