mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 17:47:03 +00:00
lua: Use table syntax for user object bindings (#325)
* lua: update bind_message to use table syntax * lua: update bind_address to use table syntax
This commit is contained in:
@@ -16,12 +16,8 @@ func registerMailAddressType(ls *lua.LState) {
|
|||||||
ls.SetField(mt, "new", ls.NewFunction(newMailAddress))
|
ls.SetField(mt, "new", ls.NewFunction(newMailAddress))
|
||||||
|
|
||||||
// Methods.
|
// Methods.
|
||||||
ls.SetField(mt, "__index", ls.SetFuncs(ls.NewTable(), mailAddressMethods))
|
ls.SetField(mt, "__index", ls.NewFunction(mailAddressIndex))
|
||||||
}
|
ls.SetField(mt, "__newindex", ls.NewFunction(mailAddressNewIndex))
|
||||||
|
|
||||||
var mailAddressMethods = map[string]lua.LGFunction{
|
|
||||||
"address": mailAddressGetSetAddress,
|
|
||||||
"name": mailAddressGetSetName,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMailAddress(ls *lua.LState) int {
|
func newMailAddress(ls *lua.LState) int {
|
||||||
@@ -48,8 +44,8 @@ func unwrapMailAddress(ud *lua.LUserData) (*mail.Address, bool) {
|
|||||||
return val, ok
|
return val, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkMailAddress(ls *lua.LState) *mail.Address {
|
func checkMailAddress(ls *lua.LState, pos int) *mail.Address {
|
||||||
ud := ls.CheckUserData(1)
|
ud := ls.CheckUserData(pos)
|
||||||
if val, ok := ud.Value.(*mail.Address); ok {
|
if val, ok := ud.Value.(*mail.Address); ok {
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
@@ -57,28 +53,40 @@ func checkMailAddress(ls *lua.LState) *mail.Address {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func mailAddressGetSetAddress(ls *lua.LState) int {
|
// Gets a field value from MailAddress user object. This emulates a Lua table,
|
||||||
val := checkMailAddress(ls)
|
// allowing `msg.subject` instead of a Lua object syntax of `msg:subject()`.
|
||||||
if ls.GetTop() == 2 {
|
func mailAddressIndex(ls *lua.LState) int {
|
||||||
// Setter.
|
a := checkMailAddress(ls, 1)
|
||||||
val.Address = ls.CheckString(2)
|
field := ls.CheckString(2)
|
||||||
return 0
|
|
||||||
|
// Push the requested field's value onto the stack.
|
||||||
|
switch field {
|
||||||
|
case "name":
|
||||||
|
ls.Push(lua.LString(a.Name))
|
||||||
|
case "address":
|
||||||
|
ls.Push(lua.LString(a.Address))
|
||||||
|
default:
|
||||||
|
// Unknown field.
|
||||||
|
ls.Push(lua.LNil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getter.
|
|
||||||
ls.Push(lua.LString(val.Address))
|
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func mailAddressGetSetName(ls *lua.LState) int {
|
// Sets a field value on MailAddress user object. This emulates a Lua table,
|
||||||
val := checkMailAddress(ls)
|
// allowing `msg.subject = x` instead of a Lua object syntax of `msg:subject(x)`.
|
||||||
if ls.GetTop() == 2 {
|
func mailAddressNewIndex(ls *lua.LState) int {
|
||||||
// Setter.
|
a := checkMailAddress(ls, 1)
|
||||||
val.Name = ls.CheckString(2)
|
index := ls.CheckString(2)
|
||||||
return 0
|
|
||||||
|
switch index {
|
||||||
|
case "name":
|
||||||
|
a.Name = ls.CheckString(3)
|
||||||
|
case "address":
|
||||||
|
a.Address = ls.CheckString(3)
|
||||||
|
default:
|
||||||
|
ls.RaiseError("invalid index %q", index)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getter.
|
return 0
|
||||||
ls.Push(lua.LString(val.Name))
|
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,17 +18,8 @@ func registerMessageMetadataType(ls *lua.LState) {
|
|||||||
ls.SetField(mt, "new", ls.NewFunction(newMessageMetadata))
|
ls.SetField(mt, "new", ls.NewFunction(newMessageMetadata))
|
||||||
|
|
||||||
// Methods.
|
// Methods.
|
||||||
ls.SetField(mt, "__index", ls.SetFuncs(ls.NewTable(), messageMetadataMethods))
|
ls.SetField(mt, "__index", ls.NewFunction(messageMetadataIndex))
|
||||||
}
|
ls.SetField(mt, "__newindex", ls.NewFunction(messageMetadataNewIndex))
|
||||||
|
|
||||||
var messageMetadataMethods = map[string]lua.LGFunction{
|
|
||||||
"mailbox": messageMetadataGetSetMailbox,
|
|
||||||
"id": messageMetadataGetSetID,
|
|
||||||
"from": messageMetadataGetSetFrom,
|
|
||||||
"to": messageMetadataGetSetTo,
|
|
||||||
"subject": messageMetadataGetSetSubject,
|
|
||||||
"date": messageMetadataGetSetDate,
|
|
||||||
"size": messageMetadataGetSetSize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMessageMetadata(ls *lua.LState) int {
|
func newMessageMetadata(ls *lua.LState) int {
|
||||||
@@ -47,8 +38,8 @@ func wrapMessageMetadata(ls *lua.LState, val *event.MessageMetadata) *lua.LUserD
|
|||||||
return ud
|
return ud
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkMessageMetadata(ls *lua.LState) *event.MessageMetadata {
|
func checkMessageMetadata(ls *lua.LState, pos int) *event.MessageMetadata {
|
||||||
ud := ls.CheckUserData(1)
|
ud := ls.CheckUserData(pos)
|
||||||
if v, ok := ud.Value.(*event.MessageMetadata); ok {
|
if v, ok := ud.Value.(*event.MessageMetadata); ok {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
@@ -56,51 +47,56 @@ func checkMessageMetadata(ls *lua.LState) *event.MessageMetadata {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func messageMetadataGetSetMailbox(ls *lua.LState) int {
|
// Gets a field value from MessageMetadata user object. This emulates a Lua table,
|
||||||
val := checkMessageMetadata(ls)
|
// allowing `msg.subject` instead of a Lua object syntax of `msg:subject()`.
|
||||||
if ls.GetTop() == 2 {
|
func messageMetadataIndex(ls *lua.LState) int {
|
||||||
// Setter.
|
m := checkMessageMetadata(ls, 1)
|
||||||
val.Mailbox = ls.CheckString(2)
|
field := ls.CheckString(2)
|
||||||
return 0
|
|
||||||
|
// Push the requested field's value onto the stack.
|
||||||
|
switch field {
|
||||||
|
case "mailbox":
|
||||||
|
ls.Push(lua.LString(m.Mailbox))
|
||||||
|
case "id":
|
||||||
|
ls.Push(lua.LString(m.ID))
|
||||||
|
case "from":
|
||||||
|
ls.Push(wrapMailAddress(ls, m.From))
|
||||||
|
case "to":
|
||||||
|
lt := &lua.LTable{}
|
||||||
|
for _, v := range m.To {
|
||||||
|
lt.Append(wrapMailAddress(ls, v))
|
||||||
|
}
|
||||||
|
ls.Push(lt)
|
||||||
|
case "date":
|
||||||
|
ls.Push(lua.LNumber(m.Date.Unix()))
|
||||||
|
case "subject":
|
||||||
|
ls.Push(lua.LString(m.Subject))
|
||||||
|
case "size":
|
||||||
|
ls.Push(lua.LNumber(m.Size))
|
||||||
|
default:
|
||||||
|
// Unknown field.
|
||||||
|
ls.Push(lua.LNil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getter.
|
|
||||||
ls.Push(lua.LString(val.Mailbox))
|
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func messageMetadataGetSetID(ls *lua.LState) int {
|
// Sets a field value on MessageMetadata user object. This emulates a Lua table,
|
||||||
val := checkMessageMetadata(ls)
|
// allowing `msg.subject = x` instead of a Lua object syntax of `msg:subject(x)`.
|
||||||
if ls.GetTop() == 2 {
|
func messageMetadataNewIndex(ls *lua.LState) int {
|
||||||
// Setter.
|
m := checkMessageMetadata(ls, 1)
|
||||||
val.ID = ls.CheckString(2)
|
index := ls.CheckString(2)
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// Getter.
|
switch index {
|
||||||
ls.Push(lua.LString(val.ID))
|
case "mailbox":
|
||||||
return 1
|
m.Mailbox = ls.CheckString(3)
|
||||||
}
|
case "id":
|
||||||
|
m.ID = ls.CheckString(3)
|
||||||
func messageMetadataGetSetFrom(ls *lua.LState) int {
|
case "from":
|
||||||
val := checkMessageMetadata(ls)
|
m.From = checkMailAddress(ls, 3)
|
||||||
if ls.GetTop() == 2 {
|
case "to":
|
||||||
// Setter.
|
lt := ls.CheckTable(3)
|
||||||
val.From = checkMailAddress(ls)
|
to := make([]*mail.Address, 0, 16)
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// Getter.
|
|
||||||
ls.Push(wrapMailAddress(ls, val.From))
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func messageMetadataGetSetTo(ls *lua.LState) int {
|
|
||||||
val := checkMessageMetadata(ls)
|
|
||||||
if ls.GetTop() == 2 {
|
|
||||||
// Setter.
|
|
||||||
lt := ls.CheckTable(2)
|
|
||||||
to := make([]*mail.Address, lt.Len())
|
|
||||||
lt.ForEach(func(k, lv lua.LValue) {
|
lt.ForEach(func(k, lv lua.LValue) {
|
||||||
if ud, ok := lv.(*lua.LUserData); ok {
|
if ud, ok := lv.(*lua.LUserData); ok {
|
||||||
if entry, ok := unwrapMailAddress(ud); ok {
|
if entry, ok := unwrapMailAddress(ud); ok {
|
||||||
@@ -108,54 +104,16 @@ func messageMetadataGetSetTo(ls *lua.LState) int {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
val.To = to
|
m.To = to
|
||||||
|
case "date":
|
||||||
|
m.Date = time.Unix(ls.CheckInt64(3), 0)
|
||||||
|
case "subject":
|
||||||
|
m.Subject = ls.CheckString(3)
|
||||||
|
case "size":
|
||||||
|
m.Size = ls.CheckInt64(3)
|
||||||
|
default:
|
||||||
|
ls.RaiseError("invalid index %q", index)
|
||||||
|
}
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getter.
|
|
||||||
lt := &lua.LTable{}
|
|
||||||
for _, v := range val.To {
|
|
||||||
lt.Append(wrapMailAddress(ls, v))
|
|
||||||
}
|
|
||||||
ls.Push(lt)
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func messageMetadataGetSetSubject(ls *lua.LState) int {
|
|
||||||
val := checkMessageMetadata(ls)
|
|
||||||
if ls.GetTop() == 2 {
|
|
||||||
// Setter.
|
|
||||||
val.Subject = ls.CheckString(2)
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// Getter.
|
|
||||||
ls.Push(lua.LString(val.Subject))
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func messageMetadataGetSetDate(ls *lua.LState) int {
|
|
||||||
val := checkMessageMetadata(ls)
|
|
||||||
if ls.GetTop() == 2 {
|
|
||||||
// Setter.
|
|
||||||
val.Date = time.Unix(ls.CheckInt64(2), 0)
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// Getter.
|
|
||||||
ls.Push(lua.LNumber(val.Date.Unix()))
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func messageMetadataGetSetSize(ls *lua.LState) int {
|
|
||||||
val := checkMessageMetadata(ls)
|
|
||||||
if ls.GetTop() == 2 {
|
|
||||||
// Setter.
|
|
||||||
val.Size = ls.CheckInt64(2)
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// Getter.
|
|
||||||
ls.Push(lua.LNumber(val.Size))
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -35,19 +35,19 @@ func TestAfterMessageStored(t *testing.T) {
|
|||||||
end
|
end
|
||||||
|
|
||||||
function after_message_stored(msg)
|
function after_message_stored(msg)
|
||||||
assert_eq(msg:mailbox(), "mb1")
|
assert_eq(msg.mailbox, "mb1")
|
||||||
assert_eq(msg:id(), "id1")
|
assert_eq(msg.id, "id1")
|
||||||
assert_eq(msg:subject(), "subj1")
|
assert_eq(msg.subject, "subj1")
|
||||||
assert_eq(msg:size(), 42)
|
assert_eq(msg.size, 42)
|
||||||
|
|
||||||
assert_eq(msg:from():name(), "name1")
|
assert_eq(msg.from.name, "name1")
|
||||||
assert_eq(msg:from():address(), "addr1")
|
assert_eq(msg.from.address, "addr1")
|
||||||
|
|
||||||
assert_eq(table.getn(msg:to()), 1)
|
assert_eq(table.getn(msg.to), 1)
|
||||||
assert_eq(msg:to()[1]:name(), "name2")
|
assert_eq(msg.to[1].name, "name2")
|
||||||
assert_eq(msg:to()[1]:address(), "addr2")
|
assert_eq(msg.to[1].address, "addr2")
|
||||||
|
|
||||||
assert_eq(msg:date(), 981173106)
|
assert_eq(msg.date, 981173106)
|
||||||
|
|
||||||
notify:send(test_ok)
|
notify:send(test_ok)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user