kubeform.dev/terraform-backend-sdk@v0.0.0-20220310143633-45f07fe731c5/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  	if a.NestedType != nil {
    30  		return cty.NullVal(a.NestedType.ImpliedType())
    31  	}
    32  	return cty.NullVal(a.Type)
    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  }