| 1 | package convertscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/ |
| 2 | |
| 3 | import ( |
| 4 | "github.com/zclconf/go-cty/ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/" |
| 5 | ) |
| 6 | |
| 7 | // conversion is an internal variant of Conversion that carries around |
| 8 | // a cty.Path to be used in error responses. |
| 9 | type conversionscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/conversion# func(ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Valuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#, ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Pathscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Path#) (ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Valuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#, error) |
| 10 | |
| 11 | func getConversionscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/getConversion().(in ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#, out ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#, unsafe bool) conversionscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/conversion# { |
| 12 | conv := getConversionKnownscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/getConversionKnown().(in, out, unsafe) |
| 13 | if conv == nil { |
| 14 | return nil |
| 15 | } |
| 16 | |
| 17 | // Wrap the conversion in some standard checks that we don't want to |
| 18 | // have to repeat in every conversion function. |
| 19 | var ret conversionscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/conversion# |
| 20 | ret = func(in ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Valuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#, path ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Pathscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Path#) (ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Valuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#, error) { |
| 21 | if in.IsMarkedscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#IsMarked().() { |
| 22 | // We must unmark during the conversion and then re-apply the |
| 23 | // same marks to the result. |
| 24 | in, inMarks := in.Unmarkscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#Unmark().() |
| 25 | v, err := ret(in, path) |
| 26 | if v != ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.NilValscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/NilVal. { |
| 27 | v = v.WithMarksscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#WithMarks().(inMarks) |
| 28 | } |
| 29 | return v, err |
| 30 | } |
| 31 | |
| 32 | if out == ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.DynamicPseudoTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/DynamicPseudoType. { |
| 33 | // Conversion to DynamicPseudoType always just passes through verbatim. |
| 34 | return in, nil |
| 35 | } |
| 36 | if isKnown, isNull := in.IsKnownscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#IsKnown().(), in.IsNullscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#IsNull().(); !isKnown || isNull { |
| 37 | // Avoid constructing unknown or null values with types which |
| 38 | // include optional attributes. Known or non-null object values |
| 39 | // will be passed to a conversion function which drops the optional |
| 40 | // attributes from the type. Unknown and null pass through values |
| 41 | // must do the same to ensure that homogeneous collections have a |
| 42 | // single element type. |
| 43 | out = out.WithoutOptionalAttributesDeepscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#WithoutOptionalAttributesDeep().() |
| 44 | |
| 45 | if !isKnown { |
| 46 | return ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.UnknownValscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/UnknownVal().(dynamicReplacescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/dynamicReplace().(in.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#Type().(), out)), nil |
| 47 | } |
| 48 | |
| 49 | if isNull { |
| 50 | // We'll pass through nulls, albeit type converted, and let |
| 51 | // the caller deal with whatever handling they want to do in |
| 52 | // case null values are considered valid in some applications. |
| 53 | return ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.NullValscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/NullVal().(dynamicReplacescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/dynamicReplace().(in.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#Type().(), out)), nil |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return conv(in, path) |
| 58 | } |
| 59 | |
| 60 | return ret |
| 61 | } |
| 62 | |
| 63 | func getConversionKnownscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/getConversionKnown().(in ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#, out ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#, unsafe bool) conversionscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/conversion# { |
| 64 | switch { |
| 65 | |
| 66 | case out == ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.DynamicPseudoTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/DynamicPseudoType.: |
| 67 | // Conversion *to* DynamicPseudoType means that the caller wishes |
| 68 | // to allow any type in this position, so we'll produce a do-nothing |
| 69 | // conversion that just passes through the value as-is. |
| 70 | return dynamicPassthroughscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/dynamicPassthrough(). |
| 71 | |
| 72 | case unsafe && in == ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.DynamicPseudoTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/DynamicPseudoType.: |
| 73 | // Conversion *from* DynamicPseudoType means that we have a value |
| 74 | // whose type isn't yet known during type checking. For these we will |
| 75 | // assume that conversion will succeed and deal with any errors that |
| 76 | // result (which is why we can only do this when "unsafe" is set). |
| 77 | return dynamicFixupscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/dynamicFixup().(out) |
| 78 | |
| 79 | case in.IsPrimitiveTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsPrimitiveType().() && out.IsPrimitiveTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsPrimitiveType().(): |
| 80 | conv := primitiveConversionsSafescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/primitiveConversionsSafe.[in][out] |
| 81 | if conv != nil { |
| 82 | return conv |
| 83 | } |
| 84 | if unsafe { |
| 85 | return primitiveConversionsUnsafescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/primitiveConversionsUnsafe.[in][out] |
| 86 | } |
| 87 | return nil |
| 88 | |
| 89 | case out.IsObjectTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsObjectType().() && in.IsObjectTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsObjectType().(): |
| 90 | return conversionObjectToObjectscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/conversionObjectToObject().(in, out, unsafe) |
| 91 | |
| 92 | case out.IsTupleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsTupleType().() && in.IsTupleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsTupleType().(): |
| 93 | return conversionTupleToTuplescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/conversionTupleToTuple().(in, out, unsafe) |
| 94 | |
| 95 | case out.IsListTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsListType().() && (in.IsListTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsListType().() || in.IsSetTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsSetType().()): |
| 96 | inEty := in.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().() |
| 97 | outEty := out.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().() |
| 98 | if inEty.Equalsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#Equals().(outEty) { |
| 99 | // This indicates that we're converting from list to set with |
| 100 | // the same element type, so we don't need an element converter. |
| 101 | return conversionCollectionToListscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/conversionCollectionToList().(outEty, nil) |
| 102 | } |
| 103 | |
| 104 | convEty := getConversionscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/getConversion().(inEty, outEty, unsafe) |
| 105 | if convEty == nil { |
| 106 | return nil |
| 107 | } |
| 108 | return conversionCollectionToListscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/conversionCollectionToList().(outEty, convEty) |
| 109 | |
| 110 | case out.IsSetTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsSetType().() && (in.IsListTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsListType().() || in.IsSetTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsSetType().()): |
| 111 | if in.IsListTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsListType().() && !unsafe { |
| 112 | // Conversion from list to map is unsafe because it will lose |
| 113 | // information: the ordering will not be preserved, and any |
| 114 | // duplicate elements will be conflated. |
| 115 | return nil |
| 116 | } |
| 117 | inEty := in.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().() |
| 118 | outEty := out.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().() |
| 119 | convEty := getConversionscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/getConversion().(inEty, outEty, unsafe) |
| 120 | if inEty.Equalsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#Equals().(outEty) { |
| 121 | // This indicates that we're converting from set to list with |
| 122 | // the same element type, so we don't need an element converter. |
| 123 | return conversionCollectionToSetscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/conversionCollectionToSet().(outEty, nil) |
| 124 | } |
| 125 | |
| 126 | if convEty == nil { |
| 127 | return nil |
| 128 | } |
| 129 | return conversionCollectionToSetscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/conversionCollectionToSet().(outEty, convEty) |
| 130 | |
| 131 | case out.IsMapTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsMapType().() && in.IsMapTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsMapType().(): |
| 132 | inEty := in.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().() |
| 133 | outEty := out.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().() |
| 134 | convEty := getConversionscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/getConversion().(inEty, outEty, unsafe) |
| 135 | if convEty == nil { |
| 136 | return nil |
| 137 | } |
| 138 | return conversionCollectionToMapscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/conversionCollectionToMap().(outEty, convEty) |
| 139 | |
| 140 | case out.IsListTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsListType().() && in.IsTupleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsTupleType().(): |
| 141 | outEty := out.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().() |
| 142 | return conversionTupleToListscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/conversionTupleToList().(in, outEty, unsafe) |
| 143 | |
| 144 | case out.IsSetTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsSetType().() && in.IsTupleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsTupleType().(): |
| 145 | outEty := out.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().() |
| 146 | return conversionTupleToSetscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/conversionTupleToSet().(in, outEty, unsafe) |
| 147 | |
| 148 | case out.IsMapTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsMapType().() && in.IsObjectTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsObjectType().(): |
| 149 | outEty := out.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().() |
| 150 | return conversionObjectToMapscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/conversionObjectToMap().(in, outEty, unsafe) |
| 151 | |
| 152 | case out.IsObjectTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsObjectType().() && in.IsMapTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsMapType().(): |
| 153 | if !unsafe { |
| 154 | // Converting a map to an object is an "unsafe" conversion, |
| 155 | // because we don't know if all the map keys will correspond to |
| 156 | // object attributes. |
| 157 | return nil |
| 158 | } |
| 159 | return conversionMapToObjectscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/conversionMapToObject().(in, out, unsafe) |
| 160 | |
| 161 | case in.IsCapsuleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsCapsuleType().() || out.IsCapsuleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsCapsuleType().(): |
| 162 | if !unsafe { |
| 163 | // Capsule types can only participate in "unsafe" conversions, |
| 164 | // because we don't know enough about their conversion behaviors |
| 165 | // to be sure that they will always be safe. |
| 166 | return nil |
| 167 | } |
| 168 | if in.Equalsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#Equals().(out) { |
| 169 | // conversion to self is never allowed |
| 170 | return nil |
| 171 | } |
| 172 | if out.IsCapsuleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsCapsuleType().() { |
| 173 | if fn := out.CapsuleOpsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#CapsuleOps().().ConversionToscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/CapsuleOps#ConversionTo.; fn != nil { |
| 174 | return conversionToCapsulescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/conversionToCapsule().(in, out, fn) |
| 175 | } |
| 176 | } |
| 177 | if in.IsCapsuleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsCapsuleType().() { |
| 178 | if fn := in.CapsuleOpsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#CapsuleOps().().ConversionFromscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/CapsuleOps#ConversionFrom.; fn != nil { |
| 179 | return conversionFromCapsulescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/conversionFromCapsule().(in, out, fn) |
| 180 | } |
| 181 | } |
| 182 | // No conversion operation is available, then. |
| 183 | return nil |
| 184 | |
| 185 | default: |
| 186 | return nil |
| 187 | |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // retConversion wraps a conversion (internal type) so it can be returned |
| 192 | // as a Conversion (public type). |
| 193 | func retConversionscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/retConversion().(conv conversionscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/conversion#) Conversionscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/Conversion# { |
| 194 | if conv == nil { |
| 195 | return nil |
| 196 | } |
| 197 | |
| 198 | return func(in ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Valuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#) (ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Valuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#, error) { |
| 199 | return conv(in, ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Pathscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Path#(nil)) |
| 200 | } |
| 201 | } |