summaryrefslogtreecommitdiff
path: root/vendor/github.com/hashicorp/hil/ast/variables_helper.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/hashicorp/hil/ast/variables_helper.go')
-rw-r--r--vendor/github.com/hashicorp/hil/ast/variables_helper.go63
1 files changed, 63 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/hil/ast/variables_helper.go b/vendor/github.com/hashicorp/hil/ast/variables_helper.go
new file mode 100644
index 00000000..06bd18de
--- /dev/null
+++ b/vendor/github.com/hashicorp/hil/ast/variables_helper.go
@@ -0,0 +1,63 @@
+package ast
+
+import "fmt"
+
+func VariableListElementTypesAreHomogenous(variableName string, list []Variable) (Type, error) {
+ if len(list) == 0 {
+ return TypeInvalid, fmt.Errorf("list %q does not have any elements so cannot determine type.", variableName)
+ }
+
+ elemType := TypeUnknown
+ for _, v := range list {
+ if v.Type == TypeUnknown {
+ continue
+ }
+
+ if elemType == TypeUnknown {
+ elemType = v.Type
+ continue
+ }
+
+ if v.Type != elemType {
+ return TypeInvalid, fmt.Errorf(
+ "list %q does not have homogenous types. found %s and then %s",
+ variableName,
+ elemType, v.Type,
+ )
+ }
+
+ elemType = v.Type
+ }
+
+ return elemType, nil
+}
+
+func VariableMapValueTypesAreHomogenous(variableName string, vmap map[string]Variable) (Type, error) {
+ if len(vmap) == 0 {
+ return TypeInvalid, fmt.Errorf("map %q does not have any elements so cannot determine type.", variableName)
+ }
+
+ elemType := TypeUnknown
+ for _, v := range vmap {
+ if v.Type == TypeUnknown {
+ continue
+ }
+
+ if elemType == TypeUnknown {
+ elemType = v.Type
+ continue
+ }
+
+ if v.Type != elemType {
+ return TypeInvalid, fmt.Errorf(
+ "map %q does not have homogenous types. found %s and then %s",
+ variableName,
+ elemType, v.Type,
+ )
+ }
+
+ elemType = v.Type
+ }
+
+ return elemType, nil
+}