github.com/opentofu/opentofu@v1.7.1/internal/configs/configschema/none_required.go (about)

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