Skip to content

Commit 8a55ea4

Browse files
committed
change: code style.
1 parent 9e45887 commit 8a55ea4

File tree

2 files changed

+34
-42
lines changed

2 files changed

+34
-42
lines changed

lib/resty/etcd.lua

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
local normalize = require "resty.etcd.path" .normalize
2-
local typeof = require "resty.etcd.typeof"
31
-- https://github.com/ledgetech/lua-resty-http
42
local HttpCli = require "resty.http"
3+
local normalize = require "resty.etcd.path"
4+
local typeof = require "resty.etcd.typeof"
55
local encode_args = ngx.encode_args
66
local setmetatable = setmetatable
77
local decode_json, encode_json
@@ -24,7 +24,7 @@ function _M.new(opts)
2424
elseif not typeof.table(opts) then
2525
return nil, 'opts must be table'
2626
end
27-
27+
2828
local timeout = opts.timeout or 5000 -- 5 sec
2929
local http_host = opts.host or "http://127.0.0.1:2379"
3030
local ttl = opts.ttl or -1
@@ -146,22 +146,22 @@ local function set(self, key, val, attr)
146146
-- ngx.log(ngx.WARN, require("cjson").encode(opts))
147147

148148
-- todo: check arguments
149-
149+
150150
-- verify key
151151
key = normalize(key)
152152
if key == '/' then
153153
return nil, "key should not be a slash"
154154
end
155-
156-
local res, err = _request(attr.inOrder and 'POST' or 'PUT',
157-
self.endpoints.full_prefix .. key,
155+
156+
local res, err = _request(attr.inOrder and 'POST' or 'PUT',
157+
self.endpoints.full_prefix .. key,
158158
opts, self.timeout)
159159
if err then
160160
return nil, err
161161
end
162-
162+
163163
-- get
164-
if res.status < 300 and res.body.node and
164+
if res.status < 300 and res.body.node and
165165
not res.body.node.dir then
166166
res.body.node.value, err = decode_json(res.body.node.value)
167167
if err then
@@ -185,7 +185,7 @@ local function get(self, key, attr)
185185
if attr.recursive then
186186
attr_recursive = attr.recursive and 'true' or 'false'
187187
end
188-
188+
189189
opts = {
190190
query = {
191191
wait = attr_wait,
@@ -196,7 +196,7 @@ local function get(self, key, attr)
196196
}
197197
end
198198

199-
local res, err = _request("GET",
199+
local res, err = _request("GET",
200200
self.endpoints.full_prefix .. normalize(key),
201201
opts, attr and attr.timeout or self.timeout)
202202
if err then
@@ -206,7 +206,7 @@ local function get(self, key, attr)
206206
-- readdir
207207
if attr and attr.dir then
208208
-- set 404 not found if result node is not directory
209-
if res.status == 200 and res.body.node and
209+
if res.status == 200 and res.body.node and
210210
not res.body.node.dir then
211211
res.status = 404
212212
res.body.node.dir = false
@@ -229,14 +229,14 @@ local function get(self, key, attr)
229229
ngx.log(ngx.WARN, "read addr: ", encode_json(res.body.node))
230230

231231
-- get
232-
elseif res.status == 200 and res.body.node and
232+
elseif res.status == 200 and res.body.node and
233233
not res.body.node.dir then
234234
res.body.node.value, err = decode_json(res.body.node.value)
235235
if err then
236236
return nil, err
237237
end
238238
end
239-
239+
240240
return res
241241
end
242242

@@ -271,8 +271,8 @@ local function delete(self, key, attr)
271271

272272
-- todo: check arguments
273273

274-
return _request("DELETE",
275-
self.endpoints.full_prefix .. normalize(key),
274+
return _request("DELETE",
275+
self.endpoints.full_prefix .. normalize(key),
276276
opts, self.timeout)
277277
end
278278

@@ -292,7 +292,7 @@ function _M.wait(self, key, modifiedIndex, timeout)
292292
attr.wait = true
293293
attr.waitIndex = modifiedIndex
294294
attr.timeout = timeout
295-
295+
296296
return get(self, key, attr)
297297
end
298298

@@ -312,7 +312,7 @@ function _M.waitdir(self, key, modifiedIndex, timeout)
312312
attr.recursive = true
313313
attr.waitIndex = modifiedIndex
314314
attr.timeout = timeout
315-
315+
316316
return get(self, key, attr)
317317
end
318318

lib/resty/etcd/path.lua

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,56 @@
11
local clear_tab = require "table.clear"
2+
local split = require "ngx.re" .split
23
local concat_tab = table.concat
34
local tostring = tostring
45
local select = select
5-
local split = require "ngx.re" .split
66
local ipairs = ipairs
77

88

9-
local _M = {}
10-
11-
local normalize
12-
do
13-
local res = {}
9+
local items = {}
1410
local function concat(sep, ...)
1511
local argc = select('#', ...)
16-
clear_tab(res)
12+
clear_tab(items)
1713
local len = 0
1814

1915
for i = 1, argc do
2016
local v = select(i, ...)
2117
if v ~= nil then
2218
len = len + 1
23-
res[len] = tostring(v)
19+
items[len] = tostring(v)
2420
end
2521
end
2622

27-
return concat_tab(res, sep);
23+
return concat_tab(items, sep);
2824
end
2925

26+
3027
local segs = {}
31-
function _M.normalize(...)
28+
return function (...)
3229
local path = concat('/', ...)
3330
-- ngx.log(ngx.WARN, "path: ", path)
34-
local res = {}
35-
local len = 0
31+
local names = {}
32+
local err
3633

37-
local segs, err = split(path, [[/]], "jo", nil, nil, segs)
34+
segs, err = split(path, [[/]], "jo", nil, nil, segs)
3835
if not segs then
3936
return nil, err
4037
end
41-
-- ngx.log(ngx.WARN, "segs: ", require("cjson").encode(segs))
4238

43-
for i,seg in ipairs(segs) do
39+
local len = 0
40+
for _, seg in ipairs(segs) do
4441
if seg == '..' then
4542
if len > 0 then
4643
len = len - 1
4744
end
48-
49-
elseif seg == '' or seg == '/' and res[len] == '/' then
45+
46+
elseif seg == '' or seg == '/' and names[len] == '/' then
5047
-- do nothing
5148

5249
elseif seg ~= '.' then
5350
len = len + 1
54-
res[len] = seg
51+
names[len] = seg
5552
end
5653
end
5754

58-
return '/' .. concat_tab(res, '/', 1, len);
55+
return '/' .. concat_tab(names, '/', 1, len);
5956
end
60-
61-
end -- do
62-
63-
64-
return _M

0 commit comments

Comments
 (0)