]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/github.com/zclconf/go-cty/cty/convert/conversion_tuple.go
Upgrade to 0.12
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / zclconf / go-cty / cty / convert / conversion_tuple.go
1 package convert
2
3 import (
4 "github.com/zclconf/go-cty/cty"
5 )
6
7 // conversionTupleToTuple returns a conversion that will make the input
8 // tuple type conform to the output tuple type, if possible.
9 //
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.
12 //
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()
19
20 if len(inEtys) != len(outEtys) {
21 return nil // no conversion is possible
22 }
23
24 elemConvs := make([]conversion, len(inEtys))
25
26 for i, outEty := range outEtys {
27 inEty := inEtys[i]
28
29 if inEty.Equals(outEty) {
30 // No conversion needed, so we can leave this one nil.
31 continue
32 }
33
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.
38 return nil
39 }
40 }
41
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]
48
49 i := 0
50 for it := val.ElementIterator(); it.Next(); i++ {
51 _, val := it.Element()
52 var err error
53
54 *pathStep = cty.IndexStep{
55 Key: cty.NumberIntVal(int64(i)),
56 }
57
58 conv := elemConvs[i]
59 if conv != nil {
60 val, err = conv(val, path)
61 if err != nil {
62 return cty.NilVal, err
63 }
64 }
65
66 elemVals[i] = val
67 }
68
69 return cty.TupleVal(elemVals), nil
70 }
71 }