-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
implied_type.go
268 lines (236 loc) · 7.11 KB
/
implied_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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package yaml
import (
"errors"
"fmt"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/convert"
)
func (c *Converter) impliedType(src []byte) (cty.Type, error) {
p := &yaml_parser_t{}
if !yaml_parser_initialize(p) {
return cty.NilType, errors.New("failed to initialize YAML parser")
}
if len(src) == 0 {
src = []byte{'\n'}
}
an := &typeAnalysis{
anchorsPending: map[string]int{},
anchorTypes: map[string]cty.Type{},
}
yaml_parser_set_input_string(p, src)
var evt yaml_event_t
if !yaml_parser_parse(p, &evt) {
return cty.NilType, parserError(p)
}
if evt.typ != yaml_STREAM_START_EVENT {
return cty.NilType, parseEventErrorf(&evt, "missing stream start token")
}
if !yaml_parser_parse(p, &evt) {
return cty.NilType, parserError(p)
}
if evt.typ != yaml_DOCUMENT_START_EVENT {
return cty.NilType, parseEventErrorf(&evt, "missing start of document")
}
ty, err := c.impliedTypeParse(an, p)
if err != nil {
return cty.NilType, err
}
if !yaml_parser_parse(p, &evt) {
return cty.NilType, parserError(p)
}
if evt.typ == yaml_DOCUMENT_START_EVENT {
return cty.NilType, parseEventErrorf(&evt, "only a single document is allowed")
}
if evt.typ != yaml_DOCUMENT_END_EVENT {
return cty.NilType, parseEventErrorf(&evt, "unexpected extra content (%s) after value", evt.typ.String())
}
if !yaml_parser_parse(p, &evt) {
return cty.NilType, parserError(p)
}
if evt.typ != yaml_STREAM_END_EVENT {
return cty.NilType, parseEventErrorf(&evt, "unexpected extra content after value")
}
return ty, err
}
func (c *Converter) impliedTypeParse(an *typeAnalysis, p *yaml_parser_t) (cty.Type, error) {
var evt yaml_event_t
if !yaml_parser_parse(p, &evt) {
return cty.NilType, parserError(p)
}
return c.impliedTypeParseRemainder(an, &evt, p)
}
func (c *Converter) impliedTypeParseRemainder(an *typeAnalysis, evt *yaml_event_t, p *yaml_parser_t) (cty.Type, error) {
switch evt.typ {
case yaml_SCALAR_EVENT:
return c.impliedTypeScalar(an, evt, p)
case yaml_ALIAS_EVENT:
return c.impliedTypeAlias(an, evt, p)
case yaml_MAPPING_START_EVENT:
return c.impliedTypeMapping(an, evt, p)
case yaml_SEQUENCE_START_EVENT:
return c.impliedTypeSequence(an, evt, p)
case yaml_DOCUMENT_START_EVENT:
return cty.NilType, parseEventErrorf(evt, "only a single document is allowed")
case yaml_STREAM_END_EVENT:
// Decoding an empty buffer, probably
return cty.NilType, parseEventErrorf(evt, "expecting value but found end of stream")
default:
// Should never happen; the above should be comprehensive
return cty.NilType, parseEventErrorf(evt, "unexpected parser event %s", evt.typ.String())
}
}
func (c *Converter) impliedTypeScalar(an *typeAnalysis, evt *yaml_event_t, p *yaml_parser_t) (cty.Type, error) {
src := evt.value
tag := string(evt.tag)
anchor := string(evt.anchor)
implicit := evt.implicit
if len(anchor) > 0 {
an.beginAnchor(anchor)
}
var ty cty.Type
switch {
case tag == "" && !implicit:
// Untagged explicit string
ty = cty.String
default:
v, err := c.resolveScalar(tag, string(src), yaml_scalar_style_t(evt.style))
if err != nil {
return cty.NilType, parseEventErrorWrap(evt, err)
}
if v.RawEquals(mergeMappingVal) {
// In any context other than a mapping key, this is just a plain string
ty = cty.String
} else {
ty = v.Type()
}
}
if len(anchor) > 0 {
an.completeAnchor(anchor, ty)
}
return ty, nil
}
func (c *Converter) impliedTypeMapping(an *typeAnalysis, evt *yaml_event_t, p *yaml_parser_t) (cty.Type, error) {
tag := string(evt.tag)
anchor := string(evt.anchor)
if tag != "" && tag != yaml_MAP_TAG {
return cty.NilType, parseEventErrorf(evt, "can't interpret mapping as %s", tag)
}
if anchor != "" {
an.beginAnchor(anchor)
}
atys := make(map[string]cty.Type)
for {
var nextEvt yaml_event_t
if !yaml_parser_parse(p, &nextEvt) {
return cty.NilType, parserError(p)
}
if nextEvt.typ == yaml_MAPPING_END_EVENT {
ty := cty.Object(atys)
if anchor != "" {
an.completeAnchor(anchor, ty)
}
return ty, nil
}
if nextEvt.typ != yaml_SCALAR_EVENT {
return cty.NilType, parseEventErrorf(&nextEvt, "only strings are allowed as mapping keys")
}
keyVal, err := c.resolveScalar(string(nextEvt.tag), string(nextEvt.value), yaml_scalar_style_t(nextEvt.style))
if err != nil {
return cty.NilType, err
}
if keyVal.RawEquals(mergeMappingVal) {
// Merging the value (which must be a mapping) into our mapping,
// then.
ty, err := c.impliedTypeParse(an, p)
if err != nil {
return cty.NilType, err
}
if !ty.IsObjectType() {
return cty.NilType, parseEventErrorf(&nextEvt, "cannot merge %s into mapping", ty.FriendlyName())
}
for name, aty := range ty.AttributeTypes() {
atys[name] = aty
}
continue
}
if keyValStr, err := convert.Convert(keyVal, cty.String); err == nil {
keyVal = keyValStr
} else {
return cty.NilType, parseEventErrorf(&nextEvt, "only strings are allowed as mapping keys")
}
if keyVal.IsNull() {
return cty.NilType, parseEventErrorf(&nextEvt, "mapping key cannot be null")
}
if !keyVal.IsKnown() {
return cty.NilType, parseEventErrorf(&nextEvt, "mapping key must be known")
}
valTy, err := c.impliedTypeParse(an, p)
if err != nil {
return cty.NilType, err
}
atys[keyVal.AsString()] = valTy
}
}
func (c *Converter) impliedTypeSequence(an *typeAnalysis, evt *yaml_event_t, p *yaml_parser_t) (cty.Type, error) {
tag := string(evt.tag)
anchor := string(evt.anchor)
if tag != "" && tag != yaml_SEQ_TAG {
return cty.NilType, parseEventErrorf(evt, "can't interpret sequence as %s", tag)
}
if anchor != "" {
an.beginAnchor(anchor)
}
var atys []cty.Type
for {
var nextEvt yaml_event_t
if !yaml_parser_parse(p, &nextEvt) {
return cty.NilType, parserError(p)
}
if nextEvt.typ == yaml_SEQUENCE_END_EVENT {
ty := cty.Tuple(atys)
if anchor != "" {
an.completeAnchor(anchor, ty)
}
return ty, nil
}
valTy, err := c.impliedTypeParseRemainder(an, &nextEvt, p)
if err != nil {
return cty.NilType, err
}
atys = append(atys, valTy)
}
}
func (c *Converter) impliedTypeAlias(an *typeAnalysis, evt *yaml_event_t, p *yaml_parser_t) (cty.Type, error) {
ty, err := an.anchorType(string(evt.anchor))
if err != nil {
err = parseEventErrorWrap(evt, err)
}
return ty, err
}
type typeAnalysis struct {
anchorsPending map[string]int
anchorTypes map[string]cty.Type
}
func (an *typeAnalysis) beginAnchor(name string) {
an.anchorsPending[name]++
}
func (an *typeAnalysis) completeAnchor(name string, ty cty.Type) {
an.anchorsPending[name]--
if an.anchorsPending[name] == 0 {
delete(an.anchorsPending, name)
}
an.anchorTypes[name] = ty
}
func (an *typeAnalysis) anchorType(name string) (cty.Type, error) {
if _, pending := an.anchorsPending[name]; pending {
// YAML normally allows self-referencing structures, but cty cannot
// represent them (it requires all structures to be finite) so we
// must fail here.
return cty.NilType, fmt.Errorf("cannot refer to anchor %q from inside its own definition", name)
}
ty, ok := an.anchorTypes[name]
if !ok {
return cty.NilType, fmt.Errorf("reference to undefined anchor %q", name)
}
return ty, nil
}