github.com/Jeffail/benthos/v3@v3.65.0/lib/util/text/util.go (about)

     1  package text
     2  
     3  //------------------------------------------------------------------------------
     4  
     5  // InterpolatedString holds a string that potentially has interpolation
     6  // functions. Each time Get is called any functions are replaced with their
     7  // evaluated results in the string.
     8  type InterpolatedString struct {
     9  	str         string
    10  	strBytes    []byte
    11  	interpolate bool
    12  }
    13  
    14  // Get evaluates functions within the original string and returns the result.
    15  func (i *InterpolatedString) Get(msg Message) string {
    16  	return i.GetFor(msg, 0)
    17  }
    18  
    19  // GetFor evaluates functions within the original string and returns the result,
    20  // evaluated for a specific message part of the message.
    21  func (i *InterpolatedString) GetFor(msg Message, index int) string {
    22  	if !i.interpolate {
    23  		return i.str
    24  	}
    25  	return string(ReplaceFunctionVariablesFor(msg, index, i.strBytes))
    26  }
    27  
    28  // NewInterpolatedString returns a type that evaluates function interpolations
    29  // on a provided string each time Get is called.
    30  func NewInterpolatedString(str string) *InterpolatedString {
    31  	strI := &InterpolatedString{
    32  		str: str,
    33  	}
    34  	if strBytes := []byte(str); ContainsFunctionVariables(strBytes) {
    35  		strI.strBytes = strBytes
    36  		strI.interpolate = true
    37  	}
    38  	return strI
    39  }
    40  
    41  //------------------------------------------------------------------------------
    42  
    43  // InterpolatedBytes holds a byte slice that potentially has interpolation
    44  // functions. Each time Get is called any functions are replaced with their
    45  // evaluated results in the byte slice.
    46  type InterpolatedBytes struct {
    47  	v           []byte
    48  	interpolate bool
    49  }
    50  
    51  // Get evaluates functions within the byte slice and returns the result.
    52  func (i *InterpolatedBytes) Get(msg Message) []byte {
    53  	return i.GetFor(msg, 0)
    54  }
    55  
    56  // GetFor evaluates functions within the byte slice and returns the result,
    57  // evaluated for a specific message part of the message.
    58  func (i *InterpolatedBytes) GetFor(msg Message, index int) []byte {
    59  	if !i.interpolate {
    60  		return i.v
    61  	}
    62  	return ReplaceFunctionVariablesFor(msg, index, i.v)
    63  }
    64  
    65  // NewInterpolatedBytes returns a type that evaluates function interpolations
    66  // on a provided byte slice each time Get is called.
    67  func NewInterpolatedBytes(v []byte) *InterpolatedBytes {
    68  	return &InterpolatedBytes{
    69  		v:           v,
    70  		interpolate: ContainsFunctionVariables(v),
    71  	}
    72  }
    73  
    74  //------------------------------------------------------------------------------