Skip to content

Commit 8ae9737

Browse files
committed
Move require_serializer into v3
* Fix #126 * cause to conform to v3.md Signed-off-by: u5surf <[email protected]>
1 parent 29de8bd commit 8ae9737

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

lib/resty/etcd.lua

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
11
local etcdv3 = require("resty.etcd.v3")
22
local typeof = require("typeof")
3-
local require = require
4-
local pcall = pcall
53

64
local _M = {version = 0.9}
75

86

9-
local function require_serializer(serializer_name)
10-
if serializer_name then
11-
local ok, module = pcall(require, "resty.etcd.serializers." .. serializer_name)
12-
if ok then
13-
return module
14-
end
15-
end
16-
17-
return require("resty.etcd.serializers.json")
18-
end
19-
207
function _M.new(opts)
218
opts = opts or {}
229
if not typeof.table(opts) then
@@ -33,7 +20,7 @@ function _M.new(opts)
3320
opts.ttl = opts.ttl or -1
3421

3522
local serializer_name = typeof.string(opts.serializer) and opts.serializer
36-
opts.serializer = require_serializer(serializer_name)
23+
opts.serializer = serializer_name
3724
opts.api_prefix = "/v3"
3825

3926
return etcdv3.new(opts)

lib/resty/etcd/v3.lua

+14-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ local decode_base64 = ngx.decode_base64
2727
local semaphore = require("ngx.semaphore")
2828
local health_check = require("resty.etcd.health_check")
2929
local pl_path = require("pl.path")
30+
local require = require
31+
local pcall = pcall
3032

3133
math.randomseed(now() * 1000 + ngx.worker.pid())
3234

@@ -265,6 +267,17 @@ local function serialize_and_encode_base64(serialize_fn, data)
265267
return encode_base64(data)
266268
end
267269

270+
local function require_serializer(serializer_name)
271+
if serializer_name then
272+
local ok, module = pcall(require, "resty.etcd.serializers." .. serializer_name)
273+
if ok then
274+
return module
275+
end
276+
end
277+
278+
return require("resty.etcd.serializers.json")
279+
end
280+
268281

269282
function _M.new(opts)
270283
local timeout = opts.timeout
@@ -278,7 +291,7 @@ function _M.new(opts)
278291
if ssl_verify == nil then
279292
ssl_verify = true
280293
end
281-
local serializer = opts.serializer
294+
local serializer = require_serializer(opts.serializer)
282295
local extra_headers = opts.extra_headers
283296
local sni = opts.sni
284297
local unix_socket_proxy = opts.unix_socket_proxy

0 commit comments

Comments
 (0)