go.uber.org/yarpc@v1.72.1/internal/interpolate/types.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package interpolate
    22  
    23  import (
    24  	"bytes"
    25  	"fmt"
    26  	"io"
    27  )
    28  
    29  // We represent the user-defined string as a series of terms. Each term is
    30  // either a literal or a variable. Literals are used as-is and variables are
    31  // resolved using a VariableResolver.
    32  type (
    33  	term interface {
    34  		term()
    35  	}
    36  
    37  	literal string
    38  
    39  	variable struct {
    40  		Name       string
    41  		Default    string
    42  		HasDefault bool
    43  	}
    44  )
    45  
    46  func (literal) term()  {}
    47  func (variable) term() {}
    48  
    49  // VariableResolver resolves the value of a variable specified in the string.
    50  //
    51  // The boolean value indicates whether this variable had a value defined. If a
    52  // variable does not have a value and no default is specified, rendering will
    53  // fail.
    54  type VariableResolver func(name string) (value string, ok bool)
    55  
    56  // String is a string that supports interpolation given some source of
    57  // variable values.
    58  //
    59  // A String can be obtained by calling Parse on a string.
    60  type String []term
    61  
    62  // Render renders and returns the string. The provided VariableResolver will
    63  // be used to determine values for the different variables mentioned in the
    64  // string.
    65  func (s String) Render(resolve VariableResolver) (string, error) {
    66  	var buff bytes.Buffer
    67  	if err := s.RenderTo(&buff, resolve); err != nil {
    68  		return "", err
    69  	}
    70  	return buff.String(), nil
    71  }
    72  
    73  // RenderTo renders the string into the given writer. The provided
    74  // VariableResolver will be used to determine values for the different
    75  // variables mentioned in the string.
    76  func (s String) RenderTo(w io.Writer, resolve VariableResolver) error {
    77  	for _, term := range s {
    78  		var value string
    79  		switch t := term.(type) {
    80  		case literal:
    81  			value = string(t)
    82  		case variable:
    83  			if val, ok := resolve(t.Name); ok {
    84  				value = val
    85  			} else if t.HasDefault {
    86  				value = t.Default
    87  			} else {
    88  				return errUnknownVariable{Name: t.Name}
    89  			}
    90  		}
    91  		if _, err := io.WriteString(w, value); err != nil {
    92  			return err
    93  		}
    94  	}
    95  	return nil
    96  }
    97  
    98  type errUnknownVariable struct{ Name string }
    99  
   100  func (e errUnknownVariable) Error() string {
   101  	return fmt.Sprintf("variable %q does not have a value or a default", e.Name)
   102  }