Skip to content

Commit

Permalink
Merge pull request #256 from alicebob/luastatus
Browse files Browse the repository at this point in the history
redis.call() should return a "status_reply" for simple strings
  • Loading branch information
alicebob authored Feb 23, 2022
2 parents 7614a88 + f4296b2 commit b85fb0f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
9 changes: 9 additions & 0 deletions integration/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,15 @@ func TestLuaCall(t *testing.T) {
c.Do("EVAL", `return redis.call("GET", "foo")`, "0")
c.Do("EVAL", `return redis.call("SET", "foo", 42)`, "0")
c.Do("EVAL", `redis.log(redis.LOG_NOTICE, "hello")`, "0")
c.Do("EVAL", `local res = redis.call("GET", "foo"); return res['ok']`, "0")
})

testRaw(t, func(c *client) {
script := `
local result = redis.call('SET', 'mykey', 'myvalue', 'NX');
return result['ok'];
`
c.Do("EVAL", script, "0")
})

// datatype errors
Expand Down
17 changes: 10 additions & 7 deletions lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ func mkLua(srv *server.Server, c *server.Peer) (map[string]lua.LGFunction, map[s
l.Push(lua.LString(string(r)))
case []interface{}:
l.Push(redisToLua(l, r))
case server.Simple:
l.Push(luaStatusReply(string(r)))
case string:
l.Push(lua.LString(r))
case error:
Expand Down Expand Up @@ -129,8 +131,7 @@ func mkLua(srv *server.Server, c *server.Peer) (map[string]lua.LGFunction, map[s
l.Error(lua.LString("wrong number or type of arguments"), 1)
return 0
}
res := &lua.LTable{}
res.RawSetString("ok", lua.LString(msg))
res := luaStatusReply(string(msg))
l.Push(res)
return 1
},
Expand Down Expand Up @@ -170,11 +171,7 @@ func luaToRedis(l *lua.LState, c *server.Peer, value lua.LValue) {
c.WriteInt(int(lua.LVAsNumber(value)))
case lua.LString:
s := lua.LVAsString(value)
if s == "OK" {
c.WriteInline(s)
} else {
c.WriteBulk(s)
}
c.WriteBulk(s)
case *lua.LTable:
// special case for tables with an 'err' or 'ok' field
// note: according to the docs this only counts when 'err' or 'ok' is
Expand Down Expand Up @@ -237,3 +234,9 @@ func redisToLua(l *lua.LState, res []interface{}) *lua.LTable {
}
return rettb
}

func luaStatusReply(msg string) *lua.LTable {
tab := &lua.LTable{}
tab.RawSetString("ok", lua.LString(msg))
return tab
}
4 changes: 3 additions & 1 deletion server/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strconv"
)

type Simple string

// ErrProtocol is the general error for unexpected input
var ErrProtocol = errors.New("invalid request")

Expand Down Expand Up @@ -98,7 +100,7 @@ func ParseReply(rd *bufio.Reader) (interface{}, error) {
return nil, ErrProtocol
case '+':
// +: simple string
return string(line[1 : len(line)-2]), nil
return Simple(line[1 : len(line)-2]), nil
case '-':
// -: errors
return nil, errors.New(string(line[1 : len(line)-2]))
Expand Down
2 changes: 1 addition & 1 deletion server/proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestParseReply(t *testing.T) {
for i, c := range []cas{
{
payload: "+hello world\r\n",
res: "hello world",
res: Simple("hello world"),
},
{
payload: "-some error\r\n",
Expand Down

0 comments on commit b85fb0f

Please sign in to comment.