github.com/yandex/pandora@v0.5.32/components/providers/scenario/grpc/preprocessor/prepare.go (about)

     1  package preprocessor
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/yandex/pandora/components/guns/grpc/scenario"
     8  	"github.com/yandex/pandora/components/providers/scenario/templater"
     9  	"github.com/yandex/pandora/lib/mp"
    10  )
    11  
    12  type PreprocessorConfig struct {
    13  	Mapping map[string]string
    14  }
    15  
    16  type PreparePreprocessor struct {
    17  	Mapping  map[string]string
    18  	iterator mp.Iterator
    19  }
    20  
    21  func (p *PreparePreprocessor) InitIterator(iterator mp.Iterator) {
    22  	p.iterator = iterator
    23  }
    24  
    25  func (p *PreparePreprocessor) Process(_ *scenario.Call, templateVars map[string]any) (map[string]any, error) {
    26  	if templateVars == nil {
    27  		return nil, errors.New("templateVars must not be nil")
    28  	}
    29  	result := make(map[string]any, len(p.Mapping))
    30  	var (
    31  		val any
    32  		err error
    33  	)
    34  	for k, v := range p.Mapping {
    35  		fun, args := templater.ParseFunc(v)
    36  		if fun != nil {
    37  			val, err = templater.ExecTemplateFuncWithVariables(fun, args, templateVars, p.iterator)
    38  		} else {
    39  			val, err = mp.GetMapValue(templateVars, v, p.iterator)
    40  		}
    41  		if err != nil {
    42  			return nil, fmt.Errorf("failed to get value for %s: %w", k, err)
    43  		}
    44  		result[k] = val
    45  	}
    46  	return result, nil
    47  }