-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathorm.lua
281 lines (247 loc) · 6.85 KB
/
orm.lua
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
local Dirty = require("dirty")
local math = math
local tconcat = table.concat
local tonumber = tonumber
local tostring = tostring
local next = next
local tointeger = math.tointeger
local math_abs = math.abs
local MATH_HUGE = math.huge
local function _cls_parse_error(cls, data, msg)
local s = string.format("cls parse: <%s> <%s> %s", cls.name, tostring(data), tostring(msg))
error(s)
end
-- 新增类型必须实现
-- is_atom 是否原子类型
-- default 默认值
-- parse 解析函数
-- new 构造函数(非atom类型)
-- setfield 修改属性(非atom类型)
local KEYWORD_MAP = {
["struct"] = require("orm_cls.struct"),
["dict"] = require("orm_cls.dict"),
["list"] = require("orm_cls.list"),
["boolean"] = {is_atom=true, default=false,
parse = function(_, s)
return s == true
end
},
["integer"] = {is_atom=true, default=0,
parse = function(cls, s)
local value = tointeger(s)
if value == nil then
_cls_parse_error(cls, s, "is not integer")
end
return value
end
},
["double"] = {is_atom=true, default=0,
parse = function(cls, s)
local value = tonumber(s)
if value == nil or value ~= value or math_abs(value) == math.huge then
_cls_parse_error(cls, s, "is not double")
end
return value
end
},
["string"] = {is_atom=true, default="",
parse = function(_, s)
return tostring(s)
end
},
["binary"] = {is_atom=true, default="",
parse = function(_, s)
return s
end
},
["date"] = {is_atom=true, default=nil,
parse = function(_, s)
return s
end
},
}
local KEYWORD_ATTRS = {
['__cls'] = true,
['__dirty'] = true,
['__data'] = true,
['__ref'] = true,
}
local function has_cls_type(cls_type)
return KEYWORD_MAP[cls_type]
end
local function get_register_cls(cls_type)
return KEYWORD_MAP[cls_type]
end
local g_cls_ref_map = {} -- cls_name : [parent_name, ...]
local g_cls_map = {} -- cls_name: cls
local function check_ref(node_id, parent_id)
-- print('check ref', node_id, parent_id)
if parent_id == nil then
return
end
if parent_id == node_id then
error(string.format('type<%s> ref redefined', node_id))
end
local p_map = g_cls_ref_map[node_id]
if not p_map then
p_map = {}
g_cls_ref_map[node_id] = p_map
end
p_map[parent_id] = true -- record parent
-- check and update parent's parent
local pp_map = g_cls_ref_map[parent_id]
if not pp_map then
pp_map = {}
g_cls_ref_map[parent_id] = pp_map
end
for pp_id, _ in pairs(pp_map) do
check_ref(node_id, pp_id)
end
end
local function create_cls(cls, parent_name)
if parent_name then
cls.name = parent_name .. "." .. cls.name
end
local cls_name = cls.name
local cls_type = cls.type
if has_cls_type(cls_name) then
error(string.format("cls name<%s> is keyword", cls_name))
return nil
end
if not cls_type then
error(string.format(" cls name<%s> invalid cls type", cls_name))
return nil
end
-- ref type
local ref_cls = g_cls_map[cls_type]
if ref_cls then
check_ref(ref_cls.name, parent_name)
-- copy ref
for k, v in pairs(ref_cls) do
cls[k] = v
end
-- reset name
cls.name = cls_name
if not ref_cls.id then
assert(false, cls.name)
cls.id = ref_cls
end
return cls
end
local cls_cfg = get_register_cls(cls_type)
if not (cls_cfg and cls_cfg.parse) then
error(string.format("data type <%s> <%s> no parse", cls_name, cls_type))
return nil
end
check_ref(cls_name, parent_name)
cls.id = cls
cls.parse = cls_cfg.parse
cls.is_atom = cls_cfg.is_atom
cls.default = cls_cfg.default
if cls.is_atom then
return cls
end
cls.new = cls_cfg.new
local mt_index = {
__cls = cls,
__enable_dirty = Dirty.__enable_dirty,
__disable_dirty = Dirty.__disable_dirty,
__get_dirty = Dirty.__get_dirty,
__clear_dirty = Dirty.__clear_dirty,
}
local mt = {
__index = function(t, key)
return t.__data[key] or mt_index[key]
end,
__newindex = cls_cfg.setfield,
__pairs = function(t)
return next, t.__data, nil
end,
}
-- TODO 其他元表补充
cls.mt = mt
if cls_type == 'struct' then
assert(cls.attrs, "not attrs")
local attrs = {}
for k, v in pairs(cls.attrs) do
if KEYWORD_ATTRS[k] then
error(string.format("class <%s> define key attr <%s>", cls_name, k))
end
v.name = k
local v_cls = create_cls(v, cls_name)
if v_cls.is_atom then
mt_index[k] = v_cls.default
end
attrs[k] = v_cls
end
cls.attrs = attrs
return cls
end
if cls_type == 'list' then
mt.__ipairs = function(t)
return function(a, i)
i = i + 1
local v = a[i]
if v then return i, v end
end, t.__data, 0
end
mt.__len = function(t)
return rawlen(t.__data)
end
mt.__concat = function(t)
return tconcat(t.__data)
end
cls.item.name = 'item'
cls.item = create_cls(cls.item, cls_name)
return cls
end
if cls_type == 'dict' then
cls.key.name = 'key'
cls.key = create_cls(cls.key, cls_name)
cls.value.name = 'value'
cls.value = create_cls(cls.value, cls_name)
return cls
end
error(string.format("unsupport cls type<%s>", cls_type))
end
local M = {}
function M.init(type_list)
-- reset
g_cls_map = {}
g_cls_ref_map = {}
for _, item in ipairs(type_list) do
local name = item.name
assert(name, 'not cls name')
g_cls_map[name] = create_cls(item, nil)
end
M.g_cls_map = g_cls_map
M.g_cls_ref_map = g_cls_ref_map
end
function M.create(cls_name, data)
local cls = g_cls_map[cls_name]
if not cls then
error(string.format("create obj, illgeal cls<%s>", cls_name))
end
return cls:new(data)
end
-- dirty manager 挂载到节点
function M.enable_dirty(obj)
obj:__enable_dirty(nil, nil)
end
function M.disable_dirty(obj)
obj:__disable_dirty()
end
function M.get_dirty_info(obj)
return Dirty.get_dirty_info(obj)
end
function M.clear_dirty_info(obj)
return Dirty.clear_dirty_info(obj)
end
function M.get_mongo_dirty_data(obj)
return Dirty.get_mongo_dirty_data(obj)
end
-- 提供用于全量存盘
function M.clone_mongodata(obj)
return Dirty.clone_mongodata(obj)
end
return M