-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
serializable.cr
184 lines (170 loc) · 5.56 KB
/
serializable.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
module DB
annotation Field
end
# The `DB::Serialization` module automatically generates methods for DB serialization when included.
#
# Once included, `ResultSet#read(t)` populates properties of the class from the
# `ResultSet`.
#
# ### Example
#
# ```
# require "db"
#
# class Employee
# include DB::Serializable
#
# property title : String
# property name : String
# end
#
# employees = Employee.from_rs(db.query("SELECT title, name FROM employees"))
# employees[0].title # => "Manager"
# employees[0].name # => "John"
# ```
#
# ### Usage
#
# `DB::Serializable` was designed in analogue with `JSON::Serializable`, so usage is identical.
# However, like `DB.mapping`, `DB::Serializable` is **strict by default**, so extra columns will raise `DB::MappingException`s.
#
# Similar to `JSON::Field`, there is an annotation `DB::Field` that can be used to set serialization behavior
# on individual instance variables.
#
# ```
# class Employee
# include DB::Serializable
#
# property title : String
#
# @[DB::Field(key: "firstname")]
# property name : String?
# end
# ```
#
# `DB::Field` properties:
# * **ignore**: if `true`, skip this field in serialization and deserialization (`false` by default)
# * **key**: defines which column to read from a `ResultSet` (name of the instance variable by default)
# * **converter**: defines an alternate type for parsing results. The given type must define `#from_rs(DB::ResultSet)` and return an instance of the included type.
#
# ### `DB::Serializable::NonStrict`
#
# Including this module is functionally identical to passing `{strict: false}` to `DB.mapping`: extra columns will not raise.
#
# ```
# class Employee
# include DB::Serializable
# include DB::Serializable::NonStrict
#
# property title : String
# property name : String
# end
#
# # does not raise!
# employees = Employee.from_rs(db.query("SELECT title, name, age FROM employees"))
# ```
module Serializable
macro included
include ::DB::Mappable
# Define a `new` and `from_rs` directly in the type, like JSON::Serializable
# For proper overload resolution
def self.new(rs : ::DB::ResultSet)
instance = allocate
instance.initialize(__set_for_db_serializable: rs)
GC.add_finalizer(instance) if instance.responds_to?(:finalize)
instance
end
def self.from_rs(rs : ::DB::ResultSet)
objs = Array(self).new
rs.each do
objs << self.new(rs)
end
objs
ensure
rs.close
end
# Inject the class methods into subclasses as well
macro inherited
def self.new(rs : ::DB::ResultSet)
super
end
def self.from_rs(rs : ::DB::ResultSet)
super
end
end
end
def initialize(*, __set_for_db_serializable rs : ::DB::ResultSet)
{% begin %}
{% properties = {} of Nil => Nil %}
{% for ivar in @type.instance_vars %}
{% ann = ivar.annotation(::DB::Field) %}
{% unless ann && ann[:ignore] %}
{%
properties[ivar.id] = {
type: ivar.type,
key: ((ann && ann[:key]) || ivar).id.stringify,
default: ivar.default_value,
nilable: ivar.type.nilable?,
converter: ann && ann[:converter],
}
%}
{% end %}
{% end %}
{% for name, value in properties %}
%var{name} = nil
%found{name} = false
{% end %}
rs.each_column do |col_name|
case col_name
{% for name, value in properties %}
when {{value[:key]}}
%found{name} = true
begin
%var{name} =
{% if value[:converter] %}
{{value[:converter]}}.from_rs(rs)
{% elsif value[:nilable] || value[:default] != nil %}
rs.read(::Union({{value[:type]}} | Nil))
{% else %}
rs.read({{value[:type]}})
{% end %}
rescue exc
::raise ::DB::MappingException.new(exc.message, self.class.to_s, {{name.stringify}}, cause: exc)
end
{% end %}
else
rs.read # Advance set, but discard result
on_unknown_db_column(col_name)
end
end
{% for key, value in properties %}
{% unless value[:nilable] || value[:default] != nil %}
if %var{key}.nil? && !%found{key}
::raise ::DB::MappingException.new("Missing column {{value[:key].id}}", self.class.to_s, {{key.stringify}})
end
{% end %}
{% end %}
{% for key, value in properties %}
{% if value[:nilable] %}
{% if value[:default] != nil %}
@{{key}} = %found{key} ? %var{key} : {{value[:default]}}
{% else %}
@{{key}} = %var{key}
{% end %}
{% elsif value[:default] != nil %}
@{{key}} = %var{key}.is_a?(Nil) ? {{value[:default]}} : %var{key}
{% else %}
@{{key}} = %var{key}.as({{value[:type]}})
{% end %}
{% end %}
{% end %}
end
protected def on_unknown_db_column(col_name)
::raise ::DB::MappingException.new("Unknown column: #{col_name}", self.class.to_s)
end
module NonStrict
protected def on_unknown_db_column(col_name)
end
end
end
end