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