Skip to content

Commit

Permalink
Move require_serializer into v3
Browse files Browse the repository at this point in the history
* Fix #126
* cause to conform to v3.md

Signed-off-by: u5surf <[email protected]>
  • Loading branch information
u5surf committed Oct 11, 2022
1 parent 29de8bd commit 8ae9737
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
15 changes: 1 addition & 14 deletions lib/resty/etcd.lua
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
local etcdv3 = require("resty.etcd.v3")
local typeof = require("typeof")
local require = require
local pcall = pcall

local _M = {version = 0.9}


local function require_serializer(serializer_name)
if serializer_name then
local ok, module = pcall(require, "resty.etcd.serializers." .. serializer_name)
if ok then
return module
end
end

return require("resty.etcd.serializers.json")
end

function _M.new(opts)
opts = opts or {}
if not typeof.table(opts) then
Expand All @@ -33,7 +20,7 @@ function _M.new(opts)
opts.ttl = opts.ttl or -1

local serializer_name = typeof.string(opts.serializer) and opts.serializer
opts.serializer = require_serializer(serializer_name)
opts.serializer = serializer_name
opts.api_prefix = "/v3"

return etcdv3.new(opts)
Expand Down
15 changes: 14 additions & 1 deletion lib/resty/etcd/v3.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ local decode_base64 = ngx.decode_base64
local semaphore = require("ngx.semaphore")
local health_check = require("resty.etcd.health_check")
local pl_path = require("pl.path")
local require = require
local pcall = pcall

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

Expand Down Expand Up @@ -265,6 +267,17 @@ local function serialize_and_encode_base64(serialize_fn, data)
return encode_base64(data)
end

local function require_serializer(serializer_name)
if serializer_name then
local ok, module = pcall(require, "resty.etcd.serializers." .. serializer_name)
if ok then
return module
end
end

return require("resty.etcd.serializers.json")
end


function _M.new(opts)
local timeout = opts.timeout
Expand All @@ -278,7 +291,7 @@ function _M.new(opts)
if ssl_verify == nil then
ssl_verify = true
end
local serializer = opts.serializer
local serializer = require_serializer(opts.serializer)
local extra_headers = opts.extra_headers
local sni = opts.sni
local unix_socket_proxy = opts.unix_socket_proxy
Expand Down

0 comments on commit 8ae9737

Please sign in to comment.