-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseObj.lua
116 lines (101 loc) · 2.65 KB
/
baseObj.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
require "util.escape"
require "util.deepcopy"
require "util.tables"
local BaseObj = {}
BaseObj.__tsdoesjson = true
BaseObj.__extends = {}
BaseObj.__type = "BaseObj"
function BaseObj:__call(type)
obj = table.deepcopy(self)
obj.__type = type
obj:updateObject()
return obj
end
function BaseObj:use(obj)
for k,v in pairs(obj) do
if self[k] == nil then
self[k] = v
end
end
local tmp = {}
for _,v in pairs(obj.__extends) do
tmp[#tmp + 1] = v
end
for _,v in pairs(self.__extends) do
tmp[#tmp + 1] = v
end
self.__extends = {}
for _,v in pairs(tmp) do
if not hasValue(self.__extends, v) then
self.__extends[#self.__extends + 1] = v
end
end
end
function BaseObj:applyTo(obj)
local mt = getmetatable(obj)
if mt ~= nil then
for k,v in pairs(mt) do
if obj[k] == nil then
obj[k] = v
end
end
end
for k,v in pairs(self) do
if obj[k] == nil then
obj[k] = v
end
end
local tmp = {}
for k,v in pairs(self.__extends) do
tmp[k] = v
end
for _,v in pairs(tmp) do
if not hasValue(obj.__extends, v) then
obj.__extends[#obj.__extends + 1] = v
end
end
obj:updateObject()
return obj
end
function BaseObj:typeof(test)
if type(test) == "table" then
if test.__type then
return hasValue(self.__extends, test.__type)
else
return false
end
end
return hasValue(self.__extends, tostring(test))
end
function BaseObj:updateObject()
if not hasValue(self.__extends, self.__type) then
self.__extends[#self.__extends + 1] = self.__type
end
if getmetatable(self) == nil then
setmetatable(self, {})
end
for k,v in pairs(self) do
if tostring(k):find("__") then
getmetatable(self)[k] = v
end
end
end
function BaseObj:__tostring()
local string = "{"
for k,v in pairs(self) do
if not tostring(k):find("__") then
if (type(v) == "table" and v["__tsdoesjson"]) or type(v) == "number" or type(v) == "boolean" then
string = string .. "\"" .. escape(tostring(k)) .. "\":" .. tostring(v) .. ","
elseif (type(v) ~= "function") then
string = string .. "\"" .. escape(tostring(k)) .. "\":\"" .. escape(tostring(v)) .. "\","
end
end
end
if string:sub(#string, #string) ~= "{" then
string = string:sub(1, -2)
end
string = string .. "}"
return string
end
BaseObj:updateObject()
return BaseObj