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

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