terraform/configs/configschema/empty_value.go

63 lines
1.9 KiB
Go
Raw Normal View History

package configschema
import (
"github.com/zclconf/go-cty/cty"
)
// EmptyValue returns the "empty value" for the recieving block, which for
// a block type is a non-null object where all of the attribute values are
// the empty values of the block's attributes and nested block types.
//
// In other words, it returns the value that would be returned if an empty
// block were decoded against the recieving schema, assuming that no required
// attribute or block constraints were honored.
func (b *Block) EmptyValue() cty.Value {
vals := make(map[string]cty.Value)
for name, attrS := range b.Attributes {
vals[name] = attrS.EmptyValue()
}
for name, blockS := range b.BlockTypes {
vals[name] = blockS.EmptyValue()
}
return cty.ObjectVal(vals)
}
// EmptyValue returns the "empty value" for the receiving attribute, which is
// the value that would be returned if there were no definition of the attribute
// at all, ignoring any required constraint.
func (a *Attribute) EmptyValue() cty.Value {
if a.NestedType != nil {
return cty.NullVal(a.NestedType.ImpliedType())
}
return cty.NullVal(a.Type)
}
// EmptyValue returns the "empty value" for when there are zero nested blocks
// present of the receiving type.
func (b *NestedBlock) EmptyValue() cty.Value {
switch b.Nesting {
case NestingSingle:
return cty.NullVal(b.Block.ImpliedType())
configs/configschema: Introduce the NestingGroup mode for blocks In study of existing providers we've found a pattern we werent previously accounting for of using a nested block type to represent a group of arguments that relate to a particular feature that is always enabled but where it improves configuration readability to group all of its settings together in a nested block. The existing NestingSingle was not a good fit for this because it is designed under the assumption that the presence or absence of the block has some significance in enabling or disabling the relevant feature, and so for these always-active cases we'd generate a misleading plan where the settings for the feature appear totally absent, rather than showing the default values that will be selected. NestingGroup is, therefore, a slight variation of NestingSingle where presence vs. absence of the block is not distinguishable (it's never null) and instead its contents are treated as unset when the block is absent. This then in turn causes any default values associated with the nested arguments to be honored and displayed in the plan whenever the block is not explicitly configured. The current SDK cannot activate this mode, but that's okay because its "legacy type system" opt-out flag allows it to force a block to be processed in this way anyway. We're adding this now so that we can introduce the feature in a future SDK without causing a breaking change to the protocol, since the set of possible block nesting modes is not extensible.
2019-04-09 00:32:53 +02:00
case NestingGroup:
return b.Block.EmptyValue()
case NestingList:
if ty := b.Block.ImpliedType(); ty.HasDynamicTypes() {
return cty.EmptyTupleVal
} else {
return cty.ListValEmpty(ty)
}
case NestingMap:
if ty := b.Block.ImpliedType(); ty.HasDynamicTypes() {
return cty.EmptyObjectVal
} else {
return cty.MapValEmpty(ty)
}
case NestingSet:
return cty.SetValEmpty(b.Block.ImpliedType())
default:
// Should never get here because the above is intended to be exhaustive,
// but we'll be robust and return a result nonetheless.
return cty.NullVal(cty.DynamicPseudoType)
}
}