aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/terraform/configs/configschema/none_required.go
blob: 0be3b8fa3576af45b31ae31e3291d34c2d54d4be (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package configschema

// NoneRequired returns a deep copy of the receiver with any required
// attributes translated to optional.
func (b *Block) NoneRequired() *Block {
	ret := &Block{}

	if b.Attributes != nil {
		ret.Attributes = make(map[string]*Attribute, len(b.Attributes))
	}
	for name, attrS := range b.Attributes {
		ret.Attributes[name] = attrS.forceOptional()
	}

	if b.BlockTypes != nil {
		ret.BlockTypes = make(map[string]*NestedBlock, len(b.BlockTypes))
	}
	for name, blockS := range b.BlockTypes {
		ret.BlockTypes[name] = blockS.noneRequired()
	}

	return ret
}

func (b *NestedBlock) noneRequired() *NestedBlock {
	ret := *b
	ret.Block = *(ret.Block.NoneRequired())
	ret.MinItems = 0
	ret.MaxItems = 0
	return &ret
}

func (a *Attribute) forceOptional() *Attribute {
	ret := *a
	ret.Optional = true
	ret.Required = false
	return &ret
}