-
Notifications
You must be signed in to change notification settings - Fork 220
/
lua.go
175 lines (163 loc) · 3.71 KB
/
lua.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package miniredis
import (
redigo "github.com/garyburd/redigo/redis"
lua "github.com/yuin/gopher-lua"
"github.com/alicebob/miniredis/server"
)
func mkLuaFuncs(conn redigo.Conn) map[string]lua.LGFunction {
mkCall := func(failFast bool) func(l *lua.LState) int {
return func(l *lua.LState) int {
top := l.GetTop()
if top == 0 {
l.Error(lua.LString("Please specify at least one argument for redis.call()"), 1)
return 0
}
var args []interface{}
for i := 1; i <= top; i++ {
switch a := l.Get(i).(type) {
// case lua.LBool:
// args[i-2] = a
case lua.LNumber:
// value, _ := strconv.ParseFloat(lua.LVAsString(arg), 64)
args = append(args, float64(a))
case lua.LString:
args = append(args, string(a))
default:
l.Error(lua.LString("Lua redis() command arguments must be strings or integers"), 1)
return 0
}
}
cmd, ok := args[0].(string)
if !ok {
l.Error(lua.LString("Unknown Redis command called from Lua script"), 1)
return 0
}
res, err := conn.Do(cmd, args[1:]...)
if err != nil {
if failFast {
// call() mode
l.Error(lua.LString(err.Error()), 1)
return 0
}
// pcall() mode
l.Push(lua.LNil)
return 1
}
if res == nil {
l.Push(lua.LNil)
} else {
switch r := res.(type) {
case int64:
l.Push(lua.LNumber(r))
case []uint8:
l.Push(lua.LString(string(r)))
case []interface{}:
l.Push(redisToLua(l, r))
case string:
l.Push(lua.LString(r))
default:
panic("type not handled")
}
}
return 1
}
}
return map[string]lua.LGFunction{
"call": mkCall(true),
"pcall": mkCall(false),
"error_reply": func(l *lua.LState) int {
msg := l.CheckString(1)
res := &lua.LTable{}
res.RawSetString("err", lua.LString(msg))
l.Push(res)
return 1
},
"status_reply": func(l *lua.LState) int {
msg := l.CheckString(1)
res := &lua.LTable{}
res.RawSetString("ok", lua.LString(msg))
l.Push(res)
return 1
},
"sha1hex": func(l *lua.LState) int {
msg := l.CheckString(1)
l.Push(lua.LString(sha1Hex(msg)))
return 1
},
}
}
func luaToRedis(l *lua.LState, c *server.Peer, value lua.LValue) {
if value == nil {
c.WriteNull()
return
}
switch t := value.(type) {
case *lua.LNilType:
c.WriteNull()
case lua.LBool:
if lua.LVAsBool(value) {
c.WriteInt(1)
} else {
c.WriteInt(0)
}
case lua.LNumber:
c.WriteInt(int(lua.LVAsNumber(value)))
case lua.LString:
c.WriteBulk(lua.LVAsString(value))
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
// the only field.
if s := t.RawGetString("err"); s.Type() != lua.LTNil {
c.WriteError(s.String())
return
}
if s := t.RawGetString("ok"); s.Type() != lua.LTNil {
c.WriteInline(s.String())
return
}
result := []lua.LValue{}
for j := 1; true; j++ {
val := l.GetTable(value, lua.LNumber(j))
if val == nil {
result = append(result, val)
continue
}
if val.Type() == lua.LTNil {
break
}
result = append(result, val)
}
c.WriteLen(len(result))
for _, r := range result {
luaToRedis(l, c, r)
}
default:
panic("....")
}
}
func redisToLua(l *lua.LState, res []interface{}) *lua.LTable {
rettb := l.NewTable()
for _, e := range res {
var v lua.LValue
if e == nil {
v = lua.LValue(nil)
} else {
switch et := e.(type) {
case int64:
v = lua.LNumber(et)
case []uint8:
v = lua.LString(string(et))
case []interface{}:
v = redisToLua(l, et)
case string:
v = lua.LString(et)
default:
// TODO: oops?
v = lua.LString(e.(string))
}
}
l.RawSet(rettb, lua.LNumber(rettb.Len()+1), v)
}
return rettb
}