@@ -5,21 +5,18 @@ local cjson = require("cjson.safe")
55local encode_args = ngx .encode_args
66local setmetatable = setmetatable
77local clear_tab = require (" table.clear" )
8- local tostring = tostring
98local ipairs = ipairs
109local type = type
1110local utils = require (" resty.etcd.utils" )
1211local encode_base64 = ngx .encode_base64
1312local require = require
1413local next = next
1514local table = table
15+ local decode_json = cjson .decode
1616local 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
2522local 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
199198end
200199
201200
202201local 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
340342local 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
0 commit comments