Skip to content

Commit ab9acc9

Browse files
authored
feat: support original string value via a serializer option (#72)
travis: replace `make dev` with `luarocks make rockspec/lua-resty-etcd-master-0.1-0.rockspec`
1 parent 6ef8cc7 commit ab9acc9

File tree

12 files changed

+244
-39
lines changed

12 files changed

+244
-39
lines changed

.luacheckrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
std = 'ngx_lua'
2+
3+
ignore={
4+
"542"
5+
}

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ env:
3636

3737
install:
3838
- git clone https://github.com/openresty/test-nginx.git test-nginx
39-
- sudo make dev
39+
#- sudo make dev
40+
- sudo luarocks make rockspec/lua-resty-etcd-master-0.1-0.rockspec
4041
- wget -qO - https://openresty.org/package/pubkey.gpg | sudo apt-key add -
4142
- sudo apt-get -y install software-properties-common
4243
- sudo add-apt-repository -y "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main"

api_v2.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Method
3535
append this prefix path string to key operation url `'/v2/keys'`.
3636
- `timeout`: int
3737
request timeout seconds.
38+
- `serializer`: string - serializer type, default `json`, also support `raw` to keep origin string value.
3839

3940
The client methods returns either a `HTTP Response Entity` or an `error string`.
4041

@@ -280,4 +281,4 @@ Gets the self statistics info.
280281

281282
Gets the store statistics info.
282283

283-
[Back to TOP](#api-v2)
284+
[Back to TOP](#api-v2)

lib/resty/etcd.lua

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
local etcdv2 = require("resty.etcd.v2")
2-
local etcdv3 = require("resty.etcd.v3")
3-
local utils = require("resty.etcd.utils")
4-
local typeof = require("typeof")
1+
local etcdv2 = require("resty.etcd.v2")
2+
local etcdv3 = require("resty.etcd.v3")
3+
local utils = require("resty.etcd.utils")
4+
local typeof = require("typeof")
5+
local require = require
6+
local pcall = pcall
57
local prefix_v3 = {
68
["3.5."] = "/v3",
79
["3.4."] = "/v3",
@@ -27,6 +29,16 @@ local function etcd_version(opts)
2729
return ver.body
2830
end
2931

32+
local function require_serializer(serializer_name)
33+
if serializer_name then
34+
local ok, module = pcall(require, "resty.etcd.serializers." .. serializer_name)
35+
if ok then
36+
return module
37+
end
38+
end
39+
40+
return require("resty.etcd.serializers.json")
41+
end
3042

3143
function _M.new(opts)
3244
opts = opts or {}
@@ -54,6 +66,8 @@ function _M.new(opts)
5466
end
5567

5668
opts.api_prefix = "/v2"
69+
local serializer_name = typeof.string(opts.serializer) and opts.serializer
70+
opts.serializer = require_serializer(serializer_name)
5771

5872
return etcdv2.new(opts)
5973
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
local cjson = require("cjson.safe")
2+
3+
4+
return {
5+
serialize = cjson.encode,
6+
deserialize = cjson.decode
7+
}

lib/resty/etcd/serializers/raw.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local type = type
2+
3+
4+
local function raw_encode(v)
5+
local t = type(v)
6+
if t ~= 'string' then
7+
return nil, 'unsupported type for ' .. t
8+
end
9+
return v
10+
end
11+
12+
local function raw_decode(v)
13+
return v
14+
end
15+
16+
17+
return {
18+
serialize = raw_encode,
19+
deserialize = raw_decode
20+
}

lib/resty/etcd/utils.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function _M.has_value(arr, val)
7474
return key
7575
end
7676
end
77-
77+
7878
return false
7979
end
8080

lib/resty/etcd/v2.lua

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,18 @@ local cjson = require("cjson.safe")
55
local encode_args = ngx.encode_args
66
local setmetatable = setmetatable
77
local clear_tab = require("table.clear")
8-
local tostring = tostring
98
local ipairs = ipairs
109
local type = type
1110
local utils = require("resty.etcd.utils")
1211
local encode_base64 = ngx.encode_base64
1312
local require = require
1413
local next = next
1514
local table = table
15+
local decode_json = cjson.decode
1616
local INIT_COUNT_RESIZE = 2e8
1717

1818

19-
local _M = {
20-
decode_json = cjson.decode,
21-
encode_json = cjson.encode,
22-
}
19+
local _M = {}
2320

2421

2522
local mt = { __index = _M }
@@ -49,6 +46,7 @@ function _M.new(opts)
4946
local http_host = opts.http_host
5047
local user = opts.user
5148
local password = opts.password
49+
local serializer = opts.serializer
5250

5351
if not typeof.uint(timeout) then
5452
return nil, 'opts.timeout must be unsigned integer'
@@ -107,7 +105,8 @@ function _M.new(opts)
107105
is_cluster = #endpoints > 1,
108106
user = user,
109107
password = password,
110-
endpoints = endpoints
108+
endpoints = endpoints,
109+
serializer = serializer
111110
},
112111
mt)
113112

@@ -194,16 +193,19 @@ local function _request(self, method, uri, opts, timeout)
194193
return res
195194
end
196195

197-
res.body = self.decode_json(res.body)
196+
res.body = decode_json(res.body)
198197
return res
199198
end
200199

201200

202201
local function set(self, key, val, attr)
203202
local err
204-
val, err = self.encode_json(val)
205-
if not val then
206-
return nil, err
203+
if val then
204+
val, err = self.serializer.serialize(val)
205+
206+
if err then
207+
return nil, err
208+
end
207209
end
208210

209211
local prev_exist
@@ -245,9 +247,9 @@ local function set(self, key, val, attr)
245247

246248
-- get
247249
if res.status < 300 and res.body.node and not res.body.node.dir then
248-
res.body.node.value, err = self.decode_json(res.body.node.value)
250+
res.body.node.value, err = self.serializer.deserialize(res.body.node.value)
249251
if err then
250-
utils.log_error("failed to json decode value of node: ", err)
252+
utils.log_error("failed to deserialize value of node: ", err)
251253
return res, err
252254
end
253255
end
@@ -269,9 +271,9 @@ local function decode_dir_value(self, body_node)
269271
for _, node in ipairs(body_node.nodes) do
270272
local val = node.value
271273
if type(val) == "string" then
272-
node.value, err = self.decode_json(val)
274+
node.value, err = self.serializer.deserialize(val)
273275
if err then
274-
utils.log_error("failed to decode json: ", err)
276+
utils.log_error("failed to deserialize: ", err)
275277
end
276278
end
277279

@@ -325,9 +327,9 @@ local function get(self, key, attr)
325327
if not ok then
326328
local val = res.body.node.value
327329
if type(val) == "string" then
328-
res.body.node.value, err = self.decode_json(val)
330+
res.body.node.value, err = self.serializer.deserialize(val)
329331
if err then
330-
utils.log_error("failed to json decode: ", err)
332+
utils.log_error("failed to deserialize: ", err)
331333
end
332334
end
333335
end
@@ -340,7 +342,7 @@ end
340342
local function delete(self, key, attr)
341343
local val, err = attr.prev_value
342344
if val ~= nil and type(val) ~= "number" then
343-
val, err = self.encode_json(val)
345+
val, err = self.serializer.serialize(val)
344346
if not val then
345347
return nil, err
346348
end

lib/resty/etcd/v3.lua

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,10 @@ local function get(self, key, attr)
381381
}
382382
}
383383

384-
local res, err = _request_uri(self, "POST",
385-
choose_endpoint(self).full_prefix .. "/kv/range",
386-
opts, attr and attr.timeout or self.timeout)
384+
local res
385+
res, err = _request_uri(self, "POST",
386+
choose_endpoint(self).full_prefix .. "/kv/range",
387+
opts, attr and attr.timeout or self.timeout)
387388

388389
if res and res.status == 200 then
389390
if res.body.kvs and tab_nkeys(res.body.kvs) > 0 then
@@ -513,7 +514,7 @@ local function request_chunk(self, method, host, port, path, opts, timeout)
513514
end
514515

515516
return function()
516-
local body, err = res.body_reader()
517+
body, err = res.body_reader()
517518
if not body then
518519
return nil, err
519520
end
@@ -664,7 +665,7 @@ end
664665
function _M.readdir(self, key, opts)
665666

666667
clear_tab(attr)
667-
668+
668669
key = utils.get_real_key(self.key_prefix, key)
669670

670671
attr.range_end = get_range_end(key)
@@ -681,9 +682,9 @@ end
681682

682683
function _M.watchdir(self, key, opts)
683684
clear_tab(attr)
684-
685+
685686
key = utils.get_real_key(self.key_prefix, key)
686-
687+
687688
attr.range_end = get_range_end(key)
688689
attr.start_revision = opts and opts.start_revision
689690
attr.timeout = opts and opts.timeout
@@ -704,7 +705,7 @@ do
704705
function _M.set(self, key, val, opts)
705706

706707
clear_tab(attr)
707-
708+
708709
key = utils.get_real_key(self.key_prefix, key)
709710

710711
attr.timeout = opts and opts.timeout
@@ -722,7 +723,7 @@ end
722723
local failure = {}
723724
function _M.setnx(self, key, val, opts)
724725
clear_tab(compare)
725-
726+
726727
key = utils.get_real_key(self.key_prefix, key)
727728

728729
compare[1] = {}
@@ -748,7 +749,7 @@ end
748749
-- set key-val and ttl if key is exists (update)
749750
function _M.setx(self, key, val, opts)
750751
clear_tab(compare)
751-
752+
752753
key = utils.get_real_key(self.key_prefix, key)
753754

754755
compare[1] = {}
@@ -934,7 +935,7 @@ do
934935
local attr = {}
935936
function _M.delete(self, key, opts)
936937
clear_tab(attr)
937-
938+
938939
key = utils.get_real_key(self.key_prefix, key)
939940

940941
attr.timeout = opts and opts.timeout
@@ -945,7 +946,7 @@ end
945946

946947
function _M.rmdir(self, key, opts)
947948
clear_tab(attr)
948-
949+
949950
key = utils.get_real_key(self.key_prefix, key)
950951

951952
attr.range_end = get_range_end(key)

rockspec/lua-resty-etcd-master-0.1-0.rockspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ build = {
2424
["resty.etcd.v2"] = "lib/resty/etcd/v2.lua",
2525
["resty.etcd.v3"] = "lib/resty/etcd/v3.lua",
2626
["resty.etcd.utils"] = "lib/resty/etcd/utils.lua",
27+
["resty.etcd.serializers.json"] = "lib/resty/etcd/serializers/json.lua",
28+
["resty.etcd.serializers.raw"] = "lib/resty/etcd/serializers/raw.lua",
2729
}
2830
}

0 commit comments

Comments
 (0)