Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move require_serializer into v3 #176

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -1,4 +1,5 @@
-- https://github.com/ledgetech/lua-resty-http
local require = require
local split = require("ngx.re").split
local typeof = require("typeof")
local cjson = require("cjson.safe")
Expand Down Expand Up @@ -27,6 +28,7 @@ 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 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
56 changes: 56 additions & 0 deletions t/v3/serializer.t
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,59 @@ checked val as expect: 111
checked val as expect: "foo"
checked val as expect: {"a":1}
checked val as expect:

=== TEST 5: resty.etcd.v3 serializer
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local cjson = require("cjson.safe")

local etcd, err = require("resty.etcd.v3").new({
timeout = 5,
http_host = "http://127.0.0.1:2379",
ttl = -1,
api_prefix = "/v3",
serializer = "raw"
})
check_res(etcd, err)

local res
res, err = etcd:rmdir("/dir")
check_res(res, err, nil, 200)

res, err = etcd:set("/dir/v3/a", '"foo"')
check_res(res, err)

res, err = etcd:get("/dir/v3/a")
check_res(res, err, '"foo"')

local s = cjson.encode({a = 1})
res, err = etcd:setx("/dir/v3/a", s)
check_res(res, err, nil, 200)

res, err = etcd:get("/dir/v3/a")
check_res(res, err, s, 200)

res, err = etcd:setnx("/dir/v3/not_exist", "")
check_res(res, err, nil, 200)

res, err = etcd:get("/dir/v3/not_exist")
check_res(res, err, "", 200)

res, err = etcd:rmdir("/dir")
check_res(res, err, nil, 200)

res, err = etcd:set("/dir/v3/b", 111)
check_res(res, err)
}
}
--- request
GET /t
--- no_error_log
[error]
--- response_body
checked val as expect: "foo"
checked val as expect: {"a":1}
checked val as expect:
err: unsupported type for number