github.com/hashicorp/hcl/v2@v2.20.0/hcldec/schema.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package hcldec 5 6 import ( 7 "github.com/hashicorp/hcl/v2" 8 ) 9 10 // ImpliedSchema returns the *hcl.BodySchema implied by the given specification. 11 // This is the schema that the Decode function will use internally to 12 // access the content of a given body. 13 func ImpliedSchema(spec Spec) *hcl.BodySchema { 14 var attrs []hcl.AttributeSchema 15 var blocks []hcl.BlockHeaderSchema 16 17 // visitSameBodyChildren walks through the spec structure, calling 18 // the given callback for each descendent spec encountered. We are 19 // interested in the specs that reference attributes and blocks. 20 var visit visitFunc 21 visit = func(s Spec) { 22 if as, ok := s.(attrSpec); ok { 23 attrs = append(attrs, as.attrSchemata()...) 24 } 25 26 if bs, ok := s.(blockSpec); ok { 27 blocks = append(blocks, bs.blockHeaderSchemata()...) 28 } 29 30 s.visitSameBodyChildren(visit) 31 } 32 33 visit(spec) 34 35 return &hcl.BodySchema{ 36 Attributes: attrs, 37 Blocks: blocks, 38 } 39 }