-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProtocol.lua
More file actions
193 lines (169 loc) · 6.46 KB
/
Copy pathProtocol.lua
File metadata and controls
193 lines (169 loc) · 6.46 KB
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
local checks <const> = require 'checks'
local function _type(arg) -- any
local arg_type <const> = type(arg)
return (arg_type == 'table' and arg.__type) or arg_type
end
local Protocol = {
__type = 'Protocol',
_default_constructor_name = 'new'
}
Protocol.__index = Protocol
function Protocol.new(fields)
checks('table')
local self <const> = setmetatable({}, Protocol)
self.constructor_name = Protocol._default_constructor_name
self.fields = fields
return self
end
function Protocol:conform(obj)
checks('Protocol', 'table')
for field_name, field_check in pairs(self.fields) do
local field = field_check(obj, field_name, obj[field_name])
obj[field_name] = field
end
end
function Protocol.remove_all_protocols(class)
class.__protocols = nil
class.new = class.__new
class.__new = nil
return class
end
function Protocol:apply(class, statics)
checks('Protocol', 'table', '?table')
if not class.__protocols then
class.__protocols = {}
class.__new = class.new
end
table.insert(class.__protocols, self)
class[self.constructor_name] = function(...)
local obj <const> = class.__new(...)
for _, protocol in ipairs(class.__protocols) do
protocol:conform(obj)
end
return obj
end
end
Protocol.errors = {
TypeError = 'Protocol type error: ',
TypeFormatError = 'Protocol type error: Type must be expressed as a string or table with .__type key.',
FieldNotImplementedError = 'Protocol field error: field not implemented.\n',
WrongFieldTypeError = 'Protocol type error: wrong field type.\n',
-- CheckFunctionNotImplementedError = 'Protocol field subclass error: a field must have implemented "check" method accepting object, field name and field value.',
NilDefaultError =
'Protocol default field error: default value cannot be nil. To declare a typed field without default implementation, use "Type" instead.',
NilFinalError =
'Protocol final field error: final value cannot be nil. To declare a typed field without final implementation, use "Type" instead.',
FinalFieldReassignedError = 'Protocol final field reassigned when instantiating ',
CannotInstantiateError =
'Protocol default/final field error: a field instantiated from class must have a constructor.',
}
local function CheckFactory(check)
checks('function')
local check_class = {
__type = 'Check',
__call = check,
}
check_class.__index = check_class
check_class.new = function(value_getter)
checks('function')
local self = setmetatable({}, check_class)
self.value_getter = value_getter
return self
end
return check_class
end
local CheckType = CheckFactory(function(self, obj, field_name, field_value)
checks('Check', 'table', 'string', '?')
local expected_type = self:value_getter(obj)
if field_value == nil then
error(Protocol.errors.FieldNotImplementedError
.. 'Field "' .. field_name
.. '" of type ' .. expected_type
.. ' not implemented in ' .. obj.__type .. ' instance.'
)
end
if _type(field_value) ~= expected_type then
error(Protocol.errors.WrongFieldTypeError
.. _type(obj) .. ' instance field "' .. field_name .. '":\n'
.. 'expected: ' .. expected_type .. '\n'
.. 'actual: ' .. _type(field_value)
)
end
end)
Protocol.Type = {
__type = 'Protocol.Type',
__call = function(self, expected_type)
expected_type = self._sanitize(expected_type)
return CheckType.new(function() return expected_type end)
end,
_sanitize = function(type_)
checks('string|table')
if type(type_) == 'table' then
return type_.__type or error(Protocol.errors.TypeFormatError)
end
return type_
end,
number = CheckType.new(function() return 'number' end),
string = CheckType.new(function() return 'string' end),
boolean = CheckType.new(function() return 'boolean' end),
table = CheckType.new(function() return 'table' end),
method = CheckType.new(function() return 'function' end) -- exception!
}
setmetatable(Protocol.Type, Protocol.Type)
local CheckDefault = CheckFactory(function(self, obj, field_name, field_value)
checks('Check', 'table', 'string', '?')
local default_value = self:value_getter(obj)
if field_value == nil then
return default_value
end
local expected_type = _type(default_value)
if _type(field_value) ~= expected_type then
error(Protocol.errors.WrongFieldTypeError
.. _type(obj) .. ' instance field "' .. field_name .. '":\n'
.. 'expected: ' .. expected_type .. '\n'
.. 'actual: ' .. _type(field_value)
)
end
return field_value
end)
Protocol.Default = {
__type = 'Protocol.Default',
__call = function(_, default_value)
if default_value == nil then error(Protocol.errors.NilDefaultError) end
return CheckDefault.new(function() return default_value end)
end,
table = CheckDefault.new(function() return {} end),
instance = function(class, ...)
checks('table')
local constructor, args = class[Protocol._default_constructor_name], { ... }
if not constructor then error(Protocol.errors.CannotInstantiateError) end
return CheckDefault.new(function() return constructor(table.unpack(args)) end)
end
}
setmetatable(Protocol.Default, Protocol.Default)
local CheckFinal = CheckFactory(function(self, obj, field_name, field_value)
checks('Check', 'table', 'string', '?')
if field_value ~= nil then
error(Protocol.errors.FinalFieldReassignedError
.. _type(obj) .. ' (final field "' .. field_name .. '")'
)
else
return self:value_getter(obj)
end
end)
Protocol.Final = {
__type = 'Protocol.Final',
__call = function(self, final_value)
if final_value == nil then error(Protocol.errors.NilFinalError) end
return CheckFinal.new(function() return final_value end)
end,
table = CheckFinal.new(function() return {} end),
instance = function(class, ...)
checks('table')
local constructor, args = class[Protocol._default_constructor_name], { ... }
if not constructor then error(Protocol.errors.CannotInstantiateError) end
return CheckFinal.new(function() return constructor(table.unpack(args)) end)
end
}
setmetatable(Protocol.Final, Protocol.Final)
return Protocol