2017-09-11 23:00:31 +00:00
|
|
|
package hcldec
|
2017-06-04 00:34:32 +00:00
|
|
|
|
|
|
|
import (
|
2019-09-09 23:08:19 +00:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2017-06-04 00:34:32 +00:00
|
|
|
)
|
|
|
|
|
2017-09-11 23:40:37 +00:00
|
|
|
// ImpliedSchema returns the *hcl.BodySchema implied by the given specification.
|
2017-06-04 00:34:32 +00:00
|
|
|
// This is the schema that the Decode function will use internally to
|
|
|
|
// access the content of a given body.
|
2017-09-11 23:40:37 +00:00
|
|
|
func ImpliedSchema(spec Spec) *hcl.BodySchema {
|
|
|
|
var attrs []hcl.AttributeSchema
|
|
|
|
var blocks []hcl.BlockHeaderSchema
|
2017-06-04 00:34:32 +00:00
|
|
|
|
|
|
|
// 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.
|
2017-10-03 19:13:29 +00:00
|
|
|
var visit visitFunc
|
|
|
|
visit = func(s Spec) {
|
2017-06-04 00:34:32 +00:00
|
|
|
if as, ok := s.(attrSpec); ok {
|
|
|
|
attrs = append(attrs, as.attrSchemata()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if bs, ok := s.(blockSpec); ok {
|
|
|
|
blocks = append(blocks, bs.blockHeaderSchemata()...)
|
|
|
|
}
|
2017-10-03 19:13:29 +00:00
|
|
|
|
|
|
|
s.visitSameBodyChildren(visit)
|
2017-06-04 00:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
visit(spec)
|
|
|
|
|
2017-09-11 23:40:37 +00:00
|
|
|
return &hcl.BodySchema{
|
2017-06-04 00:34:32 +00:00
|
|
|
Attributes: attrs,
|
|
|
|
Blocks: blocks,
|
|
|
|
}
|
|
|
|
}
|