7 "github.com/hashicorp/hil/ast"
8 "github.com/zclconf/go-cty/cty"
10 "github.com/hashicorp/terraform/configs/configschema"
13 // UnknownVariableValue is a sentinel value that can be used
14 // to denote that the value of a variable is unknown at this time.
15 // RawConfig uses this information to build up data about
17 const UnknownVariableValue = "74D93920-ED26-11E3-AC10-0800200C9A66"
19 // ConfigValueFromHCL2Block is like ConfigValueFromHCL2 but it works only for
20 // known object values and uses the provided block schema to perform some
21 // additional normalization to better mimic the shape of value that the old
22 // HCL1/HIL-based codepaths would've produced.
24 // In particular, it discards the collections that we use to represent nested
25 // blocks (other than NestingSingle) if they are empty, which better mimics
26 // the HCL1 behavior because HCL1 had no knowledge of the schema and so didn't
27 // know that an unspecified block _could_ exist.
29 // The given object value must conform to the schema's implied type or this
30 // function will panic or produce incorrect results.
32 // This is primarily useful for the final transition from new-style values to
33 // terraform.ResourceConfig before calling to a legacy provider, since
34 // helper/schema (the old provider SDK) is particularly sensitive to these
35 // subtle differences within its validation code.
36 func ConfigValueFromHCL2Block(v cty.Value, schema *configschema.Block) map[string]interface{} {
41 panic("ConfigValueFromHCL2Block used with unknown value")
43 if !v.Type().IsObjectType() {
44 panic(fmt.Sprintf("ConfigValueFromHCL2Block used with non-object value %#v", v))
47 atys := v.Type().AttributeTypes()
48 ret := make(map[string]interface{})
50 for name := range schema.Attributes {
51 if _, exists := atys[name]; !exists {
57 // Skip nulls altogether, to better mimic how HCL1 would behave
60 ret[name] = ConfigValueFromHCL2(av)
63 for name, blockS := range schema.BlockTypes {
64 if _, exists := atys[name]; !exists {
69 ret[name] = UnknownVariableValue
76 switch blockS.Nesting {
78 case configschema.NestingSingle, configschema.NestingGroup:
79 ret[name] = ConfigValueFromHCL2Block(bv, &blockS.Block)
81 case configschema.NestingList, configschema.NestingSet:
84 // skip empty collections to better mimic how HCL1 would behave
88 elems := make([]interface{}, 0, l)
89 for it := bv.ElementIterator(); it.Next(); {
92 elems = append(elems, UnknownVariableValue)
95 elems = append(elems, ConfigValueFromHCL2Block(ev, &blockS.Block))
99 case configschema.NestingMap:
100 if bv.LengthInt() == 0 {
101 // skip empty collections to better mimic how HCL1 would behave
105 elems := make(map[string]interface{})
106 for it := bv.ElementIterator(); it.Next(); {
107 ek, ev := it.Element()
109 elems[ek.AsString()] = UnknownVariableValue
112 elems[ek.AsString()] = ConfigValueFromHCL2Block(ev, &blockS.Block)
121 // ConfigValueFromHCL2 converts a value from HCL2 (really, from the cty dynamic
122 // types library that HCL2 uses) to a value type that matches what would've
123 // been produced from the HCL-based interpolator for an equivalent structure.
125 // This function will transform a cty null value into a Go nil value, which
126 // isn't a possible outcome of the HCL/HIL-based decoder and so callers may
127 // need to detect and reject any null values.
128 func ConfigValueFromHCL2(v cty.Value) interface{} {
130 return UnknownVariableValue
138 return v.True() // like HCL.BOOL
140 return v.AsString() // like HCL token.STRING or token.HEREDOC
142 // We can't match HCL _exactly_ here because it distinguishes between
143 // int and float values, but we'll get as close as we can by using
144 // an int if the number is exactly representable, and a float if not.
145 // The conversion to float will force precision to that of a float64,
146 // which is potentially losing information from the specific number
147 // given, but no worse than what HCL would've done in its own conversion
151 if i, acc := f.Int64(); acc == big.Exact {
152 // if we're on a 32-bit system and the number is too big for 32-bit
153 // int then we'll fall through here and use a float64.
154 const MaxInt = int(^uint(0) >> 1)
155 const MinInt = -MaxInt - 1
156 if i <= int64(MaxInt) && i >= int64(MinInt) {
157 return int(i) // Like HCL token.NUMBER
161 f64, _ := f.Float64()
162 return f64 // like HCL token.FLOAT
165 if v.Type().IsListType() || v.Type().IsSetType() || v.Type().IsTupleType() {
166 l := make([]interface{}, 0, v.LengthInt())
167 it := v.ElementIterator()
169 _, ev := it.Element()
170 l = append(l, ConfigValueFromHCL2(ev))
175 if v.Type().IsMapType() || v.Type().IsObjectType() {
176 l := make(map[string]interface{})
177 it := v.ElementIterator()
179 ek, ev := it.Element()
180 cv := ConfigValueFromHCL2(ev)
182 l[ek.AsString()] = cv
188 // If we fall out here then we have some weird type that we haven't
189 // accounted for. This should never happen unless the caller is using
190 // capsule types, and we don't currently have any such types defined.
191 panic(fmt.Errorf("can't convert %#v to config value", v))
194 // HCL2ValueFromConfigValue is the opposite of configValueFromHCL2: it takes
195 // a value as would be returned from the old interpolator and turns it into
196 // a cty.Value so it can be used within, for example, an HCL2 EvalContext.
197 func HCL2ValueFromConfigValue(v interface{}) cty.Value {
199 return cty.NullVal(cty.DynamicPseudoType)
201 if v == UnknownVariableValue {
202 return cty.DynamicVal
205 switch tv := v.(type) {
207 return cty.BoolVal(tv)
209 return cty.StringVal(tv)
211 return cty.NumberIntVal(int64(tv))
213 return cty.NumberFloatVal(tv)
215 vals := make([]cty.Value, len(tv))
216 for i, ev := range tv {
217 vals[i] = HCL2ValueFromConfigValue(ev)
219 return cty.TupleVal(vals)
220 case map[string]interface{}:
221 vals := map[string]cty.Value{}
222 for k, ev := range tv {
223 vals[k] = HCL2ValueFromConfigValue(ev)
225 return cty.ObjectVal(vals)
227 // HCL/HIL should never generate anything that isn't caught by
228 // the above, so if we get here something has gone very wrong.
229 panic(fmt.Errorf("can't convert %#v to cty.Value", v))
233 func HILVariableFromHCL2Value(v cty.Value) ast.Variable {
235 // Caller should guarantee/check this before calling
236 panic("Null values cannot be represented in HIL")
240 Type: ast.TypeUnknown,
241 Value: UnknownVariableValue,
252 v := ConfigValueFromHCL2(v)
253 switch tv := v.(type) {
265 // should never happen
266 panic("invalid return value for configValueFromHCL2")
270 Type: ast.TypeString,
275 if v.Type().IsListType() || v.Type().IsSetType() || v.Type().IsTupleType() {
276 l := make([]ast.Variable, 0, v.LengthInt())
277 it := v.ElementIterator()
279 _, ev := it.Element()
280 l = append(l, HILVariableFromHCL2Value(ev))
282 // If we were given a tuple then this could actually produce an invalid
283 // list with non-homogenous types, which we expect to be caught inside
284 // HIL just like a user-supplied non-homogenous list would be.
291 if v.Type().IsMapType() || v.Type().IsObjectType() {
292 l := make(map[string]ast.Variable)
293 it := v.ElementIterator()
295 ek, ev := it.Element()
296 l[ek.AsString()] = HILVariableFromHCL2Value(ev)
298 // If we were given an object then this could actually produce an invalid
299 // map with non-homogenous types, which we expect to be caught inside
300 // HIL just like a user-supplied non-homogenous map would be.
307 // If we fall out here then we have some weird type that we haven't
308 // accounted for. This should never happen unless the caller is using
309 // capsule types, and we don't currently have any such types defined.
310 panic(fmt.Errorf("can't convert %#v to HIL variable", v))
313 func HCL2ValueFromHILVariable(v ast.Variable) cty.Value {
316 vals := make([]cty.Value, len(v.Value.([]ast.Variable)))
317 for i, ev := range v.Value.([]ast.Variable) {
318 vals[i] = HCL2ValueFromHILVariable(ev)
320 return cty.TupleVal(vals)
322 vals := make(map[string]cty.Value, len(v.Value.(map[string]ast.Variable)))
323 for k, ev := range v.Value.(map[string]ast.Variable) {
324 vals[k] = HCL2ValueFromHILVariable(ev)
326 return cty.ObjectVal(vals)
328 return HCL2ValueFromConfigValue(v.Value)
332 func HCL2TypeForHILType(hilType ast.Type) cty.Type {
335 return cty.DynamicPseudoType
336 case ast.TypeUnknown:
337 return cty.DynamicPseudoType
347 return cty.List(cty.DynamicPseudoType)
349 return cty.Map(cty.DynamicPseudoType)
351 return cty.NilType // equilvalent to ast.TypeInvalid