Module:IP: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
381 bytes removed ,  7 years ago
use libraryUtil for the checkSelf function
(prevent access to IPAddress metatables, and add some more comments)
(use libraryUtil for the checkSelf function)
Line 8:
local checkType = libraryUtil.checkType
local checkTypeMulti = libraryUtil.checkTypeMulti
local makeCheckSelfFunction = libraryUtil.makeCheckSelfFunction
 
-- Constants
Line 17 ⟶ 18:
--------------------------------------------------------------------------------
 
local function makeCheckSelfFuncmakeValidationFunction(className, objectName, isSelfFuncisObjectFunc)
-- Makes a function for classes to check that their methods have a valid
-- self parameter.
return function (methodName, selfObject)
if not isSelfFunc(selfObject) then
error(string.format(
'invalid %s object. Did you call %s with a dot instead of a colon, i.e. %s.%s() instead of %s:%s()?',
className, methodName, objectName, methodName, objectName, methodName
), 3)
end
end
end
 
local function makeValidationFunc(className, isObjectFunc)
-- Make a function for validating a specific object.
return function (methodName, argIdx, arg)
Line 368 ⟶ 356:
end
 
validateIPAddress = makeValidationFuncmakeValidationFunction('IPAddress', isIPAddressObject)
-- A function to check whether methods are called with a valid self
-- parameter.
local checkSelf = makeCheckSelfFunc(
'IPAddress',
'ipAddress',
isIPAddressObject
)
 
validateIPAddress = makeValidationFunc('IPAddress', isIPAddressObject)
 
-- Metamethods that don't need upvalues
Line 407 ⟶ 387:
local data = {}
data.rawIP = rawIP
 
-- Makes aA function for classes to check that theirwhether methods haveare called with a valid self
-- self parameter.
local checkSelf = makeCheckSelfFuncmakeCheckSelfFunction(
'IP',
'ipAddress',
obj,
'IPAddress object',
)
 
-- Public methods
function obj:getIP()
checkSelf(self, 'getIP', self)
return tostring(data.rawIP)
end
 
function obj:getVersion()
checkSelf(self, 'getVersion', self)
return data.rawIP:getVersion()
end
 
function obj:isIPv4()
checkSelf(self, 'isIPv4', self)
return data.rawIP:isIPv4()
end
 
function obj:isIPv6()
checkSelf(self, 'isIPv6', self)
return data.rawIP:isIPv6()
end
 
function obj:isInSubnet(subnet)
checkSelf(self, 'isInSubnet', self)
local tp = type(subnet)
if tp == 'string' then
Line 443 ⟶ 432:
 
function obj:getSubnet(bitLength)
checkSelf(self, 'getSubnet', self)
checkType('getSubnet', 1, bitLength, 'number')
return makeSubnetFromRaw(data.rawIP, bitLength)
Line 449 ⟶ 438:
 
function obj:getNextIP()
checkSelf(self, 'getNextIP', self)
return makeIPAddressFromRaw(data.rawIP:getAdjacent())
end
 
function obj:getPreviousIP()
checkSelf(self, 'getPreviousIP', self)
return makeIPAddressFromRaw(data.rawIP:getAdjacent(true))
end
Line 523 ⟶ 512:
-- argIdx (number) - the position of the argument in the argument list
-- arg - the argument to be validated
validateSubnet = makeValidationFuncmakeValidationFunction('Subnet', isSubnetObject)
 
-- Constructors
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu