github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/configs/configschema/empty_value.go (about)

     1  package configschema
     2  
     3  import (
     4  	"github.com/zclconf/go-cty/cty"
     5  )
     6  
     7  // EmptyValue returns the "empty value" for the recieving block, which for
     8  // a block type is a non-null object where all of the attribute values are
     9  // the empty values of the block's attributes and nested block types.
    10  //
    11  // In other words, it returns the value that would be returned if an empty
    12  // block were decoded against the recieving schema, assuming that no required
    13  // attribute or block constraints were honored.
    14  func (b *Block) EmptyValue() cty.Value {
    15  	vals := make(map[string]cty.Value)
    16  	for name, attrS := range b.Attributes {
    17  		vals[name] = attrS.EmptyValue()
    18  	}
    19  	for name, blockS := range b.BlockTypes {
    20  		vals[name] = blockS.EmptyValue()
    21  	}
    22  	return cty.ObjectVal(vals)
    23  }
    24  
    25  // EmptyValue returns the "empty value" for the receiving attribute, which is
    26  // the value that would be returned if there were no definition of the attribute
    27  // at all, ignoring any required constraint.
    28  func (a *Attribute) EmptyValue() cty.Value {
    29  	return cty.NullVal(a.Type)
    30  }
    31  
    32  // EmptyValue returns the "empty value" for when there are zero nested blocks
    33  // present of the receiving type.
    34  func (b *NestedBlock) EmptyValue() cty.Value {
    35  	switch b.Nesting {
    36  	case NestingSingle:
    37  		return cty.NullVal(b.Block.ImpliedType())
    38  	case NestingGroup:
    39  		return b.Block.EmptyValue()
    40  	case NestingList:
    41  		if ty := b.Block.ImpliedType(); ty.HasDynamicTypes() {
    42  			return cty.EmptyTupleVal
    43  		} else {
    44  			return cty.ListValEmpty(ty)
    45  		}
    46  	case NestingMap:
    47  		if ty := b.Block.ImpliedType(); ty.HasDynamicTypes() {
    48  			return cty.EmptyObjectVal
    49  		} else {
    50  			return cty.MapValEmpty(ty)
    51  		}
    52  	case NestingSet:
    53  		return cty.SetValEmpty(b.Block.ImpliedType())
    54  	default:
    55  		// Should never get here because the above is intended to be exhaustive,
    56  		// but we'll be robust and return a result nonetheless.
    57  		return cty.NullVal(cty.DynamicPseudoType)
    58  	}
    59  }