kubeform.dev/terraform-backend-sdk@v0.0.0-20220310143633-45f07fe731c5/configs/util.go (about) 1 package configs 2 3 import ( 4 "github.com/hashicorp/hcl/v2" 5 "github.com/hashicorp/hcl/v2/hclsyntax" 6 ) 7 8 // exprIsNativeQuotedString determines whether the given expression looks like 9 // it's a quoted string in the HCL native syntax. 10 // 11 // This should be used sparingly only for situations where our legacy HCL 12 // decoding would've expected a keyword or reference in quotes but our new 13 // decoding expects the keyword or reference to be provided directly as 14 // an identifier-based expression. 15 func exprIsNativeQuotedString(expr hcl.Expression) bool { 16 _, ok := expr.(*hclsyntax.TemplateExpr) 17 return ok 18 } 19 20 // schemaForOverrides takes a *hcl.BodySchema and produces a new one that is 21 // equivalent except that any required attributes are forced to not be required. 22 // 23 // This is useful for dealing with "override" config files, which are allowed 24 // to omit things that they don't wish to override from the main configuration. 25 // 26 // The returned schema may have some pointers in common with the given schema, 27 // so neither the given schema nor the returned schema should be modified after 28 // using this function in order to avoid confusion. 29 // 30 // Overrides are rarely used, so it's recommended to just create the override 31 // schema on the fly only when it's needed, rather than storing it in a global 32 // variable as we tend to do for a primary schema. 33 func schemaForOverrides(schema *hcl.BodySchema) *hcl.BodySchema { 34 ret := &hcl.BodySchema{ 35 Attributes: make([]hcl.AttributeSchema, len(schema.Attributes)), 36 Blocks: schema.Blocks, 37 } 38 39 for i, attrS := range schema.Attributes { 40 ret.Attributes[i] = attrS 41 ret.Attributes[i].Required = false 42 } 43 44 return ret 45 } 46 47 // schemaWithDynamic takes a *hcl.BodySchema and produces a new one that 48 // is equivalent except that it accepts an additional block type "dynamic" with 49 // a single label, used to recognize usage of the HCL dynamic block extension. 50 func schemaWithDynamic(schema *hcl.BodySchema) *hcl.BodySchema { 51 ret := &hcl.BodySchema{ 52 Attributes: schema.Attributes, 53 Blocks: make([]hcl.BlockHeaderSchema, len(schema.Blocks), len(schema.Blocks)+1), 54 } 55 56 copy(ret.Blocks, schema.Blocks) 57 ret.Blocks = append(ret.Blocks, hcl.BlockHeaderSchema{ 58 Type: "dynamic", 59 LabelNames: []string{"type"}, 60 }) 61 62 return ret 63 }