8 "github.com/zclconf/go-cty/cty"
9 "github.com/zclconf/go-cty/cty/convert"
10 "github.com/zclconf/go-cty/cty/function"
11 "github.com/zclconf/go-cty/cty/function/stdlib"
12 "github.com/zclconf/go-cty/cty/gocty"
15 var ElementFunc = function.New(&function.Spec{
16 Params: []function.Parameter{
19 Type: cty.DynamicPseudoType,
26 Type: func(args []cty.Value) (cty.Type, error) {
30 case listTy.IsListType():
31 return listTy.ElementType(), nil
32 case listTy.IsTupleType():
33 if !args[1].IsKnown() {
34 // If the index isn't known yet then we can't predict the
35 // result type since each tuple element can have its own type.
36 return cty.DynamicPseudoType, nil
39 etys := listTy.TupleElementTypes()
41 err := gocty.FromCtyValue(args[1], &index)
43 // e.g. fractional number where whole number is required
44 return cty.DynamicPseudoType, fmt.Errorf("invalid index: %s", err)
47 return cty.DynamicPseudoType, errors.New("cannot use element function with an empty list")
49 index = index % len(etys)
50 return etys[index], nil
52 return cty.DynamicPseudoType, fmt.Errorf("cannot read elements from %s", listTy.FriendlyName())
55 Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
57 err := gocty.FromCtyValue(args[1], &index)
59 // can't happen because we checked this in the Type function above
60 return cty.DynamicVal, fmt.Errorf("invalid index: %s", err)
63 if !args[0].IsKnown() {
64 return cty.UnknownVal(retType), nil
67 l := args[0].LengthInt()
69 return cty.DynamicVal, errors.New("cannot use element function with an empty list")
73 // We did all the necessary type checks in the type function above,
74 // so this is guaranteed not to fail.
75 return args[0].Index(cty.NumberIntVal(int64(index))), nil
79 var LengthFunc = function.New(&function.Spec{
80 Params: []function.Parameter{
83 Type: cty.DynamicPseudoType,
84 AllowDynamicType: true,
88 Type: func(args []cty.Value) (cty.Type, error) {
89 collTy := args[0].Type()
91 case collTy == cty.String || collTy.IsTupleType() || collTy.IsObjectType() || collTy.IsListType() || collTy.IsMapType() || collTy.IsSetType() || collTy == cty.DynamicPseudoType:
92 return cty.Number, nil
94 return cty.Number, errors.New("argument must be a string, a collection type, or a structural type")
97 Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
99 collTy := args[0].Type()
101 case collTy == cty.DynamicPseudoType:
102 return cty.UnknownVal(cty.Number), nil
103 case collTy.IsTupleType():
104 l := len(collTy.TupleElementTypes())
105 return cty.NumberIntVal(int64(l)), nil
106 case collTy.IsObjectType():
107 l := len(collTy.AttributeTypes())
108 return cty.NumberIntVal(int64(l)), nil
109 case collTy == cty.String:
110 // We'll delegate to the cty stdlib strlen function here, because
111 // it deals with all of the complexities of tokenizing unicode
112 // grapheme clusters.
113 return stdlib.Strlen(coll)
114 case collTy.IsListType() || collTy.IsSetType() || collTy.IsMapType():
115 return coll.Length(), nil
117 // Should never happen, because of the checks in our Type func above
118 return cty.UnknownVal(cty.Number), errors.New("impossible value type for length(...)")
123 // CoalesceFunc constructs a function that takes any number of arguments and
124 // returns the first one that isn't empty. This function was copied from go-cty
125 // stdlib and modified so that it returns the first *non-empty* non-null element
126 // from a sequence, instead of merely the first non-null.
127 var CoalesceFunc = function.New(&function.Spec{
128 Params: []function.Parameter{},
129 VarParam: &function.Parameter{
131 Type: cty.DynamicPseudoType,
133 AllowDynamicType: true,
136 Type: func(args []cty.Value) (ret cty.Type, err error) {
137 argTypes := make([]cty.Type, len(args))
138 for i, val := range args {
139 argTypes[i] = val.Type()
141 retType, _ := convert.UnifyUnsafe(argTypes)
142 if retType == cty.NilType {
143 return cty.NilType, errors.New("all arguments must have the same type")
147 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
148 for _, argVal := range args {
149 // We already know this will succeed because of the checks in our Type func above
150 argVal, _ = convert.Convert(argVal, retType)
151 if !argVal.IsKnown() {
152 return cty.UnknownVal(retType), nil
157 if retType == cty.String && argVal.RawEquals(cty.StringVal("")) {
163 return cty.NilVal, errors.New("no non-null, non-empty-string arguments")
167 // CoalesceListFunc constructs a function that takes any number of list arguments
168 // and returns the first one that isn't empty.
169 var CoalesceListFunc = function.New(&function.Spec{
170 Params: []function.Parameter{},
171 VarParam: &function.Parameter{
173 Type: cty.DynamicPseudoType,
175 AllowDynamicType: true,
178 Type: func(args []cty.Value) (ret cty.Type, err error) {
180 return cty.NilType, errors.New("at least one argument is required")
183 argTypes := make([]cty.Type, len(args))
185 for i, arg := range args {
186 // if any argument is unknown, we can't be certain know which type we will return
188 return cty.DynamicPseudoType, nil
192 if !ty.IsListType() && !ty.IsTupleType() {
193 return cty.NilType, errors.New("coalescelist arguments must be lists or tuples")
196 argTypes[i] = arg.Type()
200 // If there are mixed types, we have to return a dynamic type.
201 for _, next := range argTypes[1:] {
202 if !next.Equals(last) {
203 return cty.DynamicPseudoType, nil
209 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
210 for _, arg := range args {
212 // If we run into an unknown list at some point, we can't
213 // predict the final result yet. (If there's a known, non-empty
214 // arg before this then we won't get here.)
215 return cty.UnknownVal(retType), nil
218 if arg.LengthInt() > 0 {
223 return cty.NilVal, errors.New("no non-null arguments")
227 // CompactFunc constructs a function that takes a list of strings and returns a new list
228 // with any empty string elements removed.
229 var CompactFunc = function.New(&function.Spec{
230 Params: []function.Parameter{
233 Type: cty.List(cty.String),
236 Type: function.StaticReturnType(cty.List(cty.String)),
237 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
239 if !listVal.IsWhollyKnown() {
240 // If some of the element values aren't known yet then we
241 // can't yet return a compacted list
242 return cty.UnknownVal(retType), nil
245 var outputList []cty.Value
247 for it := listVal.ElementIterator(); it.Next(); {
249 if v.AsString() == "" {
252 outputList = append(outputList, v)
255 if len(outputList) == 0 {
256 return cty.ListValEmpty(cty.String), nil
259 return cty.ListVal(outputList), nil
263 // ContainsFunc constructs a function that determines whether a given list or
264 // set contains a given single value as one of its elements.
265 var ContainsFunc = function.New(&function.Spec{
266 Params: []function.Parameter{
269 Type: cty.DynamicPseudoType,
273 Type: cty.DynamicPseudoType,
276 Type: function.StaticReturnType(cty.Bool),
277 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
281 if !ty.IsListType() && !ty.IsTupleType() && !ty.IsSetType() {
282 return cty.NilVal, errors.New("argument must be list, tuple, or set")
285 _, err = Index(cty.TupleVal(arg.AsValueSlice()), args[1])
287 return cty.False, nil
294 // IndexFunc constructs a function that finds the element index for a given value in a list.
295 var IndexFunc = function.New(&function.Spec{
296 Params: []function.Parameter{
299 Type: cty.DynamicPseudoType,
303 Type: cty.DynamicPseudoType,
306 Type: function.StaticReturnType(cty.Number),
307 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
308 if !(args[0].Type().IsListType() || args[0].Type().IsTupleType()) {
309 return cty.NilVal, errors.New("argument must be a list or tuple")
312 if !args[0].IsKnown() {
313 return cty.UnknownVal(cty.Number), nil
316 if args[0].LengthInt() == 0 { // Easy path
317 return cty.NilVal, errors.New("cannot search an empty list")
320 for it := args[0].ElementIterator(); it.Next(); {
322 eq, err := stdlib.Equal(v, args[1])
324 return cty.NilVal, err
327 return cty.UnknownVal(cty.Number), nil
333 return cty.NilVal, errors.New("item not found")
338 // DistinctFunc constructs a function that takes a list and returns a new list
339 // with any duplicate elements removed.
340 var DistinctFunc = function.New(&function.Spec{
341 Params: []function.Parameter{
344 Type: cty.List(cty.DynamicPseudoType),
347 Type: func(args []cty.Value) (cty.Type, error) {
348 return args[0].Type(), nil
350 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
353 if !listVal.IsWhollyKnown() {
354 return cty.UnknownVal(retType), nil
358 for it := listVal.ElementIterator(); it.Next(); {
360 list, err = appendIfMissing(list, v)
362 return cty.NilVal, err
366 return cty.ListVal(list), nil
370 // ChunklistFunc constructs a function that splits a single list into fixed-size chunks,
371 // returning a list of lists.
372 var ChunklistFunc = function.New(&function.Spec{
373 Params: []function.Parameter{
376 Type: cty.List(cty.DynamicPseudoType),
383 Type: func(args []cty.Value) (cty.Type, error) {
384 return cty.List(args[0].Type()), nil
386 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
388 if !listVal.IsKnown() {
389 return cty.UnknownVal(retType), nil
393 err = gocty.FromCtyValue(args[1], &size)
395 return cty.NilVal, fmt.Errorf("invalid index: %s", err)
399 return cty.NilVal, errors.New("the size argument must be positive")
402 output := make([]cty.Value, 0)
404 // if size is 0, returns a list made of the initial list
406 output = append(output, listVal)
407 return cty.ListVal(output), nil
410 chunk := make([]cty.Value, 0)
412 l := args[0].LengthInt()
415 for it := listVal.ElementIterator(); it.Next(); {
417 chunk = append(chunk, v)
419 // Chunk when index isn't 0, or when reaching the values's length
420 if (i+1)%size == 0 || (i+1) == l {
421 output = append(output, cty.ListVal(chunk))
422 chunk = make([]cty.Value, 0)
427 return cty.ListVal(output), nil
431 // FlattenFunc constructs a function that takes a list and replaces any elements
432 // that are lists with a flattened sequence of the list contents.
433 var FlattenFunc = function.New(&function.Spec{
434 Params: []function.Parameter{
437 Type: cty.DynamicPseudoType,
440 Type: func(args []cty.Value) (cty.Type, error) {
441 if !args[0].IsWhollyKnown() {
442 return cty.DynamicPseudoType, nil
445 argTy := args[0].Type()
446 if !argTy.IsListType() && !argTy.IsSetType() && !argTy.IsTupleType() {
447 return cty.NilType, errors.New("can only flatten lists, sets and tuples")
450 retVal, known := flattener(args[0])
452 return cty.DynamicPseudoType, nil
455 tys := make([]cty.Type, len(retVal))
456 for i, ty := range retVal {
459 return cty.Tuple(tys), nil
461 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
463 if inputList.LengthInt() == 0 {
464 return cty.EmptyTupleVal, nil
467 out, known := flattener(inputList)
469 return cty.UnknownVal(retType), nil
472 return cty.TupleVal(out), nil
476 // Flatten until it's not a cty.List, and return whether the value is known.
477 // We can flatten lists with unknown values, as long as they are not
479 func flattener(flattenList cty.Value) ([]cty.Value, bool) {
480 out := make([]cty.Value, 0)
481 for it := flattenList.ElementIterator(); it.Next(); {
482 _, val := it.Element()
483 if val.Type().IsListType() || val.Type().IsSetType() || val.Type().IsTupleType() {
488 res, known := flattener(val)
492 out = append(out, res...)
494 out = append(out, val)
500 // KeysFunc constructs a function that takes a map and returns a sorted list of the map keys.
501 var KeysFunc = function.New(&function.Spec{
502 Params: []function.Parameter{
505 Type: cty.DynamicPseudoType,
509 Type: func(args []cty.Value) (cty.Type, error) {
513 return cty.List(cty.String), nil
514 case ty.IsObjectType():
515 atys := ty.AttributeTypes()
517 return cty.EmptyTuple, nil
519 // All of our result elements will be strings, and atys just
520 // decides how many there are.
521 etys := make([]cty.Type, len(atys))
522 for i := range etys {
525 return cty.Tuple(etys), nil
527 return cty.DynamicPseudoType, function.NewArgErrorf(0, "must have map or object type")
530 Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
535 case m.Type().IsObjectType():
536 // In this case we allow unknown values so we must work only with
537 // the attribute _types_, not with the value itself.
539 for name := range m.Type().AttributeTypes() {
540 names = append(names, name)
542 sort.Strings(names) // same ordering guaranteed by cty's ElementIterator
544 return cty.EmptyTupleVal, nil
546 keys = make([]cty.Value, len(names))
547 for i, name := range names {
548 keys[i] = cty.StringVal(name)
550 return cty.TupleVal(keys), nil
553 return cty.UnknownVal(retType), nil
556 // cty guarantees that ElementIterator will iterate in lexicographical
558 for it := args[0].ElementIterator(); it.Next(); {
560 keys = append(keys, k)
563 return cty.ListValEmpty(cty.String), nil
565 return cty.ListVal(keys), nil
570 // ListFunc constructs a function that takes an arbitrary number of arguments
571 // and returns a list containing those values in the same order.
573 // This function is deprecated in Terraform v0.12
574 var ListFunc = function.New(&function.Spec{
575 Params: []function.Parameter{},
576 VarParam: &function.Parameter{
578 Type: cty.DynamicPseudoType,
580 AllowDynamicType: true,
583 Type: func(args []cty.Value) (ret cty.Type, err error) {
585 return cty.NilType, errors.New("at least one argument is required")
588 argTypes := make([]cty.Type, len(args))
590 for i, arg := range args {
591 argTypes[i] = arg.Type()
594 retType, _ := convert.UnifyUnsafe(argTypes)
595 if retType == cty.NilType {
596 return cty.NilType, errors.New("all arguments must have the same type")
599 return cty.List(retType), nil
601 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
602 newList := make([]cty.Value, 0, len(args))
604 for _, arg := range args {
605 // We already know this will succeed because of the checks in our Type func above
606 arg, _ = convert.Convert(arg, retType.ElementType())
607 newList = append(newList, arg)
610 return cty.ListVal(newList), nil
614 // LookupFunc constructs a function that performs dynamic lookups of map types.
615 var LookupFunc = function.New(&function.Spec{
616 Params: []function.Parameter{
619 Type: cty.DynamicPseudoType,
626 VarParam: &function.Parameter{
628 Type: cty.DynamicPseudoType,
630 AllowDynamicType: true,
633 Type: func(args []cty.Value) (ret cty.Type, err error) {
634 if len(args) < 1 || len(args) > 3 {
635 return cty.NilType, fmt.Errorf("lookup() takes two or three arguments, got %d", len(args))
641 case ty.IsObjectType():
642 if !args[1].IsKnown() {
643 return cty.DynamicPseudoType, nil
646 key := args[1].AsString()
647 if ty.HasAttribute(key) {
648 return args[0].GetAttr(key).Type(), nil
649 } else if len(args) == 3 {
650 // if the key isn't found but a default is provided,
651 // return the default type
652 return args[2].Type(), nil
654 return cty.DynamicPseudoType, function.NewArgErrorf(0, "the given object has no attribute %q", key)
656 return ty.ElementType(), nil
658 return cty.NilType, function.NewArgErrorf(0, "lookup() requires a map as the first argument")
661 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
662 var defaultVal cty.Value
663 defaultValueSet := false
667 defaultValueSet = true
671 lookupKey := args[1].AsString()
673 if !mapVar.IsWhollyKnown() {
674 return cty.UnknownVal(retType), nil
677 if mapVar.Type().IsObjectType() {
678 if mapVar.Type().HasAttribute(lookupKey) {
679 return mapVar.GetAttr(lookupKey), nil
681 } else if mapVar.HasIndex(cty.StringVal(lookupKey)) == cty.True {
682 v := mapVar.Index(cty.StringVal(lookupKey))
683 if ty := v.Type(); !ty.Equals(cty.NilType) {
685 case ty.Equals(cty.String):
686 return cty.StringVal(v.AsString()), nil
687 case ty.Equals(cty.Number):
688 return cty.NumberVal(v.AsBigFloat()), nil
690 return cty.NilVal, errors.New("lookup() can only be used with flat lists")
696 defaultVal, err = convert.Convert(defaultVal, retType)
698 return cty.NilVal, err
700 return defaultVal, nil
703 return cty.UnknownVal(cty.DynamicPseudoType), fmt.Errorf(
704 "lookup failed to find '%s'", lookupKey)
708 // MapFunc constructs a function that takes an even number of arguments and
709 // returns a map whose elements are constructed from consecutive pairs of arguments.
711 // This function is deprecated in Terraform v0.12
712 var MapFunc = function.New(&function.Spec{
713 Params: []function.Parameter{},
714 VarParam: &function.Parameter{
716 Type: cty.DynamicPseudoType,
718 AllowDynamicType: true,
721 Type: func(args []cty.Value) (ret cty.Type, err error) {
722 if len(args) < 2 || len(args)%2 != 0 {
723 return cty.NilType, fmt.Errorf("map requires an even number of two or more arguments, got %d", len(args))
726 argTypes := make([]cty.Type, len(args)/2)
729 for i := 0; i < len(args); i += 2 {
730 argTypes[index] = args[i+1].Type()
734 valType, _ := convert.UnifyUnsafe(argTypes)
735 if valType == cty.NilType {
736 return cty.NilType, errors.New("all arguments must have the same type")
739 return cty.Map(valType), nil
741 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
742 for _, arg := range args {
743 if !arg.IsWhollyKnown() {
744 return cty.UnknownVal(retType), nil
748 outputMap := make(map[string]cty.Value)
750 for i := 0; i < len(args); i += 2 {
752 key := args[i].AsString()
754 err := gocty.FromCtyValue(args[i], &key)
756 return cty.NilVal, err
761 var variable cty.Value
762 err = gocty.FromCtyValue(val, &variable)
764 return cty.NilVal, err
767 // We already know this will succeed because of the checks in our Type func above
768 variable, _ = convert.Convert(variable, retType.ElementType())
770 // Check for duplicate keys
771 if _, ok := outputMap[key]; ok {
772 return cty.NilVal, fmt.Errorf("argument %d is a duplicate key: %q", i+1, key)
774 outputMap[key] = variable
777 return cty.MapVal(outputMap), nil
781 // MatchkeysFunc constructs a function that constructs a new list by taking a
782 // subset of elements from one list whose indexes match the corresponding
783 // indexes of values in another list.
784 var MatchkeysFunc = function.New(&function.Spec{
785 Params: []function.Parameter{
788 Type: cty.List(cty.DynamicPseudoType),
792 Type: cty.List(cty.DynamicPseudoType),
796 Type: cty.List(cty.DynamicPseudoType),
799 Type: func(args []cty.Value) (cty.Type, error) {
800 if !args[1].Type().Equals(args[2].Type()) {
801 return cty.NilType, errors.New("lists must be of the same type")
804 return args[0].Type(), nil
806 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
807 if !args[0].IsKnown() {
808 return cty.UnknownVal(cty.List(retType.ElementType())), nil
811 if args[0].LengthInt() != args[1].LengthInt() {
812 return cty.ListValEmpty(retType.ElementType()), errors.New("length of keys and values should be equal")
815 output := make([]cty.Value, 0)
821 // if searchset is empty, return an empty list.
822 if searchset.LengthInt() == 0 {
823 return cty.ListValEmpty(retType.ElementType()), nil
826 if !values.IsWhollyKnown() || !keys.IsWhollyKnown() {
827 return cty.UnknownVal(retType), nil
831 for it := keys.ElementIterator(); it.Next(); {
832 _, key := it.Element()
833 for iter := searchset.ElementIterator(); iter.Next(); {
834 _, search := iter.Element()
835 eq, err := stdlib.Equal(key, search)
837 return cty.NilVal, err
840 return cty.ListValEmpty(retType.ElementType()), nil
843 v := values.Index(cty.NumberIntVal(int64(i)))
844 output = append(output, v)
851 // if we haven't matched any key, then output is an empty list.
852 if len(output) == 0 {
853 return cty.ListValEmpty(retType.ElementType()), nil
855 return cty.ListVal(output), nil
859 // MergeFunc constructs a function that takes an arbitrary number of maps and
860 // returns a single map that contains a merged set of elements from all of the maps.
862 // If more than one given map defines the same key then the one that is later in
863 // the argument sequence takes precedence.
864 var MergeFunc = function.New(&function.Spec{
865 Params: []function.Parameter{},
866 VarParam: &function.Parameter{
868 Type: cty.DynamicPseudoType,
869 AllowDynamicType: true,
872 Type: function.StaticReturnType(cty.DynamicPseudoType),
873 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
874 outputMap := make(map[string]cty.Value)
876 for _, arg := range args {
877 if !arg.IsWhollyKnown() {
878 return cty.UnknownVal(retType), nil
880 if !arg.Type().IsObjectType() && !arg.Type().IsMapType() {
881 return cty.NilVal, fmt.Errorf("arguments must be maps or objects, got %#v", arg.Type().FriendlyName())
883 for it := arg.ElementIterator(); it.Next(); {
885 outputMap[k.AsString()] = v
888 return cty.ObjectVal(outputMap), nil
892 // ReverseFunc takes a sequence and produces a new sequence of the same length
893 // with all of the same elements as the given sequence but in reverse order.
894 var ReverseFunc = function.New(&function.Spec{
895 Params: []function.Parameter{
898 Type: cty.DynamicPseudoType,
901 Type: func(args []cty.Value) (cty.Type, error) {
902 argTy := args[0].Type()
904 case argTy.IsTupleType():
905 argTys := argTy.TupleElementTypes()
906 retTys := make([]cty.Type, len(argTys))
907 for i, ty := range argTys {
908 retTys[len(retTys)-i-1] = ty
910 return cty.Tuple(retTys), nil
911 case argTy.IsListType(), argTy.IsSetType(): // We accept sets here to mimic the usual behavior of auto-converting to list
912 return cty.List(argTy.ElementType()), nil
914 return cty.NilType, function.NewArgErrorf(0, "can only reverse list or tuple values, not %s", argTy.FriendlyName())
917 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
918 in := args[0].AsValueSlice()
919 outVals := make([]cty.Value, len(in))
920 for i, v := range in {
921 outVals[len(outVals)-i-1] = v
924 case retType.IsTupleType():
925 return cty.TupleVal(outVals), nil
927 if len(outVals) == 0 {
928 return cty.ListValEmpty(retType.ElementType()), nil
930 return cty.ListVal(outVals), nil
935 // SetProductFunc calculates the cartesian product of two or more sets or
936 // sequences. If the arguments are all lists then the result is a list of tuples,
937 // preserving the ordering of all of the input lists. Otherwise the result is a
939 var SetProductFunc = function.New(&function.Spec{
940 Params: []function.Parameter{},
941 VarParam: &function.Parameter{
943 Type: cty.DynamicPseudoType,
945 Type: func(args []cty.Value) (retType cty.Type, err error) {
947 return cty.NilType, errors.New("at least two arguments are required")
951 elemTys := make([]cty.Type, len(args))
952 for i, arg := range args {
955 case aty.IsSetType():
956 elemTys[i] = aty.ElementType()
957 case aty.IsListType():
958 elemTys[i] = aty.ElementType()
960 case aty.IsTupleType():
961 // We can accept a tuple type only if there's some common type
962 // that all of its elements can be converted to.
963 allEtys := aty.TupleElementTypes()
964 if len(allEtys) == 0 {
965 elemTys[i] = cty.DynamicPseudoType
969 ety, _ := convert.UnifyUnsafe(allEtys)
970 if ety == cty.NilType {
971 return cty.NilType, function.NewArgErrorf(i, "all elements must be of the same type")
976 return cty.NilType, function.NewArgErrorf(i, "a set or a list is required")
980 if listCount == len(args) {
981 return cty.List(cty.Tuple(elemTys)), nil
983 return cty.Set(cty.Tuple(elemTys)), nil
985 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
986 ety := retType.ElementType()
989 for _, arg := range args {
990 // Because of our type checking function, we are guaranteed that
991 // all of the arguments are known, non-null values of types that
992 // support LengthInt.
993 total *= arg.LengthInt()
997 // If any of the arguments was an empty collection then our result
998 // is also an empty collection, which we'll short-circuit here.
999 if retType.IsListType() {
1000 return cty.ListValEmpty(ety), nil
1002 return cty.SetValEmpty(ety), nil
1005 subEtys := ety.TupleElementTypes()
1006 product := make([][]cty.Value, total)
1008 b := make([]cty.Value, total*len(args))
1009 n := make([]int, len(args))
1011 argVals := make([][]cty.Value, len(args))
1012 for i, arg := range args {
1013 argVals[i] = arg.AsValueSlice()
1016 for i := range product {
1022 for j, n := range n {
1023 val := argVals[j][n]
1025 if !val.Type().Equals(ty) {
1027 val, err = convert.Convert(val, ty)
1029 // Should never happen since we checked this in our
1030 // type-checking function.
1031 return cty.NilVal, fmt.Errorf("failed to convert argVals[%d][%d] to %s; this is a bug in Terraform", j, n, ty.FriendlyName())
1037 for j := len(n) - 1; j >= 0; j-- {
1039 if n[j] < len(argVals[j]) {
1046 productVals := make([]cty.Value, total)
1047 for i, vals := range product {
1048 productVals[i] = cty.TupleVal(vals)
1051 if retType.IsListType() {
1052 return cty.ListVal(productVals), nil
1054 return cty.SetVal(productVals), nil
1058 // SliceFunc constructs a function that extracts some consecutive elements
1059 // from within a list.
1060 var SliceFunc = function.New(&function.Spec{
1061 Params: []function.Parameter{
1064 Type: cty.DynamicPseudoType,
1067 Name: "start_index",
1075 Type: func(args []cty.Value) (cty.Type, error) {
1079 if argTy.IsSetType() {
1080 return cty.NilType, function.NewArgErrorf(0, "cannot slice a set, because its elements do not have indices; use the tolist function to force conversion to list if the ordering of the result is not important")
1082 if !argTy.IsListType() && !argTy.IsTupleType() {
1083 return cty.NilType, function.NewArgErrorf(0, "must be a list or tuple value")
1086 startIndex, endIndex, idxsKnown, err := sliceIndexes(args)
1088 return cty.NilType, err
1091 if argTy.IsListType() {
1096 // If we don't know our start/end indices then we can't predict
1097 // the result type if we're planning to return a tuple.
1098 return cty.DynamicPseudoType, nil
1100 return cty.Tuple(argTy.TupleElementTypes()[startIndex:endIndex]), nil
1102 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
1103 inputList := args[0]
1105 if retType == cty.DynamicPseudoType {
1106 return cty.DynamicVal, nil
1109 // we ignore idxsKnown return value here because the indices are always
1110 // known here, or else the call would've short-circuited.
1111 startIndex, endIndex, _, err := sliceIndexes(args)
1113 return cty.NilVal, err
1116 if endIndex-startIndex == 0 {
1117 if retType.IsTupleType() {
1118 return cty.EmptyTupleVal, nil
1120 return cty.ListValEmpty(retType.ElementType()), nil
1123 outputList := inputList.AsValueSlice()[startIndex:endIndex]
1125 if retType.IsTupleType() {
1126 return cty.TupleVal(outputList), nil
1129 return cty.ListVal(outputList), nil
1133 func sliceIndexes(args []cty.Value) (int, int, bool, error) {
1134 var startIndex, endIndex, length int
1135 var startKnown, endKnown, lengthKnown bool
1137 if args[0].Type().IsTupleType() || args[0].IsKnown() { // if it's a tuple then we always know the length by the type, but lists must be known
1138 length = args[0].LengthInt()
1142 if args[1].IsKnown() {
1143 if err := gocty.FromCtyValue(args[1], &startIndex); err != nil {
1144 return 0, 0, false, function.NewArgErrorf(1, "invalid start index: %s", err)
1147 return 0, 0, false, function.NewArgErrorf(1, "start index must not be less than zero")
1149 if lengthKnown && startIndex > length {
1150 return 0, 0, false, function.NewArgErrorf(1, "start index must not be greater than the length of the list")
1154 if args[2].IsKnown() {
1155 if err := gocty.FromCtyValue(args[2], &endIndex); err != nil {
1156 return 0, 0, false, function.NewArgErrorf(2, "invalid end index: %s", err)
1159 return 0, 0, false, function.NewArgErrorf(2, "end index must not be less than zero")
1161 if lengthKnown && endIndex > length {
1162 return 0, 0, false, function.NewArgErrorf(2, "end index must not be greater than the length of the list")
1166 if startKnown && endKnown {
1167 if startIndex > endIndex {
1168 return 0, 0, false, function.NewArgErrorf(1, "start index must not be greater than end index")
1171 return startIndex, endIndex, startKnown && endKnown, nil
1174 // TransposeFunc contructs a function that takes a map of lists of strings and
1175 // TransposeFunc constructs a function that takes a map of lists of strings and
1176 // swaps the keys and values to produce a new map of lists of strings.
1177 var TransposeFunc = function.New(&function.Spec{
1178 Params: []function.Parameter{
1181 Type: cty.Map(cty.List(cty.String)),
1184 Type: function.StaticReturnType(cty.Map(cty.List(cty.String))),
1185 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
1187 if !inputMap.IsWhollyKnown() {
1188 return cty.UnknownVal(retType), nil
1191 outputMap := make(map[string]cty.Value)
1192 tmpMap := make(map[string][]string)
1194 for it := inputMap.ElementIterator(); it.Next(); {
1195 inKey, inVal := it.Element()
1196 for iter := inVal.ElementIterator(); iter.Next(); {
1197 _, val := iter.Element()
1198 if !val.Type().Equals(cty.String) {
1199 return cty.MapValEmpty(cty.List(cty.String)), errors.New("input must be a map of lists of strings")
1202 outKey := val.AsString()
1203 if _, ok := tmpMap[outKey]; !ok {
1204 tmpMap[outKey] = make([]string, 0)
1206 outVal := tmpMap[outKey]
1207 outVal = append(outVal, inKey.AsString())
1208 sort.Strings(outVal)
1209 tmpMap[outKey] = outVal
1213 for outKey, outVal := range tmpMap {
1214 values := make([]cty.Value, 0)
1215 for _, v := range outVal {
1216 values = append(values, cty.StringVal(v))
1218 outputMap[outKey] = cty.ListVal(values)
1221 return cty.MapVal(outputMap), nil
1225 // ValuesFunc constructs a function that returns a list of the map values,
1226 // in the order of the sorted keys.
1227 var ValuesFunc = function.New(&function.Spec{
1228 Params: []function.Parameter{
1231 Type: cty.DynamicPseudoType,
1234 Type: func(args []cty.Value) (ret cty.Type, err error) {
1235 ty := args[0].Type()
1237 return cty.List(ty.ElementType()), nil
1238 } else if ty.IsObjectType() {
1239 // The result is a tuple type with all of the same types as our
1240 // object type's attributes, sorted in lexicographical order by the
1241 // keys. (This matches the sort order guaranteed by ElementIterator
1242 // on a cty object value.)
1243 atys := ty.AttributeTypes()
1245 return cty.EmptyTuple, nil
1247 attrNames := make([]string, 0, len(atys))
1248 for name := range atys {
1249 attrNames = append(attrNames, name)
1251 sort.Strings(attrNames)
1253 tys := make([]cty.Type, len(attrNames))
1254 for i, name := range attrNames {
1257 return cty.Tuple(tys), nil
1259 return cty.NilType, errors.New("values() requires a map as the first argument")
1261 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
1264 // We can just iterate the map/object value here because cty guarantees
1265 // that these types always iterate in key lexicographical order.
1266 var values []cty.Value
1267 for it := mapVar.ElementIterator(); it.Next(); {
1268 _, val := it.Element()
1269 values = append(values, val)
1272 if retType.IsTupleType() {
1273 return cty.TupleVal(values), nil
1275 if len(values) == 0 {
1276 return cty.ListValEmpty(retType.ElementType()), nil
1278 return cty.ListVal(values), nil
1282 // ZipmapFunc constructs a function that constructs a map from a list of keys
1283 // and a corresponding list of values.
1284 var ZipmapFunc = function.New(&function.Spec{
1285 Params: []function.Parameter{
1288 Type: cty.List(cty.String),
1292 Type: cty.DynamicPseudoType,
1295 Type: func(args []cty.Value) (ret cty.Type, err error) {
1298 valuesTy := values.Type()
1301 case valuesTy.IsListType():
1302 return cty.Map(values.Type().ElementType()), nil
1303 case valuesTy.IsTupleType():
1304 if !keys.IsWhollyKnown() {
1305 // Since zipmap with a tuple produces an object, we need to know
1306 // all of the key names before we can predict our result type.
1307 return cty.DynamicPseudoType, nil
1310 keysRaw := keys.AsValueSlice()
1311 valueTypesRaw := valuesTy.TupleElementTypes()
1312 if len(keysRaw) != len(valueTypesRaw) {
1313 return cty.NilType, fmt.Errorf("number of keys (%d) does not match number of values (%d)", len(keysRaw), len(valueTypesRaw))
1315 atys := make(map[string]cty.Type, len(valueTypesRaw))
1316 for i, keyVal := range keysRaw {
1317 if keyVal.IsNull() {
1318 return cty.NilType, fmt.Errorf("keys list has null value at index %d", i)
1320 key := keyVal.AsString()
1321 atys[key] = valueTypesRaw[i]
1323 return cty.Object(atys), nil
1326 return cty.NilType, errors.New("values argument must be a list or tuple value")
1329 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
1333 if !keys.IsWhollyKnown() {
1334 // Unknown map keys and object attributes are not supported, so
1335 // our entire result must be unknown in this case.
1336 return cty.UnknownVal(retType), nil
1339 // both keys and values are guaranteed to be shallowly-known here,
1340 // because our declared params above don't allow unknown or null values.
1341 if keys.LengthInt() != values.LengthInt() {
1342 return cty.NilVal, fmt.Errorf("number of keys (%d) does not match number of values (%d)", keys.LengthInt(), values.LengthInt())
1345 output := make(map[string]cty.Value)
1348 for it := keys.ElementIterator(); it.Next(); {
1349 _, v := it.Element()
1350 val := values.Index(cty.NumberIntVal(int64(i)))
1351 output[v.AsString()] = val
1356 case retType.IsMapType():
1357 if len(output) == 0 {
1358 return cty.MapValEmpty(retType.ElementType()), nil
1360 return cty.MapVal(output), nil
1361 case retType.IsObjectType():
1362 return cty.ObjectVal(output), nil
1364 // Should never happen because the type-check function should've
1365 // caught any other case.
1366 return cty.NilVal, fmt.Errorf("internally selected incorrect result type %s (this is a bug)", retType.FriendlyName())
1371 // helper function to add an element to a list, if it does not already exist
1372 func appendIfMissing(slice []cty.Value, element cty.Value) ([]cty.Value, error) {
1373 for _, ele := range slice {
1374 eq, err := stdlib.Equal(ele, element)
1382 return append(slice, element), nil
1385 // Element returns a single element from a given list at the given index. If
1386 // index is greater than the length of the list then it is wrapped modulo
1388 func Element(list, index cty.Value) (cty.Value, error) {
1389 return ElementFunc.Call([]cty.Value{list, index})
1392 // Length returns the number of elements in the given collection or number of
1393 // Unicode characters in the given string.
1394 func Length(collection cty.Value) (cty.Value, error) {
1395 return LengthFunc.Call([]cty.Value{collection})
1398 // Coalesce takes any number of arguments and returns the first one that isn't empty.
1399 func Coalesce(args ...cty.Value) (cty.Value, error) {
1400 return CoalesceFunc.Call(args)
1403 // CoalesceList takes any number of list arguments and returns the first one that isn't empty.
1404 func CoalesceList(args ...cty.Value) (cty.Value, error) {
1405 return CoalesceListFunc.Call(args)
1408 // Compact takes a list of strings and returns a new list
1409 // with any empty string elements removed.
1410 func Compact(list cty.Value) (cty.Value, error) {
1411 return CompactFunc.Call([]cty.Value{list})
1414 // Contains determines whether a given list contains a given single value
1415 // as one of its elements.
1416 func Contains(list, value cty.Value) (cty.Value, error) {
1417 return ContainsFunc.Call([]cty.Value{list, value})
1420 // Index finds the element index for a given value in a list.
1421 func Index(list, value cty.Value) (cty.Value, error) {
1422 return IndexFunc.Call([]cty.Value{list, value})
1425 // Distinct takes a list and returns a new list with any duplicate elements removed.
1426 func Distinct(list cty.Value) (cty.Value, error) {
1427 return DistinctFunc.Call([]cty.Value{list})
1430 // Chunklist splits a single list into fixed-size chunks, returning a list of lists.
1431 func Chunklist(list, size cty.Value) (cty.Value, error) {
1432 return ChunklistFunc.Call([]cty.Value{list, size})
1435 // Flatten takes a list and replaces any elements that are lists with a flattened
1436 // sequence of the list contents.
1437 func Flatten(list cty.Value) (cty.Value, error) {
1438 return FlattenFunc.Call([]cty.Value{list})
1441 // Keys takes a map and returns a sorted list of the map keys.
1442 func Keys(inputMap cty.Value) (cty.Value, error) {
1443 return KeysFunc.Call([]cty.Value{inputMap})
1446 // List takes any number of list arguments and returns a list containing those
1447 // values in the same order.
1448 func List(args ...cty.Value) (cty.Value, error) {
1449 return ListFunc.Call(args)
1452 // Lookup performs a dynamic lookup into a map.
1453 // There are two required arguments, map and key, plus an optional default,
1454 // which is a value to return if no key is found in map.
1455 func Lookup(args ...cty.Value) (cty.Value, error) {
1456 return LookupFunc.Call(args)
1459 // Map takes an even number of arguments and returns a map whose elements are constructed
1460 // from consecutive pairs of arguments.
1461 func Map(args ...cty.Value) (cty.Value, error) {
1462 return MapFunc.Call(args)
1465 // Matchkeys constructs a new list by taking a subset of elements from one list
1466 // whose indexes match the corresponding indexes of values in another list.
1467 func Matchkeys(values, keys, searchset cty.Value) (cty.Value, error) {
1468 return MatchkeysFunc.Call([]cty.Value{values, keys, searchset})
1471 // Merge takes an arbitrary number of maps and returns a single map that contains
1472 // a merged set of elements from all of the maps.
1474 // If more than one given map defines the same key then the one that is later in
1475 // the argument sequence takes precedence.
1476 func Merge(maps ...cty.Value) (cty.Value, error) {
1477 return MergeFunc.Call(maps)
1480 // Reverse takes a sequence and produces a new sequence of the same length
1481 // with all of the same elements as the given sequence but in reverse order.
1482 func Reverse(list cty.Value) (cty.Value, error) {
1483 return ReverseFunc.Call([]cty.Value{list})
1486 // SetProduct computes the cartesian product of sets or sequences.
1487 func SetProduct(sets ...cty.Value) (cty.Value, error) {
1488 return SetProductFunc.Call(sets)
1491 // Slice extracts some consecutive elements from within a list.
1492 func Slice(list, start, end cty.Value) (cty.Value, error) {
1493 return SliceFunc.Call([]cty.Value{list, start, end})
1496 // Transpose takes a map of lists of strings and swaps the keys and values to
1497 // produce a new map of lists of strings.
1498 func Transpose(values cty.Value) (cty.Value, error) {
1499 return TransposeFunc.Call([]cty.Value{values})
1502 // Values returns a list of the map values, in the order of the sorted keys.
1503 // This function only works on flat maps.
1504 func Values(values cty.Value) (cty.Value, error) {
1505 return ValuesFunc.Call([]cty.Value{values})
1508 // Zipmap constructs a map from a list of keys and a corresponding list of values.
1509 func Zipmap(keys, values cty.Value) (cty.Value, error) {
1510 return ZipmapFunc.Call([]cty.Value{keys, values})