aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/hcl2/hcldec/schema.go
blob: b57bd969209d0e0bd7ca434408d2cbcd937c5af3 (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
package hcldec

import (
	"github.com/hashicorp/hcl2/hcl"
)

// ImpliedSchema returns the *hcl.BodySchema implied by the given specification.
// This is the schema that the Decode function will use internally to
// access the content of a given body.
func ImpliedSchema(spec Spec) *hcl.BodySchema {
	var attrs []hcl.AttributeSchema
	var blocks []hcl.BlockHeaderSchema

	// visitSameBodyChildren walks through the spec structure, calling
	// the given callback for each descendent spec encountered. We are
	// interested in the specs that reference attributes and blocks.
	var visit visitFunc
	visit = func(s Spec) {
		if as, ok := s.(attrSpec); ok {
			attrs = append(attrs, as.attrSchemata()...)
		}

		if bs, ok := s.(blockSpec); ok {
			blocks = append(blocks, bs.blockHeaderSchemata()...)
		}

		s.visitSameBodyChildren(visit)
	}

	visit(spec)

	return &hcl.BodySchema{
		Attributes: attrs,
		Blocks:     blocks,
	}
}