Skip to content

Commit 06c9bd7

Browse files
committed
change: moved the code from etcd.path to etcd.lua.
1 parent 8a55ea4 commit 06c9bd7

File tree

4 files changed

+110
-108
lines changed

4 files changed

+110
-108
lines changed

lib/resty/etcd.lua

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
-- https://github.com/ledgetech/lua-resty-http
22
local HttpCli = require "resty.http"
3-
local normalize = require "resty.etcd.path"
43
local typeof = require "resty.etcd.typeof"
54
local encode_args = ngx.encode_args
65
local setmetatable = setmetatable
@@ -12,11 +11,70 @@ do
1211
end
1312
local clear_tab = require "table.clear"
1413
local tab_nkeys = require "table.nkeys"
14+
local split = require "ngx.re" .split
15+
local concat_tab = table.concat
16+
local tostring = tostring
17+
local select = select
18+
local ipairs = ipairs
19+
1520

1621
local _M = {}
1722
local mt = { __index = _M }
1823

1924

25+
local normalize
26+
do
27+
local items = {}
28+
local function concat(sep, ...)
29+
local argc = select('#', ...)
30+
clear_tab(items)
31+
local len = 0
32+
33+
for i = 1, argc do
34+
local v = select(i, ...)
35+
if v ~= nil then
36+
len = len + 1
37+
items[len] = tostring(v)
38+
end
39+
end
40+
41+
return concat_tab(items, sep);
42+
end
43+
44+
45+
local segs = {}
46+
function normalize(...)
47+
local path = concat('/', ...)
48+
local names = {}
49+
local err
50+
51+
segs, err = split(path, [[/]], "jo", nil, nil, segs)
52+
if not segs then
53+
return nil, err
54+
end
55+
56+
local len = 0
57+
for _, seg in ipairs(segs) do
58+
if seg == '..' then
59+
if len > 0 then
60+
len = len - 1
61+
end
62+
63+
elseif seg == '' or seg == '/' and names[len] == '/' then
64+
-- do nothing
65+
66+
elseif seg ~= '.' then
67+
len = len + 1
68+
names[len] = seg
69+
end
70+
end
71+
72+
return '/' .. concat_tab(names, '/', 1, len);
73+
end
74+
end
75+
_M.normalize = normalize
76+
77+
2078
function _M.new(opts)
2179
if opts == nil then
2280
opts = {}

lib/resty/etcd/path.lua

Lines changed: 0 additions & 56 deletions
This file was deleted.

lib/resty/etcd/typeof.lua

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,111 +8,111 @@ local function typeof(cmp, arg)
88
return cmp == type(arg)
99
end
1010

11-
local function typeofNil(...)
11+
local function typeof_nil(...)
1212
return typeof('nil', ...)
1313
end
1414

15-
local function typeofBoolean(...)
15+
local function typeof_bool(...)
1616
return typeof('boolean', ...)
1717
end
1818

19-
local function typeofString(...)
19+
local function typeof_str(...)
2020
return typeof('string', ...)
2121
end
2222

23-
local function typeofNumber(...)
23+
local function typeof_num(...)
2424
return typeof('number', ...)
2525
end
2626

27-
local function typeofFunction(...)
27+
local function typeof_fun(...)
2828
return typeof('function', ...)
2929
end
3030

31-
local function typeofTable(...)
31+
local function typeof_table(...)
3232
return typeof('table', ...)
3333
end
3434

35-
local function typeofThread(...)
35+
local function typeof_thread(...)
3636
return typeof('thread', ...)
3737
end
3838

39-
local function typeofUserdata(...)
39+
local function typeof_userdata(...)
4040
return typeof('userdata', ...)
4141
end
4242

43-
local function typeofFinite(arg)
43+
local function typeof_finite(arg)
4444
return type(arg) == 'number' and (arg < INFINITE_POS and arg > INFINITE_NEG)
4545
end
4646

47-
local function typeofUnsigned(arg)
47+
local function typeof_unsigned(arg)
4848
return type(arg) == 'number' and (arg < INFINITE_POS and arg >= 0)
4949
end
5050

51-
local function typeofInt(arg)
52-
return typeofFinite(arg) and rawequal(floor(arg), arg)
51+
local function typeof_int(arg)
52+
return typeof_finite(arg) and rawequal(floor(arg), arg)
5353
end
5454

55-
local function typeofInt8(arg)
56-
return typeofInt(arg) and arg >= -128 and arg <= 127
55+
local function typeof_int8(arg)
56+
return typeof_int(arg) and arg >= -128 and arg <= 127
5757
end
5858

59-
local function typeofInt16(arg)
60-
return typeofInt(arg) and arg >= -32768 and arg <= 32767
59+
local function typeof_int16(arg)
60+
return typeof_int(arg) and arg >= -32768 and arg <= 32767
6161
end
6262

63-
local function typeofInt32(arg)
64-
return typeofInt(arg) and arg >= -2147483648 and arg <= 2147483647
63+
local function typeof_int32(arg)
64+
return typeof_int(arg) and arg >= -2147483648 and arg <= 2147483647
6565
end
6666

67-
local function typeofUInt(arg)
68-
return typeofUnsigned(arg) and rawequal(floor(arg), arg)
67+
local function typeof_uint(arg)
68+
return typeof_unsigned(arg) and rawequal(floor(arg), arg)
6969
end
7070

71-
local function typeofUInt8(arg)
72-
return typeofUInt(arg) and arg <= 255
71+
local function typeof_uint8(arg)
72+
return typeof_uint(arg) and arg <= 255
7373
end
7474

75-
local function typeofUInt16(arg)
76-
return typeofUInt(arg) and arg <= 65535
75+
local function typeof_uint16(arg)
76+
return typeof_uint(arg) and arg <= 65535
7777
end
7878

79-
local function typeofUInt32(arg)
80-
return typeofUInt(arg) and arg <= 4294967295
79+
local function typeof_uint32(arg)
80+
return typeof_uint(arg) and arg <= 4294967295
8181
end
8282

83-
local function typeofNaN(arg)
83+
local function typeof_nan(arg)
8484
return arg ~= arg
8585
end
8686

87-
local function typeofNon(arg)
87+
local function typeof_non(arg)
8888
return arg == nil or arg == false or arg == 0 or arg == '' or arg ~= arg
8989
end
9090

9191

9292
local _M = {
93-
['nil'] = typeofNil,
94-
['boolean'] = typeofBoolean,
95-
['string'] = typeofString,
96-
['number'] = typeofNumber,
97-
['function'] = typeofFunction,
98-
['table'] = typeofTable,
99-
['thread'] = typeofThread,
100-
['userdata'] = typeofUserdata,
101-
['finite'] = typeofFinite,
102-
['unsigned'] = typeofUnsigned,
103-
['int'] = typeofInt,
104-
['int8'] = typeofInt8,
105-
['int16'] = typeofInt16,
106-
['int32'] = typeofInt32,
107-
['uint'] = typeofUInt,
108-
['uint8'] = typeofUInt8,
109-
['uint16'] = typeofUInt16,
110-
['uint32'] = typeofUInt32,
111-
['nan'] = typeofNaN,
112-
['non'] = typeofNon,
93+
['nil'] = typeof_nil,
94+
['boolean'] = typeof_bool,
95+
['string'] = typeof_str,
96+
['number'] = typeof_num,
97+
['function'] = typeof_fun,
98+
['table'] = typeof_table,
99+
['thread'] = typeof_thread,
100+
['userdata'] = typeof_userdata,
101+
['finite'] = typeof_finite,
102+
['unsigned'] = typeof_unsigned,
103+
['int'] = typeof_int,
104+
['int8'] = typeof_int8,
105+
['int16'] = typeof_int16,
106+
['int32'] = typeof_int32,
107+
['uint'] = typeof_uint,
108+
['uint8'] = typeof_uint8,
109+
['uint16'] = typeof_uint16,
110+
['uint32'] = typeof_uint32,
111+
['nan'] = typeof_nan,
112+
['non'] = typeof_non,
113113
-- alias
114-
['Nil'] = typeofNil,
115-
['Function'] = typeofFunction
114+
['Nil'] = typeof_nil,
115+
['Function'] = typeof_fun
116116
}
117117

118118
return _M

t/normalize.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ __DATA__
1717
--- config
1818
location /t {
1919
content_by_lua_block {
20-
local normalize = require "resty.etcd.path" .normalize
20+
local normalize = require "resty.etcd" .normalize
2121
2222
local function ifNotEqual(l, r)
2323
if l ~= r then

0 commit comments

Comments
 (0)