forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move checkHCLKeys into hclutil (hashicorp#4749)
- Loading branch information
Showing
6 changed files
with
48 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package hclutil | ||
|
||
import ( | ||
"fmt" | ||
|
||
multierror "github.com/hashicorp/go-multierror" | ||
"github.com/hashicorp/hcl/hcl/ast" | ||
) | ||
|
||
// CheckHCLKeys checks whether the keys in the AST list contains any of the valid keys provided. | ||
func CheckHCLKeys(node ast.Node, valid []string) error { | ||
var list *ast.ObjectList | ||
switch n := node.(type) { | ||
case *ast.ObjectList: | ||
list = n | ||
case *ast.ObjectType: | ||
list = n.List | ||
default: | ||
return fmt.Errorf("cannot check HCL keys of type %T", n) | ||
} | ||
|
||
validMap := make(map[string]struct{}, len(valid)) | ||
for _, v := range valid { | ||
validMap[v] = struct{}{} | ||
} | ||
|
||
var result error | ||
for _, item := range list.Items { | ||
key := item.Keys[0].Token.Value().(string) | ||
if _, ok := validMap[key]; !ok { | ||
result = multierror.Append(result, fmt.Errorf("invalid key %q on line %d", key, item.Assign.Line)) | ||
} | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters