pkulchenko / ZeroBranePackage

Packages for ZeroBrane Studio (https://studio.zerobrane.com)
MIT License
246 stars 148 forks source link

Redis: Minor issues Remote Console #63

Closed tw-bert closed 6 years ago

tw-bert commented 6 years ago

redis.lua: version 0.33

Gist that replicates both small issues:

INFO
"ldb_eval:1: Script attempted to access nonexistent global variable 'INFO'"

@hset hello world 1
1
@hset hello universe 2
1

@hgetall hello
Error in processing results: [string "return {["world","1","universe","2"]}"]:1: ']' expected near ','

redis.call('hgetall','hello')
{"world"; "1"; "universe"; "2"}
pkulchenko commented 6 years ago

@tw-bert, the first result (INFO) is by design; the second is a bug and should be fixed with the following patch:

diff --git a/redis.lua b/redis.lua
index 752f594..d459691 100644
--- a/redis.lua
+++ b/redis.lua
@@ -1573,7 +1573,7 @@ local package = {
   name = "Redis",
   description = "Integrates with Redis.",
   author = "Paul Kulchenko",
-  version = 0.33,
+  version = 0.34,
   dependencies = "1.30",

   onRegister = function(self)
@@ -1650,7 +1650,7 @@ local function getreply(response)
   local msg = table.concat(getval(response, "<reply>", "%s") or {}, ",")
   -- add proper quoting to those messages that may be truncated because of `maxlen` limit
   if msg:find('^"') and not msg:find('"$') then msg = msg..'"' end
-  return "return {"..(msg == "NULL" and "'nil'" or msg).."}"
+  return ("return {%s}"):format(msg == "NULL" and "'nil'" or ("%q"):format(msg))
 end
 local function geterror(response)
   local err = getval(type(response) == 'table' and response or {response}, "<error>")

Let me know if this worked for you.

tw-bert commented 6 years ago

@pkulchenko , statements like INFO MEMORY are nicely formatted before this patch, is it possible to retain that? After the patch, it's just a long string with escaped chars.

pkulchenko commented 6 years ago

@tw-bert, agree, this should be a better patch:

diff --git a/redis.lua b/redis.lua
index d459691..f2e701e 100644
--- a/redis.lua
+++ b/redis.lua
@@ -1650,7 +1650,13 @@ local function getreply(response)
   local msg = table.concat(getval(response, "<reply>", "%s") or {}, ",")
   -- add proper quoting to those messages that may be truncated because of `maxlen` limit
   if msg:find('^"') and not msg:find('"$') then msg = msg..'"' end
-  return "return {"..(msg == "NULL" and "'nil'" or msg).."}"
+  return ("return {%s}"):format(
+    -- show returned NULL as `nil`
+    msg == "NULL" and "'nil'" or
+    -- show array (`[...]`) results from commands like `@hgetall something` as is
+    msg:find("^%[.+%]$") and ("%q"):format(msg) or
+    msg
+  )
 end
 local function geterror(response)
   local err = getval(type(response) == 'table' and response or {response}, "<error>")

I didn't want to have array-specific logic in it, but it seems like it may be necessary in this case.

tw-bert commented 6 years ago

@pkulchenko This patch works fine