Skip to content

Commit

Permalink
change: added an example of plugin. (#2)
Browse files Browse the repository at this point in the history
* change: added an example of plugin.
  • Loading branch information
membphis authored May 6, 2019
1 parent a18afea commit 1703fc6
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 0 deletions.
121 changes: 121 additions & 0 deletions lua/apimeta/comm/typeof.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
-- TODO: need to support more restriction rules

local INFINITE_POS = math.huge
local INFINITE_NEG = -INFINITE_POS
local type = type
local floor = math.floor
local rawequal = rawequal

local function typeof(cmp, arg)
return cmp == type(arg)
end

local function typeofNil(...)
return typeof('nil', ...)
end

local function typeofBoolean(...)
return typeof('boolean', ...)
end

local function typeofString(...)
return typeof('string', ...)
end

local function typeofNumber(...)
return typeof('number', ...)
end

local function typeofFunction(...)
return typeof('function', ...)
end

local function typeofTable(...)
return typeof('table', ...)
end

local function typeofThread(...)
return typeof('thread', ...)
end

local function typeofUserdata(...)
return typeof('userdata', ...)
end

local function typeofFinite(arg)
return type(arg) == 'number' and (arg < INFINITE_POS and arg > INFINITE_NEG)
end

local function typeofUnsigned(arg)
return type(arg) == 'number' and (arg < INFINITE_POS and arg >= 0)
end

local function typeofInt(arg)
return typeofFinite(arg) and rawequal(floor(arg), arg)
end

local function typeofInt8(arg)
return typeofInt(arg) and arg >= -128 and arg <= 127
end

local function typeofInt16(arg)
return typeofInt(arg) and arg >= -32768 and arg <= 32767
end

local function typeofInt32(arg)
return typeofInt(arg) and arg >= -2147483648 and arg <= 2147483647
end

local function typeofUInt(arg)
return typeofUnsigned(arg) and rawequal(floor(arg), arg)
end

local function typeofUInt8(arg)
return typeofUInt(arg) and arg <= 255
end

local function typeofUInt16(arg)
return typeofUInt(arg) and arg <= 65535
end

local function typeofUInt32(arg)
return typeofUInt(arg) and arg <= 4294967295
end

local function typeofNaN(arg)
return arg ~= arg
end

local function typeofNon(arg)
return arg == nil or arg == false or arg == 0 or arg == '' or arg ~= arg
end


local _M = {
['nil'] = typeofNil,
['boolean'] = typeofBoolean,
['string'] = typeofString,
['number'] = typeofNumber,
['function'] = typeofFunction,
['table'] = typeofTable,
['thread'] = typeofThread,
['userdata'] = typeofUserdata,
['finite'] = typeofFinite,
['unsigned'] = typeofUnsigned,
['int'] = typeofInt,
['int8'] = typeofInt8,
['int16'] = typeofInt16,
['int32'] = typeofInt32,
['uint'] = typeofUInt,
['uint8'] = typeofUInt8,
['uint16'] = typeofUInt16,
['uint32'] = typeofUInt32,
['nan'] = typeofNaN,
['non'] = typeofNon,
-- alias
['Nil'] = typeofNil,
['Function'] = typeofFunction
}


return _M
23 changes: 23 additions & 0 deletions lua/apimeta/plugin.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
local typeof = require("apimeta.comm.typeof")
local log = require("apimeta.comm.log")
local tostring = tostring


local _M = {
log = log,
}


function _M.check_args(args, scheme)
for k, v in pairs(scheme) do
if not typeof[v](args[k]) then
return nil, "args." .. k .. " expect " .. v .. " value but got: ["
.. tostring(args[k]) .. "]"
end
end

return true
end


return _M
43 changes: 43 additions & 0 deletions lua/apimeta/plugins/example_plugin.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
local plugin = require("apimeta.plugin")


local args_schema = {
i = "int", -- value list: apimeta.comm.typeof#92
s = "string",
t = "table",
}


local _M = {version = 0.1}


function _M.check_args(config)
local ok, err = plugin.check_args(config, args_schema)
if not ok then
return false, err
end

-- add more restriction rules if we needs

return true
end


function _M.init(config)
plugin.log.warn("plugin init")

local ok, err = _M.check_args(config)
if not ok then
return false, err
end

return true
end


function _M.rewrite(ctx)
plugin.log.warn("plugin rewrite phase")
return true
end

return _M
46 changes: 46 additions & 0 deletions t/example-plugin.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use t::APIMeta 'no_plan';

repeat_each(1);
no_long_string();
no_shuffle();
log_level('info');

run_tests;

__DATA__

=== TEST 1: sanity
--- config
location /t {
content_by_lua_block {
local plugin = require("apimeta.plugins.example_plugin")
local ok, err = plugin.check_args({i = 1, s = "s", t = {}})
if not ok then
ngx.say("failed to check args: ", err)
end

ok, err = plugin.check_args({s = "s", t = {}})
if not ok then
ngx.say("failed to check args: ", err)
end

ok, err = plugin.check_args({i = 1, s = 3, t = {}})
if not ok then
ngx.say("failed to check args: ", err)
end

ok, err = plugin.check_args({i = 1, s = "s", t = ""})
if not ok then
ngx.say("failed to check args: ", err)
end

ngx.say("done")
}
}
--- request
GET /t
--- response_body
failed to check args: args.i expect int value but got: [nil]
failed to check args: args.s expect string value but got: [3]
failed to check args: args.t expect table value but got: []
done

0 comments on commit 1703fc6

Please sign in to comment.