commit e0a162299ee6c690553aaefbf0f372b15d43cf04
parent 1f94ec5c6c74be77853ee77027367b80008368ce
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date: Sun, 31 Aug 2025 19:17:41 +0000
Message headers are available to subscription callbacks
Diffstat:
1 file changed, 31 insertions(+), 1 deletion(-)
diff --git a/natsbot.go b/natsbot.go
@@ -87,7 +87,8 @@ func processMsg(L *lua.LState, msg *nats.Msg) {
err := L.CallByParam(lua.P{Fn: fn, NRet: 0, Protect: true},
subs,
lua.LString(msg.Subject),
- lua.LString(string(msg.Data)))
+ lua.LString(string(msg.Data)),
+ luaHeaders(L, msg.Reply, msg.Header))
if err != nil {
panic(err)
}
@@ -565,6 +566,35 @@ func runTimers(L *lua.LState, parentTimer *time.Timer) {
/********** Tools **********/
+func luaHeader(L *lua.LState, header []string) lua.LValue {
+ switch len(header) {
+ case 0:
+ return lua.LNil
+ case 1:
+ return lua.LString(header[0])
+ default:
+ result := L.CreateTable(len(header), 0)
+ for i, v := range header {
+ L.RawSetInt(result, i+1, lua.LString(v))
+ }
+ return result
+ }
+}
+
+func luaHeaders(L *lua.LState, reply string, headers map[string][]string) lua.LValue {
+ result := L.NewTable()
+
+ if reply != "" {
+ L.RawSetInt(result, 1, lua.LString(reply))
+ }
+
+ for key, values := range headers {
+ L.SetField(result, key, luaHeader(L, values))
+ }
+
+ return result
+}
+
func newUserData(L *lua.LState, v interface{}) *lua.LUserData {
res := L.NewUserData()
res.Value = v