6 "github.com/hashicorp/terraform/configs/configschema"
7 "github.com/zclconf/go-cty/cty"
10 // SetUnknowns takes a cty.Value, and compares it to the schema setting any null
11 // values which are computed to unknown.
12 func SetUnknowns(val cty.Value, schema *configschema.Block) cty.Value {
17 // If the object was null, we still need to handle the top level attributes
18 // which might be computed, but we don't need to expand the blocks.
20 objMap := map[string]cty.Value{}
22 for name, attr := range schema.Attributes {
25 objMap[name] = cty.UnknownVal(attr.Type)
28 objMap[name] = cty.NullVal(attr.Type)
32 // If this object has no unknown attributes, then we can leave it null.
37 return cty.ObjectVal(objMap)
40 valMap := val.AsValueMap()
41 newVals := make(map[string]cty.Value)
43 for name, attr := range schema.Attributes {
46 if attr.Computed && v.IsNull() {
47 newVals[name] = cty.UnknownVal(attr.Type)
54 for name, blockS := range schema.BlockTypes {
55 blockVal := valMap[name]
56 if blockVal.IsNull() || !blockVal.IsKnown() {
57 newVals[name] = blockVal
61 blockValType := blockVal.Type()
62 blockElementType := blockS.Block.ImpliedType()
64 // This switches on the value type here, so we can correctly switch
65 // between Tuples/Lists and Maps/Objects.
67 case blockS.Nesting == configschema.NestingSingle || blockS.Nesting == configschema.NestingGroup:
68 // NestingSingle is the only exception here, where we treat the
69 // block directly as an object
70 newVals[name] = SetUnknowns(blockVal, &blockS.Block)
72 case blockValType.IsSetType(), blockValType.IsListType(), blockValType.IsTupleType():
73 listVals := blockVal.AsValueSlice()
74 newListVals := make([]cty.Value, 0, len(listVals))
76 for _, v := range listVals {
77 newListVals = append(newListVals, SetUnknowns(v, &blockS.Block))
81 case blockValType.IsSetType():
82 switch len(newListVals) {
84 newVals[name] = cty.SetValEmpty(blockElementType)
86 newVals[name] = cty.SetVal(newListVals)
88 case blockValType.IsListType():
89 switch len(newListVals) {
91 newVals[name] = cty.ListValEmpty(blockElementType)
93 newVals[name] = cty.ListVal(newListVals)
95 case blockValType.IsTupleType():
96 newVals[name] = cty.TupleVal(newListVals)
99 case blockValType.IsMapType(), blockValType.IsObjectType():
100 mapVals := blockVal.AsValueMap()
101 newMapVals := make(map[string]cty.Value)
103 for k, v := range mapVals {
104 newMapVals[k] = SetUnknowns(v, &blockS.Block)
108 case blockValType.IsMapType():
109 switch len(newMapVals) {
111 newVals[name] = cty.MapValEmpty(blockElementType)
113 newVals[name] = cty.MapVal(newMapVals)
115 case blockValType.IsObjectType():
116 if len(newMapVals) == 0 {
117 // We need to populate empty values to make a valid object.
118 for attr, ty := range blockElementType.AttributeTypes() {
119 newMapVals[attr] = cty.NullVal(ty)
122 newVals[name] = cty.ObjectVal(newMapVals)
126 panic(fmt.Sprintf("failed to set unknown values for nested block %q:%#v", name, blockValType))
130 return cty.ObjectVal(newVals)