github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/configs/configschema/empty_value.go (about)

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