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

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package configschema
     5  
     6  // NoneRequired returns a deep copy of the receiver with any required
     7  // attributes translated to optional.
     8  func (b *Block) NoneRequired() *Block {
     9  	ret := &Block{}
    10  
    11  	if b.Attributes != nil {
    12  		ret.Attributes = make(map[string]*Attribute, len(b.Attributes))
    13  	}
    14  	for name, attrS := range b.Attributes {
    15  		ret.Attributes[name] = attrS.forceOptional()
    16  	}
    17  
    18  	if b.BlockTypes != nil {
    19  		ret.BlockTypes = make(map[string]*NestedBlock, len(b.BlockTypes))
    20  	}
    21  	for name, blockS := range b.BlockTypes {
    22  		ret.BlockTypes[name] = blockS.noneRequired()
    23  	}
    24  
    25  	return ret
    26  }
    27  
    28  func (b *NestedBlock) noneRequired() *NestedBlock {
    29  	ret := *b
    30  	ret.Block = *(ret.Block.NoneRequired())
    31  	ret.MinItems = 0
    32  	ret.MaxItems = 0
    33  	return &ret
    34  }
    35  
    36  func (a *Attribute) forceOptional() *Attribute {
    37  	ret := *a
    38  	ret.Optional = true
    39  	ret.Required = false
    40  	return &ret
    41  }