Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 53 additions & 8 deletions interface.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,44 @@
-- function name(arg) *1 -> name {} -> f({}) --

local GlobalInterfaces = {}
local interfaceCallMeta = { __call = function(self, ...) return self:validateInterface(self,...) end }
local interfaceCallMeta = { __call = function(self, ...) return self:validateInterface(self,...) end }
local validTypes = {
luaTypes = {'string', 'number', 'boolean', 'table'},
customTypes = {
['interfaceObject'] = function (interface)
if (type(interface) == 'table') then
return interface.__type == 'interfaceObject' or false
end
return false
end
}
}

function isLuaType(typeValue)
for _, v in ipairs(validTypes.luaTypes) do
if (typeValue == v) then
return true
end
end
return false
end

function getTypeNameByTypeValue(typeValue)
for typeName, tV in pairs(validTypes) do
if (isLuaType(typeValue)) then
return typeName
else
if (typeName == 'customTypes') then
for typeName2, _ in pairs(tV) do
if (typeValue == typeName2) then
return typeName
end
end
end
end
end
return false
end

function interfaceExists(name)
return GlobalInterfaces[name] ~= nil or false
Expand Down Expand Up @@ -33,6 +70,11 @@ function Interface(name, interfaceTable)
assert(type(interfaceTable) == 'table', '[createInterface] - Expected a \'table\' on #2 argument and got '..type(interfaceTable))
if interfaceExists(name) == false then
assert(isValidInterface(interfaceTable) == true, '[Interface] - Invalid interface construction, please make sure that every field has a \'string\' value.')

-- for field, typeValue in pairs(interfaceTable) do
-- print("["..field.."] ["..typeValue.."] ["..tostring(getTypeNameByTypeValue(typeValue)).."]")
-- end

GlobalInterfaces[name] = {
__name = name,
__type = 'interfaceObject',
Expand All @@ -52,24 +94,28 @@ function Interface(name, interfaceTable)
return true
end,
hasSameTypes = function(self, validateTable)
for field, v in pairs(self.interface) do
if type(validateTable[field]) ~= v then
for field, v in pairs(self.interface) do
if (getTypeNameByTypeValue(v) == 'luaTypes' and type(validateTable[field]) ~= v) then
return false
elseif (getTypeNameByTypeValue(v) == 'customTypes' and validTypes.customTypes[v](validateTable[field]) == false) then
return false
end
end
return true
end,
getDiffMethods = function(self, validateTable)
for field, v in pairs(self.interface) do
if validateTable[field] == nil then
for field, v in pairs(self.interface) do
if validateTable[field] == nil then
print('[Interface] - Missing Field: '..field)
end
end
end,
getDiffTypes = function(self, validateTable)
for field, v in pairs(self.interface) do
if type(validateTable[field]) ~= v then
print('[getDiffTypes] - Field: '..field..' expected \''..v..'\' and got ' ..type(validateTable[field]))
if (getTypeNameByTypeValue(v) == 'luaTypes' and type(validateTable[field]) ~= v) then
print('[getDiffTypes] - Field: ('..field..') expected \''..v..'\' and got ' ..type(validateTable[field]))
elseif (getTypeNameByTypeValue(v) == 'customTypes' and validTypes.customTypes[v](validateTable[field]) == false) then
print('[getDiffTypes] - Field: ('..field..') expected \''..v..'\'')
end
end
end,
Expand All @@ -83,4 +129,3 @@ function Interface(name, interfaceTable)
}
else return getInterfaceObject(name):validateInterface(interfaceTable) end
end