diff --git a/interface.lua b/interface.lua index e2c605d..6faf0d1 100644 --- a/interface.lua +++ b/interface.lua @@ -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' and interface.__name and GlobalInterfaces[tostring(interface.__name)]) 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 @@ -52,24 +89,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, @@ -83,4 +124,3 @@ function Interface(name, interfaceTable) } else return getInterfaceObject(name):validateInterface(interfaceTable) end end -