Skip to content

Commit

Permalink
change error_reply() to work as redis does, not as the docs say
Browse files Browse the repository at this point in the history
Also check function arguments.
Same for status_reply()
  • Loading branch information
alicebob committed Jan 15, 2018
1 parent 3dc460a commit 4ce3763
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
12 changes: 12 additions & 0 deletions cmd_scripting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,18 @@ func TestCmdEvalReply(t *testing.T) {
ok(t, err)
equals(t, "good", v)
}

_, err = c.Do("EVAL", `return redis.error_reply()`, 0)
assert(t, err != nil, "no EVAL error")

_, err = c.Do("EVAL", `return redis.error_reply(1)`, 0)
assert(t, err != nil, "no EVAL error")

_, err = c.Do("EVAL", `return redis.status_reply()`, 0)
assert(t, err != nil, "no EVAL error")

_, err = c.Do("EVAL", `return redis.status_reply(1)`, 0)
assert(t, err != nil, "no EVAL error")
}

func TestCmdEvalResponse(t *testing.T) {
Expand Down
24 changes: 11 additions & 13 deletions lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,16 @@ func mkLuaFuncs(conn redigo.Conn) map[string]lua.LGFunction {
return 1 // Notify that we pushed one value to the stack
},
"error_reply": func(l *lua.LState) int {
msg := l.CheckAny(1)
msg := l.CheckString(1)
res := &lua.LTable{}
res.RawSetString("err", msg)
res.RawSetString("err", lua.LString(msg))
l.Push(res)
return 1
},
"status_reply": func(l *lua.LState) int {
msg := l.CheckAny(1)
msg := l.CheckString(1)
res := &lua.LTable{}
res.RawSetString("ok", msg)
res.RawSetString("ok", lua.LString(msg))
l.Push(res)
return 1
},
Expand Down Expand Up @@ -104,17 +104,15 @@ func luaToRedis(l *lua.LState, c *server.Peer, value lua.LValue) {
c.WriteBulk(lua.LVAsString(value))
case lua.LTTable:
t := value.(*lua.LTable)
// special case for table with only an 'err' field
var keys []string
t.ForEach(func(k, _ lua.LValue) {
keys = append(keys, k.String())
})
if len(keys) == 1 && keys[0] == "err" {
c.WriteError(t.RawGetString("err").String())
// special case for tables with an 'err' or 'ok' field
// note: according to the docs this only counts when 'err' or 'ok' is
// the only field.
if s := t.RawGetString("err"); s.Type() != lua.LTNil {
c.WriteError(s.String())
return
}
if len(keys) == 1 && keys[0] == "ok" {
c.WriteInline(t.RawGetString("ok").String())
if s := t.RawGetString("ok"); s.Type() != lua.LTNil {
c.WriteInline(s.String())
return
}

Expand Down

0 comments on commit 4ce3763

Please sign in to comment.