-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
any.cr
380 lines (325 loc) · 8.8 KB
/
any.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# `YAML::Any` is a convenient wrapper around all possible YAML core types
# (`YAML::Any::Type`) and can be used for traversing dynamic or
# unknown YAML structures.
#
# ```
# require "yaml"
#
# data = YAML.parse <<-END
# ---
# foo:
# bar:
# baz:
# - qux
# - fox
# END
# data["foo"]["bar"]["baz"][0].as_s # => "qux"
# data["foo"]["bar"]["baz"].as_a # => ["qux", "fox"]
# ```
#
# Note that methods used to traverse a YAML structure, `#[]`, `#[]?` and `#each`,
# always return a `YAML::Any` to allow further traversal. To convert them to `String`,
# `Array`, etc., use the `as_` methods, such as `#as_s`, `#as_a`, which perform
# a type check against the raw underlying value. This means that invoking `#as_s`
# when the underlying value is not a `String` will raise: the value won't automatically
# be converted (parsed) to a `String`.
struct YAML::Any
# All valid YAML core schema types.
alias Type = Nil | Bool | Int64 | Float64 | String | Time | Bytes | Array(Any) | Hash(Any, Any) | Set(Any)
def self.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
anchors = {} of String => Any
convert(node, anchors)
end
private def self.convert(node, anchors)
case node
when YAML::Nodes::Scalar
new YAML::Schema::Core.parse_scalar(node.value)
when YAML::Nodes::Sequence
ary = [] of Any
if anchor = node.anchor
anchors[anchor] = Any.new(ary)
end
node.each do |value|
ary << convert(value, anchors)
end
new ary
when YAML::Nodes::Mapping
hash = {} of Any => Any
if anchor = node.anchor
anchors[anchor] = Any.new(hash)
end
node.each do |key, value|
hash[convert(key, anchors)] = convert(value, anchors)
end
new hash
when YAML::Nodes::Alias
anchors[node.anchor]
else
raise "Unknown node: #{node.class}"
end
end
# Returns the raw underlying value, a `Type`.
getter raw : Type
# Creates a `Any` that wraps the given `Type`.
def initialize(@raw : Type)
end
# Assumes the underlying value is an `Array` or `Hash` and returns its size.
#
# Raises if the underlying value is not an `Array` or `Hash`.
def size : Int
case object = @raw
when Array
object.size
when Hash
object.size
else
raise "Expected Array or Hash for #size, not #{object.class}"
end
end
# Assumes the underlying value is an `Array` or `Hash`
# and returns the element at the given *index_or_key*.
#
# Raises if the underlying value is not an `Array` nor a `Hash`.
def [](index_or_key) : YAML::Any
case object = @raw
when Array
if index_or_key.is_a?(Int)
object[index_or_key]
else
raise "Expected int key for Array#[], not #{object.class}"
end
when Hash
object[index_or_key]
else
raise "Expected Array or Hash, not #{object.class}"
end
end
# Assumes the underlying value is an `Array` or `Hash` and returns the element
# at the given *index_or_key*, or `nil` if out of bounds or the key is missing.
#
# Raises if the underlying value is not an `Array` nor a `Hash`.
def []?(index_or_key) : YAML::Any?
case object = @raw
when Array
if index_or_key.is_a?(Int)
object[index_or_key]?
else
nil
end
when Hash
object[index_or_key]?
else
raise "Expected Array or Hash, not #{object.class}"
end
end
# Traverses the depth of a structure and returns the value.
# Returns `nil` if not found.
def dig?(index_or_key, *subkeys) : YAML::Any?
self[index_or_key]?.try &.dig?(*subkeys)
end
# :nodoc:
def dig?(index_or_key) : YAML::Any?
case @raw
when Hash, Array
self[index_or_key]?
else
nil
end
end
# Traverses the depth of a structure and returns the value, otherwise raises.
def dig(index_or_key, *subkeys) : YAML::Any
self[index_or_key].dig(*subkeys)
end
# :nodoc:
def dig(index_or_key) : YAML::Any
self[index_or_key]
end
# Checks that the underlying value is `Nil`, and returns `nil`.
# Raises otherwise.
def as_nil : Nil
@raw.as(Nil)
end
# Checks that the underlying value is `Bool`, and returns its value.
# Raises otherwise.
def as_bool : Bool
@raw.as(Bool)
end
# Checks that the underlying value is `Bool`, and returns its value.
# Returns `nil` otherwise.
def as_bool? : Bool?
as_bool if @raw.is_a?(Bool)
end
# Checks that the underlying value is `String`, and returns its value.
# Raises otherwise.
def as_s : String
@raw.as(String)
end
# Checks that the underlying value is `String`, and returns its value.
# Returns `nil` otherwise.
def as_s? : String?
@raw.as?(String)
end
# Checks that the underlying value is `Int64`, and returns its value.
# Raises otherwise.
def as_i64 : Int64
@raw.as(Int64)
end
# Checks that the underlying value is `Int64`, and returns its value.
# Returns `nil` otherwise.
def as_i64? : Int64?
@raw.as?(Int64)
end
# Checks that the underlying value is `Int64`, and returns its value as `Int32`.
# Raises otherwise.
def as_i : Int32
@raw.as(Int64).to_i
end
# Checks that the underlying value is `Int64`, and returns its value as `Int32`.
# Returns `nil` otherwise.
def as_i? : Int32?
as_i if @raw.is_a?(Int)
end
# Checks that the underlying value is `Float64`, and returns its value.
# Raises otherwise.
def as_f : Float64
@raw.as(Float64)
end
# Checks that the underlying value is `Float64`, and returns its value.
# Returns `nil` otherwise.
def as_f? : Float64?
@raw.as?(Float64)
end
# Checks that the underlying value is `Float`, and returns its value as an `Float32`.
# Raises otherwise.
def as_f32 : Float32
@raw.as(Float).to_f32
end
# Checks that the underlying value is `Float`, and returns its value as an `Float32`.
# Returns `nil` otherwise.
def as_f32? : Float32?
as_f32 if @raw.is_a?(Float)
end
# Checks that the underlying value is `Time`, and returns its value.
# Raises otherwise.
def as_time : Time
@raw.as(Time)
end
# Checks that the underlying value is `Time`, and returns its value.
# Returns `nil` otherwise.
def as_time? : Time?
@raw.as?(Time)
end
# Checks that the underlying value is `Array`, and returns its value.
# Raises otherwise.
def as_a : Array(Any)
@raw.as(Array)
end
# Checks that the underlying value is `Array`, and returns its value.
# Returns `nil` otherwise.
def as_a? : Array(Any)?
@raw.as?(Array)
end
# Checks that the underlying value is `Hash`, and returns its value.
# Raises otherwise.
def as_h : Hash(Any, Any)
@raw.as(Hash)
end
# Checks that the underlying value is `Hash`, and returns its value.
# Returns `nil` otherwise.
def as_h? : Hash(Any, Any)?
@raw.as?(Hash)
end
# Checks that the underlying value is `Bytes`, and returns its value.
# Raises otherwise.
def as_bytes : Bytes
@raw.as(Bytes)
end
# Checks that the underlying value is `Bytes`, and returns its value.
# Returns `nil` otherwise.
def as_bytes? : Bytes?
@raw.as?(Bytes)
end
# :nodoc:
def inspect(io : IO) : Nil
@raw.inspect(io)
end
# :nodoc:
def to_s(io : IO) : Nil
@raw.to_s(io)
end
# :nodoc:
def pretty_print(pp)
@raw.pretty_print(pp)
end
# Returns `true` if both `self` and *other*'s raw object are equal.
def ==(other : YAML::Any)
raw == other.raw
end
# Returns `true` if the raw object is equal to *other*.
def ==(other)
raw == other
end
# See `Object#hash(hasher)`
def_hash raw
# :nodoc:
def to_yaml(io)
raw.to_yaml(io)
end
def to_json(builder : JSON::Builder)
if (raw = self.raw).is_a?(Slice)
raise "Can't serialize #{raw.class} to JSON"
else
raw.to_json(builder)
end
end
# Returns a new YAML::Any instance with the `raw` value `dup`ed.
def dup
Any.new(raw.dup)
end
# Returns a new YAML::Any instance with the `raw` value `clone`ed.
def clone
Any.new(raw.clone)
end
# Forwards `to_json_object_key` to `raw` if it responds to that method,
# raises `JSON::Error` otherwise.
def to_json_object_key
raw = @raw
if raw.responds_to?(:to_json_object_key)
raw.to_json_object_key
else
raise JSON::Error.new("can't convert #{raw.class} to a JSON object key")
end
end
end
class Object
def ===(other : YAML::Any)
self === other.raw
end
end
struct Value
def ==(other : YAML::Any)
self == other.raw
end
end
class Reference
def ==(other : YAML::Any)
self == other.raw
end
end
class Array
def ==(other : YAML::Any)
self == other.raw
end
end
class Hash
def ==(other : YAML::Any)
self == other.raw
end
end
class Regex
def ===(other : YAML::Any)
value = self === other.raw
$~ = $~
value
end
end