github.com/beauknowssoftware/makehcl@v0.0.0-20200322000747-1b9bb1e1c008/internal/functions/concat.go (about)

     1  package functions
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/zclconf/go-cty/cty"
     7  	"github.com/zclconf/go-cty/cty/function"
     8  )
     9  
    10  func Concat(args []cty.Value, retType cty.Type) (cty.Value, error) {
    11  	if len(args) == 0 {
    12  		return cty.ListValEmpty(cty.List(cty.String)), nil
    13  	}
    14  
    15  	var res []cty.Value
    16  
    17  	for _, a := range args {
    18  		switch {
    19  		case a.CanIterateElements():
    20  			res = append(res, a.AsValueSlice()...)
    21  		case a.Type() == cty.String:
    22  			res = append(res, a)
    23  		default:
    24  			return cty.UnknownVal(cty.List(cty.String)), fmt.Errorf("expected list of strings, got %v", a.Type().FriendlyName())
    25  		}
    26  	}
    27  
    28  	return cty.ListVal(res), nil
    29  }
    30  
    31  var ConcatSpec = function.Spec{
    32  	VarParam: &function.Parameter{Type: cty.DynamicPseudoType},
    33  	Type:     function.StaticReturnType(cty.List(cty.String)),
    34  	Impl:     Concat,
    35  }