aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/hil/ast/variables_helper.go
blob: 06bd18de2ac2db9b253ceecdb051a9fae06cb2d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
}