]>
Commit | Line | Data |
---|---|---|
bae9f6d2 JC |
1 | package ast |
2 | ||
3 | import "fmt" | |
4 | ||
5 | func VariableListElementTypesAreHomogenous(variableName string, list []Variable) (Type, error) { | |
6 | if len(list) == 0 { | |
7 | return TypeInvalid, fmt.Errorf("list %q does not have any elements so cannot determine type.", variableName) | |
8 | } | |
9 | ||
10 | elemType := TypeUnknown | |
11 | for _, v := range list { | |
12 | if v.Type == TypeUnknown { | |
13 | continue | |
14 | } | |
15 | ||
16 | if elemType == TypeUnknown { | |
17 | elemType = v.Type | |
18 | continue | |
19 | } | |
20 | ||
21 | if v.Type != elemType { | |
22 | return TypeInvalid, fmt.Errorf( | |
23 | "list %q does not have homogenous types. found %s and then %s", | |
24 | variableName, | |
25 | elemType, v.Type, | |
26 | ) | |
27 | } | |
28 | ||
29 | elemType = v.Type | |
30 | } | |
31 | ||
32 | return elemType, nil | |
33 | } | |
34 | ||
35 | func VariableMapValueTypesAreHomogenous(variableName string, vmap map[string]Variable) (Type, error) { | |
36 | if len(vmap) == 0 { | |
37 | return TypeInvalid, fmt.Errorf("map %q does not have any elements so cannot determine type.", variableName) | |
38 | } | |
39 | ||
40 | elemType := TypeUnknown | |
41 | for _, v := range vmap { | |
42 | if v.Type == TypeUnknown { | |
43 | continue | |
44 | } | |
45 | ||
46 | if elemType == TypeUnknown { | |
47 | elemType = v.Type | |
48 | continue | |
49 | } | |
50 | ||
51 | if v.Type != elemType { | |
52 | return TypeInvalid, fmt.Errorf( | |
53 | "map %q does not have homogenous types. found %s and then %s", | |
54 | variableName, | |
55 | elemType, v.Type, | |
56 | ) | |
57 | } | |
58 | ||
59 | elemType = v.Type | |
60 | } | |
61 | ||
62 | return elemType, nil | |
63 | } |