github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/lang/funcs/string.go (about)

     1  package funcs
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  
     7  	"github.com/zclconf/go-cty/cty"
     8  	"github.com/zclconf/go-cty/cty/function"
     9  )
    10  
    11  // StartsWithFunc constructs a function that checks if a string starts with
    12  // a specific prefix using strings.HasPrefix
    13  var StartsWithFunc = function.New(&function.Spec{
    14  	Params: []function.Parameter{
    15  		{
    16  			Name: "str",
    17  			Type: cty.String,
    18  		},
    19  		{
    20  			Name: "prefix",
    21  			Type: cty.String,
    22  		},
    23  	},
    24  	Type: function.StaticReturnType(cty.Bool),
    25  	Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
    26  		str := args[0].AsString()
    27  		prefix := args[1].AsString()
    28  
    29  		if strings.HasPrefix(str, prefix) {
    30  			return cty.True, nil
    31  		}
    32  
    33  		return cty.False, nil
    34  	},
    35  })
    36  
    37  // EndsWithFunc constructs a function that checks if a string ends with
    38  // a specific suffix using strings.HasSuffix
    39  var EndsWithFunc = function.New(&function.Spec{
    40  	Params: []function.Parameter{
    41  		{
    42  			Name: "str",
    43  			Type: cty.String,
    44  		},
    45  		{
    46  			Name: "suffix",
    47  			Type: cty.String,
    48  		},
    49  	},
    50  	Type: function.StaticReturnType(cty.Bool),
    51  	Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
    52  		str := args[0].AsString()
    53  		suffix := args[1].AsString()
    54  
    55  		if strings.HasSuffix(str, suffix) {
    56  			return cty.True, nil
    57  		}
    58  
    59  		return cty.False, nil
    60  	},
    61  })
    62  
    63  // ReplaceFunc constructs a function that searches a given string for another
    64  // given substring, and replaces each occurence with a given replacement string.
    65  var ReplaceFunc = function.New(&function.Spec{
    66  	Params: []function.Parameter{
    67  		{
    68  			Name: "str",
    69  			Type: cty.String,
    70  		},
    71  		{
    72  			Name: "substr",
    73  			Type: cty.String,
    74  		},
    75  		{
    76  			Name: "replace",
    77  			Type: cty.String,
    78  		},
    79  	},
    80  	Type: function.StaticReturnType(cty.String),
    81  	Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
    82  		str := args[0].AsString()
    83  		substr := args[1].AsString()
    84  		replace := args[2].AsString()
    85  
    86  		// We search/replace using a regexp if the string is surrounded
    87  		// in forward slashes.
    88  		if len(substr) > 1 && substr[0] == '/' && substr[len(substr)-1] == '/' {
    89  			re, err := regexp.Compile(substr[1 : len(substr)-1])
    90  			if err != nil {
    91  				return cty.UnknownVal(cty.String), err
    92  			}
    93  
    94  			return cty.StringVal(re.ReplaceAllString(str, replace)), nil
    95  		}
    96  
    97  		return cty.StringVal(strings.Replace(str, substr, replace, -1)), nil
    98  	},
    99  })
   100  
   101  // Replace searches a given string for another given substring,
   102  // and replaces all occurences with a given replacement string.
   103  func Replace(str, substr, replace cty.Value) (cty.Value, error) {
   104  	return ReplaceFunc.Call([]cty.Value{str, substr, replace})
   105  }