Yiyiyimu commented on a change in pull request #2036:
URL: https://github.com/apache/apisix/pull/2036#discussion_r488751665



##########
File path: apisix/core/etcd.lua
##########
@@ -44,24 +49,144 @@ end
 _M.new = new
 
 
+local function kvs_to_node(kvs)
+    local node = {}
+    node.key = kvs.key
+    node.value = kvs.value
+    node.createdIndex = tonumber(kvs.create_revision)
+    node.modifiedIndex = tonumber(kvs.mod_revision)
+    return node
+end
+
+local function kvs_to_nodes(res, start_index)
+    res.body.node.dir = true
+    res.body.node.nodes = {}
+    for i=start_index, #res.body.kvs do
+        if start_index == 1 then
+            res.body.node.nodes[i] = kvs_to_node(res.body.kvs[i])
+        else
+            res.body.node.nodes[i-1] = kvs_to_node(res.body.kvs[i])
+        end
+    end
+    return res
+end
+
+
+local function not_found(res)
+    res.body.message = "Key not found"
+    res.reason = "Not found"
+    res.status = 404
+    return res
+end
+
+
+function _M.get_format(res, realkey)
+    if res.body.error == "etcdserver: user name is empty" then
+        return nil, "insufficient credentials code: 401"
+    end
+
+    res.headers["X-Etcd-Index"] = res.body.header.revision
+
+    if not res.body.kvs then
+        return not_found(res)
+    end
+    res.body.action = "get"
+
+    res.body.node = kvs_to_node(res.body.kvs[1])
+    -- kvs.value = nil, so key is root
+    if type(res.body.kvs[1].value) == "userdata" or not res.body.kvs[1].value 
then
+        -- remove last "/" when necesary
+        if string.sub(res.body.node.key, -1, -1) == "/" then
+            res.body.node.key = string.sub(res.body.node.key, 1, 
#res.body.node.key-1)
+        end
+        res = kvs_to_nodes(res, 2)
+    -- key not match, so realkey is root
+    elseif res.body.kvs[1].key ~= realkey then
+        res.body.node.key = realkey
+        res = kvs_to_nodes(res, 1)
+    -- first is root (in v2, root not contains value), others are nodes
+    elseif #res.body.kvs > 1 then
+        res = kvs_to_nodes(res, 2)
+    end
+
+    res.body.kvs = nil
+    return res, nil
+end
+
+
+function _M.watch_format(v3res)

Review comment:
       The change [I made last 
time](https://github.com/apache/apisix/pull/2036#discussion_r477341702)
   
   Maybe I need some suggestions on how to name these two functions 🤣 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to