github.com/hashicorp/vault/sdk@v0.11.0/helper/hclutil/hcl.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package hclutil 5 6 import ( 7 "fmt" 8 9 multierror "github.com/hashicorp/go-multierror" 10 "github.com/hashicorp/hcl/hcl/ast" 11 ) 12 13 // CheckHCLKeys checks whether the keys in the AST list contains any of the valid keys provided. 14 func CheckHCLKeys(node ast.Node, valid []string) error { 15 var list *ast.ObjectList 16 switch n := node.(type) { 17 case *ast.ObjectList: 18 list = n 19 case *ast.ObjectType: 20 list = n.List 21 default: 22 return fmt.Errorf("cannot check HCL keys of type %T", n) 23 } 24 25 validMap := make(map[string]struct{}, len(valid)) 26 for _, v := range valid { 27 validMap[v] = struct{}{} 28 } 29 30 var result error 31 for _, item := range list.Items { 32 key := item.Keys[0].Token.Value().(string) 33 if _, ok := validMap[key]; !ok { 34 result = multierror.Append(result, fmt.Errorf("invalid key %q on line %d", key, item.Assign.Line)) 35 } 36 } 37 38 return result 39 }