diff --git a/interface.lua b/interface.lua index e2c605d..50e572c 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' 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 @@ -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', @@ -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, @@ -83,4 +129,3 @@ function Interface(name, interfaceTable) } else return getInterfaceObject(name):validateInterface(interfaceTable) end end -