Difference between revisions of "Module:LibraryUtil"

From Minetest Wiki
Jump to navigation Jump to search
m
m
Line 1: Line 1:
 
--[[
 
--[[
 
     Code released under the GPL v2+ as per:
 
     Code released under the GPL v2+ as per:
https://github.com/wikimedia/mediawiki-extensions-Scribunto/blob/7d676c3/COPYING
+
    https://github.com/wikimedia/mediawiki-extensions-Scribunto/blob/7d676c3/COPYING
  
@license GNU GPL v2+
+
    @license GNU GPL v2+
@author Brad Jorsch < bjorsch@wikimedia.org >
+
    @author Brad Jorsch < bjorsch@wikimedia.org >
@author "Mr. Stradivarius" < misterstrad@gmail.com >
+
    @author "Mr. Stradivarius" < misterstrad@gmail.com >
 
]]
 
]]
  
Line 11: Line 11:
  
 
function LibraryUtil.checkType( name, argIdx, arg, expectType, nilOk )
 
function LibraryUtil.checkType( name, argIdx, arg, expectType, nilOk )
if arg == nil and nilOk then
+
    if arg == nil and nilOk then
return
+
        return
end
+
    end
if type( arg ) ~= expectType then
+
    if type( arg ) ~= expectType then
local msg = string.format( "bad argument #%d to '%s' (%s expected, got %s)",
+
        local msg = string.format( "bad argument #%d to '%s' (%s expected, got %s)",
argIdx, name, expectType, type( arg )
+
            argIdx, name, expectType, type( arg )
)
+
        )
error( msg, 3 )
+
        error( msg, 3 )
end
+
    end
 
end
 
end
  
 
function LibraryUtil.checkTypeMulti( name, argIdx, arg, expectTypes )
 
function LibraryUtil.checkTypeMulti( name, argIdx, arg, expectTypes )
local argType = type( arg )
+
    local argType = type( arg )
for _, expectType in ipairs( expectTypes ) do
+
    for _, expectType in ipairs( expectTypes ) do
if argType == expectType then
+
        if argType == expectType then
return
+
            return
end
+
        end
end
+
    end
local n = #expectTypes
+
    local n = #expectTypes
local typeList
+
    local typeList
if n > 1 then
+
    if n > 1 then
typeList = table.concat( expectTypes, ', ', 1, n - 1 ) .. ' or ' .. expectTypes[n]
+
        typeList = table.concat( expectTypes, ', ', 1, n - 1 ) .. ' or ' .. expectTypes[n]
else
+
    else
typeList = expectTypes[1]
+
        typeList = expectTypes[1]
end
+
    end
local msg = string.format( "bad argument #%d to '%s' (%s expected, got %s)",
+
    local msg = string.format( "bad argument #%d to '%s' (%s expected, got %s)",
argIdx,
+
        argIdx,
name,
+
        name,
typeList,
+
        typeList,
type( arg )
+
        type( arg )
)
+
    )
error( msg, 3 )
+
    error( msg, 3 )
 
end
 
end
  
 
function LibraryUtil.checkTypeForIndex( index, value, expectType )
 
function LibraryUtil.checkTypeForIndex( index, value, expectType )
if type( value ) ~= expectType then
+
    if type( value ) ~= expectType then
local msg = string.format( "value for index '%s' must be %s, %s given",
+
        local msg = string.format( "value for index '%s' must be %s, %s given",
index, expectType, type( value )
+
            index, expectType, type( value )
)
+
        )
error( msg, 3 )
+
        error( msg, 3 )
end
+
    end
 
end
 
end
  
 
function LibraryUtil.checkTypeForNamedArg( name, argName, arg, expectType, nilOk )
 
function LibraryUtil.checkTypeForNamedArg( name, argName, arg, expectType, nilOk )
if arg == nil and nilOk then
+
    if arg == nil and nilOk then
return
+
        return
end
+
    end
if type( arg ) ~= expectType then
+
    if type( arg ) ~= expectType then
local msg = string.format( "bad named argument %s to '%s' (%s expected, got %s)",
+
        local msg = string.format( "bad named argument %s to '%s' (%s expected, got %s)",
argName, name, expectType, type( arg )
+
            argName, name, expectType, type( arg )
)
+
        )
error( msg, 3 )
+
        error( msg, 3 )
end
+
    end
 
end
 
end
  
 
function LibraryUtil.makeCheckSelfFunction( libraryName, varName, selfObj, selfObjDesc )
 
function LibraryUtil.makeCheckSelfFunction( libraryName, varName, selfObj, selfObjDesc )
return function ( self, method )
+
    return function ( self, method )
if self ~= selfObj then
+
        if self ~= selfObj then
error( string.format(
+
            error( string.format(
"%s: invalid %s. Did you call %s with a dot instead of a colon, i.e. " ..
+
                "%s: invalid %s. Did you call %s with a dot instead of a colon, i.e. " ..
"%s.%s() instead of %s:%s()?",
+
                "%s.%s() instead of %s:%s()?",
libraryName, selfObjDesc, method, varName, method, varName, method
+
                libraryName, selfObjDesc, method, varName, method, varName, method
), 3 )
+
            ), 3 )
end
+
        end
end
+
    end
 
end
 
end
  
 
return LibraryUtil
 
return LibraryUtil

Revision as of 06:03, 9 April 2017

--[[

   Code released under the GPL v2+ as per:
   https://github.com/wikimedia/mediawiki-extensions-Scribunto/blob/7d676c3/COPYING
   @license GNU GPL v2+
   @author Brad Jorsch < bjorsch@wikimedia.org >
   @author "Mr. Stradivarius" < misterstrad@gmail.com >

]]

local LibraryUtil = {}

function LibraryUtil.checkType( name, argIdx, arg, expectType, nilOk )

   if arg == nil and nilOk then
       return
   end
   if type( arg ) ~= expectType then
       local msg = string.format( "bad argument #%d to '%s' (%s expected, got %s)",
           argIdx, name, expectType, type( arg )
       )
       error( msg, 3 )
   end

end

function LibraryUtil.checkTypeMulti( name, argIdx, arg, expectTypes )

   local argType = type( arg )
   for _, expectType in ipairs( expectTypes ) do
       if argType == expectType then
           return
       end
   end
   local n = #expectTypes
   local typeList
   if n > 1 then
       typeList = table.concat( expectTypes, ', ', 1, n - 1 ) .. ' or ' .. expectTypes[n]
   else
       typeList = expectTypes[1]
   end
   local msg = string.format( "bad argument #%d to '%s' (%s expected, got %s)",
       argIdx,
       name,
       typeList,
       type( arg )
   )
   error( msg, 3 )

end

function LibraryUtil.checkTypeForIndex( index, value, expectType )

   if type( value ) ~= expectType then
       local msg = string.format( "value for index '%s' must be %s, %s given",
           index, expectType, type( value )
       )
       error( msg, 3 )
   end

end

function LibraryUtil.checkTypeForNamedArg( name, argName, arg, expectType, nilOk )

   if arg == nil and nilOk then
       return
   end
   if type( arg ) ~= expectType then
       local msg = string.format( "bad named argument %s to '%s' (%s expected, got %s)",
           argName, name, expectType, type( arg )
       )
       error( msg, 3 )
   end

end

function LibraryUtil.makeCheckSelfFunction( libraryName, varName, selfObj, selfObjDesc )

   return function ( self, method )
       if self ~= selfObj then
           error( string.format(
               "%s: invalid %s. Did you call %s with a dot instead of a colon, i.e. " ..
               "%s.%s() instead of %s:%s()?",
               libraryName, selfObjDesc, method, varName, method, varName, method
           ), 3 )
       end
   end

end

return LibraryUtil