-
Notifications
You must be signed in to change notification settings - Fork 32
/
type.go
236 lines (211 loc) · 5.48 KB
/
type.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tftypes
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
)
// Type is an interface representing a Terraform type. It is only meant to be
// implemented by the tftypes package. Types define the shape and
// characteristics of data coming from or being sent to Terraform.
type Type interface {
// AttributePathStepper requires each Type to implement the
// ApplyTerraform5AttributePathStep method, so Type is compatible with
// WalkAttributePath. The method should return the Type found at that
// AttributePath within the Type or ErrInvalidStep.
AttributePathStepper
// Is is used to determine what type a Type implementation is. It is
// the recommended method for determining whether two types are
// equivalent or not.
// Is performs shallow type equality checks, in that the root type is
// compared, but underlying attribute/element types are not.
Is(Type) bool
// Equal performs deep type equality checks, including attribute/element
// types and whether attributes are optional or not.
Equal(Type) bool
// UsableAs performs type conformance checks. This primarily checks if the
// target implements DynamicPsuedoType in a compatible manner.
UsableAs(Type) bool
// String returns a string representation of the Type's name.
String() string
// MarshalJSON returns a JSON representation of the Type's signature.
// It is modeled based on Terraform's requirements for type signature
// JSON representations, and may change over time to match Terraform's
// formatting. The error return should always be nil.
//
// Deprecated: this is not meant to be called by third-party code.
MarshalJSON() ([]byte, error)
// private is meant to keep this interface from being implemented by
// types from other packages.
private()
// supportedGoTypes returns a list of string representations of the Go
// types that the Type supports for its values.
supportedGoTypes() []string
}
// TypeFromElements returns the common type that the passed elements all have
// in common. An error will be returned if the passed elements are not of the
// same type.
func TypeFromElements(elements []Value) (Type, error) {
var typ Type
for _, el := range elements {
if typ == nil {
typ = el.Type()
continue
}
if !typ.Equal(el.Type()) {
return nil, errors.New("elements do not all have the same types")
}
}
if typ == nil {
return DynamicPseudoType, nil
}
return typ, nil
}
type jsonType struct {
t Type
}
// ParseJSONType returns a Type from its JSON representation. The JSON
// representation should come from Terraform or from MarshalJSON as the format
// is not part of this package's API guarantees.
//
// Deprecated: this is not meant to be called by third-party code.
func ParseJSONType(buf []byte) (Type, error) {
var t jsonType
err := json.Unmarshal(buf, &t)
return t.t, err
}
func (t *jsonType) UnmarshalJSON(buf []byte) error {
r := bytes.NewReader(buf)
dec := json.NewDecoder(r)
tok, err := dec.Token()
if err != nil {
return err
}
switch v := tok.(type) {
case string:
switch v {
case "bool":
t.t = Bool
case "number":
t.t = Number
case "string":
t.t = String
case "dynamic":
t.t = DynamicPseudoType
default:
return fmt.Errorf("invalid primitive type name %q", v)
}
if dec.More() {
return fmt.Errorf("extraneous data after type description")
}
return nil
case json.Delim:
if rune(v) != '[' {
return fmt.Errorf("invalid complex type description")
}
tok, err = dec.Token()
if err != nil {
return err
}
kind, ok := tok.(string)
if !ok {
return fmt.Errorf("invalid complex type kind name")
}
switch kind {
case "list":
var ety jsonType
err = dec.Decode(&ety)
if err != nil {
return err
}
t.t = List{
ElementType: ety.t,
}
case "map":
var ety jsonType
err = dec.Decode(&ety)
if err != nil {
return err
}
t.t = Map{
ElementType: ety.t,
}
case "set":
var ety jsonType
err = dec.Decode(&ety)
if err != nil {
return err
}
t.t = Set{
ElementType: ety.t,
}
case "object":
var atys map[string]jsonType
err = dec.Decode(&atys)
if err != nil {
return err
}
types := make(map[string]Type, len(atys))
for k, v := range atys {
types[k] = v.t
}
o := Object{
AttributeTypes: types,
OptionalAttributes: map[string]struct{}{},
}
if dec.More() {
var optionals []string
err = dec.Decode(&optionals)
if err != nil {
return err
}
for _, attr := range optionals {
o.OptionalAttributes[attr] = struct{}{}
}
}
t.t = o
case "tuple":
var etys []jsonType
err = dec.Decode(&etys)
if err != nil {
return err
}
types := make([]Type, 0, len(etys))
for _, ty := range etys {
types = append(types, ty.t)
}
t.t = Tuple{
ElementTypes: types,
}
default:
return fmt.Errorf("invalid complex type kind name")
}
tok, err = dec.Token()
if err != nil {
return err
}
if delim, ok := tok.(json.Delim); !ok || rune(delim) != ']' || dec.More() {
return fmt.Errorf("unexpected extra data in type description")
}
return nil
default:
return fmt.Errorf("invalid type description")
}
}
func formattedSupportedGoTypes(t Type) string {
sgt := t.supportedGoTypes()
switch len(sgt) {
case 0:
return "no supported Go types"
case 1:
return sgt[0]
case 2:
return sgt[0] + " or " + sgt[1]
default:
sgt[len(sgt)-1] = "or " + sgt[len(sgt)-1]
return strings.Join(sgt, ", ")
}
}