diff options
Diffstat (limited to 'vendor/github.com/zclconf/go-cty/cty/primitive_type.go')
-rw-r--r-- | vendor/github.com/zclconf/go-cty/cty/primitive_type.go | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/vendor/github.com/zclconf/go-cty/cty/primitive_type.go b/vendor/github.com/zclconf/go-cty/cty/primitive_type.go new file mode 100644 index 0000000..b8682dd --- /dev/null +++ b/vendor/github.com/zclconf/go-cty/cty/primitive_type.go | |||
@@ -0,0 +1,122 @@ | |||
1 | package cty | ||
2 | |||
3 | import "math/big" | ||
4 | |||
5 | // primitiveType is the hidden implementation of the various primitive types | ||
6 | // that are exposed as variables in this package. | ||
7 | type primitiveType struct { | ||
8 | typeImplSigil | ||
9 | Kind primitiveTypeKind | ||
10 | } | ||
11 | |||
12 | type primitiveTypeKind byte | ||
13 | |||
14 | const ( | ||
15 | primitiveTypeBool primitiveTypeKind = 'B' | ||
16 | primitiveTypeNumber primitiveTypeKind = 'N' | ||
17 | primitiveTypeString primitiveTypeKind = 'S' | ||
18 | ) | ||
19 | |||
20 | func (t primitiveType) Equals(other Type) bool { | ||
21 | if otherP, ok := other.typeImpl.(primitiveType); ok { | ||
22 | return otherP.Kind == t.Kind | ||
23 | } | ||
24 | return false | ||
25 | } | ||
26 | |||
27 | func (t primitiveType) FriendlyName() string { | ||
28 | switch t.Kind { | ||
29 | case primitiveTypeBool: | ||
30 | return "bool" | ||
31 | case primitiveTypeNumber: | ||
32 | return "number" | ||
33 | case primitiveTypeString: | ||
34 | return "string" | ||
35 | default: | ||
36 | // should never happen | ||
37 | panic("invalid primitive type") | ||
38 | } | ||
39 | } | ||
40 | |||
41 | func (t primitiveType) GoString() string { | ||
42 | switch t.Kind { | ||
43 | case primitiveTypeBool: | ||
44 | return "cty.Bool" | ||
45 | case primitiveTypeNumber: | ||
46 | return "cty.Number" | ||
47 | case primitiveTypeString: | ||
48 | return "cty.String" | ||
49 | default: | ||
50 | // should never happen | ||
51 | panic("invalid primitive type") | ||
52 | } | ||
53 | } | ||
54 | |||
55 | // Number is the numeric type. Number values are arbitrary-precision | ||
56 | // decimal numbers, which can then be converted into Go's various numeric | ||
57 | // types only if they are in the appropriate range. | ||
58 | var Number Type | ||
59 | |||
60 | // String is the string type. String values are sequences of unicode codepoints | ||
61 | // encoded internally as UTF-8. | ||
62 | var String Type | ||
63 | |||
64 | // Bool is the boolean type. The two values of this type are True and False. | ||
65 | var Bool Type | ||
66 | |||
67 | // True is the truthy value of type Bool | ||
68 | var True Value | ||
69 | |||
70 | // False is the falsey value of type Bool | ||
71 | var False Value | ||
72 | |||
73 | // Zero is a number value representing exactly zero. | ||
74 | var Zero Value | ||
75 | |||
76 | // PositiveInfinity is a Number value representing positive infinity | ||
77 | var PositiveInfinity Value | ||
78 | |||
79 | // NegativeInfinity is a Number value representing negative infinity | ||
80 | var NegativeInfinity Value | ||
81 | |||
82 | func init() { | ||
83 | Number = Type{ | ||
84 | primitiveType{Kind: primitiveTypeNumber}, | ||
85 | } | ||
86 | String = Type{ | ||
87 | primitiveType{Kind: primitiveTypeString}, | ||
88 | } | ||
89 | Bool = Type{ | ||
90 | primitiveType{Kind: primitiveTypeBool}, | ||
91 | } | ||
92 | True = Value{ | ||
93 | ty: Bool, | ||
94 | v: true, | ||
95 | } | ||
96 | False = Value{ | ||
97 | ty: Bool, | ||
98 | v: false, | ||
99 | } | ||
100 | Zero = Value{ | ||
101 | ty: Number, | ||
102 | v: big.NewFloat(0), | ||
103 | } | ||
104 | PositiveInfinity = Value{ | ||
105 | ty: Number, | ||
106 | v: (&big.Float{}).SetInf(false), | ||
107 | } | ||
108 | NegativeInfinity = Value{ | ||
109 | ty: Number, | ||
110 | v: (&big.Float{}).SetInf(true), | ||
111 | } | ||
112 | } | ||
113 | |||
114 | // IsPrimitiveType returns true if and only if the reciever is a primitive | ||
115 | // type, which means it's either number, string, or bool. Any two primitive | ||
116 | // types can be safely compared for equality using the standard == operator | ||
117 | // without panic, which is not a guarantee that holds for all types. Primitive | ||
118 | // types can therefore also be used in switch statements. | ||
119 | func (t Type) IsPrimitiveType() bool { | ||
120 | _, ok := t.typeImpl.(primitiveType) | ||
121 | return ok | ||
122 | } | ||