From 9a609ce88092606f131bfef533caec863cd912ed Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Mon, 24 Jul 2023 10:32:25 +0200 Subject: [PATCH] work in progress: matrix; message: sender as Addressable type --- core/message/message.go | 6 +++--- matrix/matrix.go | 16 ++++++++++++++++ scopes.go | 9 +++++++-- tests/etc/etc_mx.go | 5 ++++- tests/matrix_test.go | 21 +++++++++++++++++++-- 5 files changed, 49 insertions(+), 8 deletions(-) diff --git a/core/message/message.go b/core/message/message.go index 1a50e5b..eb33d88 100644 --- a/core/message/message.go +++ b/core/message/message.go @@ -42,7 +42,7 @@ func (h *head) Item() string { type message struct { *head - sender lib.Address + sender lib.Addressable payload lib.Payload } @@ -50,7 +50,7 @@ func (msg *message) Head() lib.MsgHead { return msg } -func (msg *message) Sender() lib.Address { +func (msg *message) Sender() lib.Addressable { return msg.sender } @@ -58,7 +58,7 @@ func (msg *message) Payload() lib.Payload { return msg.payload } -func (msg *message) WithSender(s lib.Address) lib.Message { +func (msg *message) WithSender(s lib.Addressable) lib.Message { msg.sender = s return msg } diff --git a/matrix/matrix.go b/matrix/matrix.go index 2903ff7..7ce41de 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -1 +1,17 @@ package matrix + +import ( + lib "git.sr.ht/~cco/go-scopes" + "git.sr.ht/~cco/go-scopes/core" + "git.sr.ht/~cco/go-scopes/core/action" +) + +func Start(ctx lib.Context) { + cfg := ctx.Config() + cfg.AddAction("create", action.Base(create)) + core.Start(ctx) +} + +func create(act lib.Action) bool { + return true +} diff --git a/scopes.go b/scopes.go index 8147602..2fefaeb 100644 --- a/scopes.go +++ b/scopes.go @@ -41,12 +41,17 @@ type MsgHead interface { type Message interface { MsgHead Head() MsgHead - Sender() Address + Sender() Addressable Payload() Payload - WithSender(Address) Message + WithSender(Addressable) Message WithPayload(Payload) Message } +type Addressable interface { + Address() Address + Cell() Context +} + type Address interface { fmt.Stringer Url() string diff --git a/tests/etc/etc_mx.go b/tests/etc/etc_mx.go index 98dd005..07efd7b 100644 --- a/tests/etc/etc_mx.go +++ b/tests/etc/etc_mx.go @@ -8,6 +8,7 @@ import ( "git.sr.ht/~cco/go-scopes/core" "git.sr.ht/~cco/go-scopes/core/action" "git.sr.ht/~cco/go-scopes/logging" + "git.sr.ht/~cco/go-scopes/matrix" ) func ConfigMx() lib.Config { @@ -25,7 +26,9 @@ func ConfigMx() lib.Config { test_rcvr := b("test-receiver", core.Start). AddAction("demo", action.Base(AH_MxReceiver)) - app_c.Add(test_rcvr) + cell_0 := b("cell-0", matrix.Start) + + app_c.Add(cell_0, test_rcvr) return app_c } diff --git a/tests/matrix_test.go b/tests/matrix_test.go index e52939f..b2311a3 100644 --- a/tests/matrix_test.go +++ b/tests/matrix_test.go @@ -4,7 +4,9 @@ import ( "os" tbase "testing" + lib "git.sr.ht/~cco/go-scopes" "git.sr.ht/~cco/go-scopes/common/testing" + "git.sr.ht/~cco/go-scopes/core/message" "git.sr.ht/~cco/go-scopes/tests/etc" ) @@ -14,10 +16,25 @@ func TestMatrixApp(tb *tbase.T) { t := testing.SetUpApp(tb, etc.ConfigMx()) t.Run("app-matrix", MxTest) t.TearDownApp("matrix") - t.AssertEqual(t.LogCheck(logfile, true), 3) + t.AssertEqual(t.LogCheck(logfile, true), 6) } func MxTest(t *testing.T) { ctx := t.Ctx - t.AssertEqual(len(ctx.Services()), 2) + t.AssertEqual(len(ctx.Services()), 3) + rcvr := message.SimpleAddress("cell-0") + msg := message.SimpleMessage("create") + lib.Send(ctx, rcvr, msg) +} + +// action handlers + +func MxReceiver(act lib.Action) bool { + t := testing.GetT(act.Context()) + t.AssertEqual(act.Message().Action(), "demo") + return true +} + +func init() { + etc.AH_MxReceiver = MxReceiver }