-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcommon.lua
82 lines (68 loc) · 1.74 KB
/
common.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
local _M = {}
local function split(str, pat)
local t = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
function _M.incr_or_create(stats, key, count)
local newval, err = stats:incr(key, count)
if not newval and err == "not found" then
stats:add(key, 0)
stats:incr(key, count)
end
end
function _M.key(key_table)
return table.concat(key_table, ':')
end
function _M.in_table (value, table)
for _, v in pairs(table) do
if (v == value) then
return true
end
end
return false
end
function _M.format_response(key, value, response)
local path = split(tostring(key), ':')
key = table.remove(path, 1)
if key == nil or key == '' then
return value
elseif response[key] == nil then
response[key] = _M.format_response(_M.key(path), value, {})
elseif response[key] ~= nil then
response[key] = _M.format_response(_M.key(path), value, response[key])
end
return response
end
function _M.get_status_code_class(status)
if status:sub(1,1) == '1' then
return "1xx"
elseif status:sub(1,1) == '2' then
return "2xx"
elseif status:sub(1,1) == '3' then
return "3xx"
elseif status:sub(1,1) == '4' then
return "4xx"
elseif status:sub(1,1) == '5' then
return "5xx"
else
return "xxx"
end
end
function _M.update(stats, key, value)
stats:set(key, value)
end
return _M