forked from crystal-lang/crystal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrom_yaml.cr
362 lines (298 loc) · 8.8 KB
/
from_yaml.cr
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# Deserializes the given YAML in *string_or_io* into
# an instance of `self`. This simply creates an instance of
# `YAML::ParseContext` and invokes `new(parser, yaml)`:
# classes that want to provide YAML deserialization must provide an
# `def initialize(parser : YAML::ParseContext, yaml : string_or_io)`
# method.
#
# ```
# Hash(String, String).from_yaml("{env: production}") # => {"env" => "production"}
# ```
def Object.from_yaml(string_or_io : String | IO)
new(YAML::ParseContext.new, parse_yaml(string_or_io))
end
def Array.from_yaml(string_or_io : String | IO)
new(YAML::ParseContext.new, parse_yaml(string_or_io)) do |element|
yield element
end
end
private def parse_yaml(string_or_io)
document = YAML::Nodes.parse(string_or_io)
# If the document is empty we simulate an empty scalar with
# plain style, that parses to Nil
document.nodes.first? || begin
scalar = YAML::Nodes::Scalar.new("")
scalar.style = YAML::ScalarStyle::PLAIN
scalar
end
end
private def parse_scalar(ctx, node, type : T.class) forall T
ctx.read_alias(node, T) do |obj|
return obj
end
if node.is_a?(YAML::Nodes::Scalar)
value = YAML::Schema::Core.parse_scalar(node)
if value.is_a?(T)
ctx.record_anchor(node, value)
value
else
node.raise "Expected #{T}, not #{node.value.inspect}"
end
else
node.raise "Expected scalar, not #{node.kind}"
end
end
def Nil.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
parse_scalar(ctx, node, self)
end
def Bool.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
parse_scalar(ctx, node, self)
end
{% for type in %w(Int8 Int16 Int32 Int64 UInt8 UInt16 UInt32 UInt64) %}
def {{type.id}}.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
{{type.id}}.new! parse_scalar(ctx, node, Int64)
end
{% end %}
def String.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
ctx.read_alias(node, String) do |obj|
return obj
end
if node.is_a?(YAML::Nodes::Scalar)
value = node.value
ctx.record_anchor(node, value)
value
else
node.raise "Expected String, not #{node.kind}"
end
end
def Path.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
new(String.new(ctx, node))
end
def Float32.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
parse_scalar(ctx, node, Float64).to_f32!
end
def Float64.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
parse_scalar(ctx, node, self)
end
def Array.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
ctx.read_alias(node, self) do |obj|
return obj
end
ary = new
ctx.record_anchor(node, ary)
new(ctx, node) do |element|
ary << element
end
ary
end
def Array.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
unless node.is_a?(YAML::Nodes::Sequence)
node.raise "Expected sequence, not #{node.kind}"
end
node.each do |value|
yield T.new(ctx, value)
end
end
def Set.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
ctx.read_alias(node, self) do |obj|
return obj
end
ary = new
ctx.record_anchor(node, ary)
new(ctx, node) do |element|
ary << element
end
ary
end
def Set.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
unless node.is_a?(YAML::Nodes::Sequence)
node.raise "Expected sequence, not #{node.kind}"
end
node.each do |value|
yield T.new(ctx, value)
end
end
def Hash.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
ctx.read_alias(node, self) do |obj|
return obj
end
hash = new
ctx.record_anchor(node, hash)
new(ctx, node) do |key, value|
hash[key] = value
end
hash
end
def Hash.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
unless node.is_a?(YAML::Nodes::Mapping)
node.raise "Expected mapping, not #{node.kind}"
end
YAML::Schema::Core.each(node) do |key, value|
yield K.new(ctx, key), V.new(ctx, value)
end
end
def Tuple.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
unless node.is_a?(YAML::Nodes::Sequence)
node.raise "Expected sequence, not #{node.kind}"
end
if node.nodes.size != {{T.size}}
node.raise "Expected #{{{T.size}}} elements, not #{node.nodes.size}"
end
{% begin %}
Tuple.new(
{% for i in 0...T.size %}
(self[{{i}}].new(ctx, node.nodes[{{i}}])),
{% end %}
)
{% end %}
end
def NamedTuple.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
unless node.is_a?(YAML::Nodes::Mapping)
node.raise "Expected mapping, not #{node.kind}"
end
{% begin %}
{% for key in T.keys %}
%var{key.id} = nil
%found{key.id} = false
{% end %}
YAML::Schema::Core.each(node) do |key, value|
key = String.new(ctx, key)
case key
{% for key, type in T %}
when {{key.stringify}}
%var{key.id} = {{type}}.new(ctx, value)
%found{key.id} = true
{% end %}
else
# ignore the key
end
end
{% for key, type in T %}
if %var{key.id}.nil? && !%found{key.id} && !{{type.nilable?}}
node.raise "Missing yaml attribute: #{{{key.id.stringify}}}"
end
{% end %}
{
{% for key, type in T %}
{{key.id.stringify}}: (%var{key.id}).as({{type}}),
{% end %}
}
{% end %}
end
# Reads a serialized enum member by name from *ctx* and *node*.
#
# See `#to_yaml` for reference.
#
# Raises `YAML::ParseException` if the deserialization fails.
def Enum.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
{% if @type.annotation(Flags) %}
if node.is_a?(YAML::Nodes::Sequence)
value = {{ @type }}::None
node.each do |element|
string = parse_scalar(ctx, element, String)
value |= parse?(string) || element.raise "Unknown enum #{self} value: #{string.inspect}"
end
value
else
node.raise "Expected sequence, not #{node.kind}"
end
{% else %}
string = parse_scalar(ctx, node, String)
parse?(string) || node.raise "Unknown enum #{self} value: #{string.inspect}"
{% end %}
end
module Enum::ValueConverter(T)
def self.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : T
from_yaml(ctx, node)
end
# Reads a serialized enum member by value from *ctx* and *node*.
#
# See `.to_yaml` for reference.
#
# Raises `YAML::ParseException` if the deserialization fails.
def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : T
value = parse_scalar ctx, node, Int64
T.from_value?(value) || node.raise "Unknown enum #{T} value: #{value}"
end
end
def Union.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
if node.is_a?(YAML::Nodes::Alias)
{% for type in T %}
{% if type < ::Reference %}
ctx.read_alias?(node, {{type}}) do |obj|
return obj
end
{% end %}
{% end %}
node.raise("Error deserializing alias")
end
{% begin %}
# String must come last because anything can be parsed into a String.
# So, we give a chance first to types in the union to be parsed.
{% string_type = T.find { |type| type == ::String } %}
{% for type in T %}
{% unless type == string_type %}
begin
return {{type}}.new(ctx, node)
rescue YAML::ParseException
# Ignore
end
{% end %}
{% end %}
{% if string_type %}
begin
return {{string_type}}.new(ctx, node)
rescue YAML::ParseException
# Ignore
end
{% end %}
{% end %}
node.raise "Couldn't parse #{self}"
end
def Time.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
parse_scalar(ctx, node, Time)
end
struct Time::Format
def from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : Time
unless node.is_a?(YAML::Nodes::Scalar)
node.raise "Expected scalar, not #{node.kind}"
end
parse(node.value, Time::Location::UTC)
end
end
module Time::EpochConverter
def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : Time
unless node.is_a?(YAML::Nodes::Scalar)
node.raise "Expected scalar, not #{node.kind}"
end
Time.unix(node.value.to_i)
end
end
module Time::EpochMillisConverter
def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : Time
unless node.is_a?(YAML::Nodes::Scalar)
node.raise "Expected scalar, not #{node.kind}"
end
Time.unix_ms(node.value.to_i64)
end
end
module YAML::ArrayConverter(Converter)
def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : Array
unless node.is_a?(YAML::Nodes::Sequence)
node.raise "Expected sequence, not #{node.kind}"
end
ary = Array(typeof(Converter.from_yaml(ctx, node))).new
node.each do |value|
ary << Converter.from_yaml(ctx, value)
end
ary
end
end
struct Slice
def self.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
{% if T != UInt8 %}
{% raise "Can only deserialize Slice(UInt8), not #{@type}}" %}
{% end %}
parse_scalar(ctx, node, self)
end
end