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

     1  package templater
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/yandex/pandora/lib/mp"
     7  )
     8  
     9  var ErrUnsupportedFunctionType = fmt.Errorf("unsupported function type")
    10  
    11  func ExecTemplateFuncWithVariables(fun any, args []string, templateVars map[string]any, iter mp.Iterator) (string, error) {
    12  	a := make([]any, len(args))
    13  	for i := range args {
    14  		v, err := mp.GetMapValue(templateVars, args[i], iter)
    15  		if err == nil {
    16  			a[i] = v
    17  		} else {
    18  			a[i] = args[i]
    19  		}
    20  	}
    21  	switch exec := fun.(type) {
    22  	case func() (string, error):
    23  		return exec()
    24  	case func(args ...any) (string, error):
    25  		return exec(a...)
    26  	}
    27  	return "", ErrUnsupportedFunctionType
    28  }
    29  
    30  func ExecTemplateFunc(fun any, args []string) (string, error) {
    31  	a := make([]any, len(args))
    32  	for i := range args {
    33  		a[i] = args[i]
    34  	}
    35  	switch exec := fun.(type) {
    36  	case func() (string, error):
    37  		return exec()
    38  	case func(args ...any) (string, error):
    39  		return exec(a...)
    40  	}
    41  	return "", ErrUnsupportedFunctionType
    42  }