/
to_yaml.cr
119 lines (103 loc) · 1.85 KB
/
to_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
class Object
def to_yaml
String.build do |io|
to_yaml(io)
end
end
def to_yaml(io : IO)
YAML.build(io) do |yaml|
to_yaml(yaml)
end
end
end
class Hash
def to_yaml(yaml : YAML::Builder)
yaml.mapping do
each do |key, value|
key.to_yaml(yaml)
value.to_yaml(yaml)
end
end
end
end
class Array
def to_yaml(yaml : YAML::Builder)
yaml.sequence do
each &.to_yaml(yaml)
end
end
end
struct Tuple
def to_yaml(yaml : YAML::Builder)
yaml.sequence do
each &.to_yaml(yaml)
end
end
end
struct NamedTuple
def to_yaml(yaml : YAML::Builder)
yaml.mapping do
{% for key in T.keys %}
{{key.symbolize}}.to_yaml(yaml)
self[{{key.symbolize}}].to_yaml(yaml)
{% end %}
end
end
end
class String
def to_yaml(yaml : YAML::Builder)
yaml.scalar self
end
end
struct Number
def to_yaml(yaml : YAML::Builder)
yaml.scalar self
end
end
struct Nil
def to_yaml(yaml : YAML::Builder)
yaml.scalar ""
end
end
struct Bool
def to_yaml(yaml : YAML::Builder)
yaml.scalar self
end
end
struct Set
def to_yaml(yaml : YAML::Builder)
yaml.sequence do
each &.to_yaml(yaml)
end
end
end
struct Symbol
def to_yaml(yaml : YAML::Builder)
yaml.scalar self
end
end
struct Enum
def to_yaml(yaml : YAML::Builder)
yaml.scalar value
end
end
struct Time
def to_yaml(yaml : YAML::Builder)
yaml.scalar Time::Format::ISO_8601_DATE_TIME.format(self)
end
end
struct Time::Format
def to_yaml(value : Time, yaml : YAML::Builder)
format(value).to_yaml(yaml)
end
end
module Time::EpochConverter
def self.to_yaml(value : Time, yaml : YAML::Builder)
yaml.scalar value.epoch
end
end
module Time::EpochMillisConverter
def self.to_yaml(value : Time, yaml : YAML::Builder)
yaml.scalar value.epoch_ms
end
end