forked from Garoze/testLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv1.lua
More file actions
54 lines (51 loc) · 2.44 KB
/
v1.lua
File metadata and controls
54 lines (51 loc) · 2.44 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
local callMeta = { __call = function(self, ...) return self:validate(...) end }
function isValidInterface(interface)
local errors = {}
for field, v in pairs(interface) do
if type(field) ~= 'string' or type(v) ~= 'string' then table.insert(errors, field) end
end
return #errors >= 1 and errors or true
end
function Interface(interface, name)
assert(isValidInterface(interface) == true, '[Interface] - Invalid interface construction, please make sure that every field has a \'string\' value.')
return setmetatable({
__name = name or 'default',
__type = 'interfaceObject',
interface = interface,
validate = function(self, validateInterface)
assert(type(validateInterface) == 'table', '[Interface - '..self.__name..'] - Expected a \'table\' on #1 argument and got '..type(validadeInterface))
if self:hasSameFields(validateInterface) == true then
local returnValue = 0
for field, v in pairs(validateInterface) do
if self:getFieldType(field, validateInterface) == false then
print('Name: '..self.__name..' (f)_getDiffFields ['..field..'] - Expected \''..self.interface[field]..'\' and got '..type(validateInterface[field]))
returnValue = returnValue + 1
end
end
return returnValue == 0 and true or false
else return false, self:getDiffFields(validateInterface) end
end,
hasSameFields = function(self, validateInterface)
for field, v in pairs(self.interface) do
if validateInterface[field] == nil then return false end
end
return true
end,
getDiffFields = function(self, validateInterface)
local index = {}
for field, _ in pairs(self.interface) do
if validateInterface[field] == nil then table.insert(index, tostring(field)) end
end
return index
end,
hasSameType = function(self, validateInterface)
for field, t in pairs(self.interface) do
if type(validateInterface[i]) ~= t then return false end
end
return true
end,
getFieldType = function(self, field, validateInterface)
if type(validateInterface[field]) ~= self.interface[field] then return false end
end,
}, callMeta)
end