github.com/hashicorp/hcl/v2@v2.20.0/ext/dynblock/variables_hcldec.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package dynblock 5 6 import ( 7 "github.com/hashicorp/hcl/v2" 8 "github.com/hashicorp/hcl/v2/hcldec" 9 ) 10 11 // VariablesHCLDec is a wrapper around WalkVariables that uses the given hcldec 12 // specification to automatically drive the recursive walk through nested 13 // blocks in the given body. 14 // 15 // This is a drop-in replacement for hcldec.Variables which is able to treat 16 // blocks of type "dynamic" in the same special way that dynblock.Expand would, 17 // exposing both the variables referenced in the "for_each" and "labels" 18 // arguments and variables used in the nested "content" block. 19 func VariablesHCLDec(body hcl.Body, spec hcldec.Spec) []hcl.Traversal { 20 rootNode := WalkVariables(body) 21 return walkVariablesWithHCLDec(rootNode, spec) 22 } 23 24 // ExpandVariablesHCLDec is like VariablesHCLDec but it includes only the 25 // minimal set of variables required to call Expand, ignoring variables that 26 // are referenced only inside normal block contents. See WalkExpandVariables 27 // for more information. 28 func ExpandVariablesHCLDec(body hcl.Body, spec hcldec.Spec) []hcl.Traversal { 29 rootNode := WalkExpandVariables(body) 30 return walkVariablesWithHCLDec(rootNode, spec) 31 } 32 33 func walkVariablesWithHCLDec(node WalkVariablesNode, spec hcldec.Spec) []hcl.Traversal { 34 vars, children := node.Visit(hcldec.ImpliedSchema(spec)) 35 36 if len(children) > 0 { 37 childSpecs := hcldec.ChildBlockTypes(spec) 38 for _, child := range children { 39 if childSpec, exists := childSpecs[child.BlockTypeName]; exists { 40 vars = append(vars, walkVariablesWithHCLDec(child.Node, childSpec)...) 41 } 42 } 43 } 44 45 return vars 46 }