]>
Commit | Line | Data |
---|---|---|
15c0b25d AP |
1 | package cty |
2 | ||
3 | import ( | |
4 | "fmt" | |
5 | ) | |
6 | ||
7 | // TypeList instances represent specific list types. Each distinct ElementType | |
8 | // creates a distinct, non-equal list type. | |
9 | type typeList struct { | |
10 | typeImplSigil | |
11 | ElementTypeT Type | |
12 | } | |
13 | ||
14 | // List creates a map type with the given element Type. | |
15 | // | |
16 | // List types are CollectionType implementations. | |
17 | func List(elem Type) Type { | |
18 | return Type{ | |
19 | typeList{ | |
20 | ElementTypeT: elem, | |
21 | }, | |
22 | } | |
23 | } | |
24 | ||
25 | // Equals returns true if the other Type is a list whose element type is | |
26 | // equal to that of the receiver. | |
27 | func (t typeList) Equals(other Type) bool { | |
28 | ot, isList := other.typeImpl.(typeList) | |
29 | if !isList { | |
30 | return false | |
31 | } | |
32 | ||
33 | return t.ElementTypeT.Equals(ot.ElementTypeT) | |
34 | } | |
35 | ||
107c1cdb ND |
36 | func (t typeList) FriendlyName(mode friendlyTypeNameMode) string { |
37 | elemName := t.ElementTypeT.friendlyNameMode(mode) | |
38 | if mode == friendlyTypeConstraintName { | |
39 | if t.ElementTypeT == DynamicPseudoType { | |
40 | elemName = "any single type" | |
41 | } | |
42 | } | |
43 | return "list of " + elemName | |
15c0b25d AP |
44 | } |
45 | ||
46 | func (t typeList) ElementType() Type { | |
47 | return t.ElementTypeT | |
48 | } | |
49 | ||
50 | func (t typeList) GoString() string { | |
51 | return fmt.Sprintf("cty.List(%#v)", t.ElementTypeT) | |
52 | } | |
53 | ||
54 | // IsListType returns true if the given type is a list type, regardless of its | |
55 | // element type. | |
56 | func (t Type) IsListType() bool { | |
57 | _, ok := t.typeImpl.(typeList) | |
58 | return ok | |
59 | } | |
60 | ||
61 | // ListElementType is a convenience method that checks if the given type is | |
62 | // a list type, returning a pointer to its element type if so and nil | |
63 | // otherwise. This is intended to allow convenient conditional branches, | |
64 | // like so: | |
65 | // | |
66 | // if et := t.ListElementType(); et != nil { | |
67 | // // Do something with *et | |
68 | // } | |
69 | func (t Type) ListElementType() *Type { | |
70 | if lt, ok := t.typeImpl.(typeList); ok { | |
71 | return <.ElementTypeT | |
72 | } | |
73 | return nil | |
74 | } |