4 "github.com/zclconf/go-cty/cty"
7 // conversionTupleToTuple returns a conversion that will make the input
8 // tuple type conform to the output tuple type, if possible.
10 // Conversion is possible only if the two tuple types have the same number
11 // of elements and the corresponding elements by index can be converted.
13 // Shallow tuple conversions work the same for both safe and unsafe modes,
14 // but the safety flag is passed on to recursive conversions and may thus
15 // limit which element type conversions are possible.
16 func conversionTupleToTuple(in, out cty.Type, unsafe bool) conversion {
17 inEtys := in.TupleElementTypes()
18 outEtys := out.TupleElementTypes()
20 if len(inEtys) != len(outEtys) {
21 return nil // no conversion is possible
24 elemConvs := make([]conversion, len(inEtys))
26 for i, outEty := range outEtys {
29 if inEty.Equals(outEty) {
30 // No conversion needed, so we can leave this one nil.
34 elemConvs[i] = getConversion(inEty, outEty, unsafe)
35 if elemConvs[i] == nil {
36 // If a recursive conversion isn't available, then our top-level
37 // configuration is impossible too.
42 // If we get here then a conversion is possible, using the element
43 // conversions given in elemConvs.
44 return func(val cty.Value, path cty.Path) (cty.Value, error) {
45 elemVals := make([]cty.Value, len(elemConvs))
46 path = append(path, nil)
47 pathStep := &path[len(path)-1]
50 for it := val.ElementIterator(); it.Next(); i++ {
51 _, val := it.Element()
54 *pathStep = cty.IndexStep{
55 Key: cty.NumberIntVal(int64(i)),
60 val, err = conv(val, path)
62 return cty.NilVal, err
69 return cty.TupleVal(elemVals), nil