diff --git a/lib/lib.go b/lib/lib.go index 5c911b3..19c2581 100644 --- a/lib/lib.go +++ b/lib/lib.go @@ -54,7 +54,12 @@ type Context interface { // message, address type MsgHead interface { + fmt.Stringer + Slice() []string + Domain() string Action() string + Class() string + Item() string } type Message interface { @@ -62,6 +67,8 @@ type Message interface { Head() MsgHead Sender() Address Payload() Payload + WithSender(Address) Message + WithPayload(Payload) Message } type Address interface { diff --git a/lib/message/message.go b/lib/message/message.go index accf767..5511515 100644 --- a/lib/message/message.go +++ b/lib/message/message.go @@ -1,31 +1,85 @@ package message import ( + "strings" + "git.sr.ht/~cco/go-scopes/lib" ) -// Message implementation(s) +// message head -type strMessage string +type head struct { + domain, action, class, item string +} -func (msg strMessage) Head() lib.MsgHead { +func (h *head) String() string { + return strings.Join(h.Slice(), "/") +} + +func (h *head) Slice() []string { + return []string{h.domain, h.action, h.class, h.item} +} + +func (h *head) Domain() string { + return h.domain +} + +func (h *head) Action() string { + return h.action +} + +func (h *head) Class() string { + return h.class +} + +func (h *head) Item() string { + return h.item +} + +// message + +type message struct { + *head + sender lib.Address + payload lib.Payload +} + +func (msg *message) Head() lib.MsgHead { return msg } -func (msg strMessage) Action() string { - return string(msg) +func (msg *message) Sender() lib.Address { + return msg.sender } -func (msg strMessage) Sender() lib.Address { - return nil +func (msg *message) Payload() lib.Payload { + return msg.payload } -func (msg strMessage) Payload() lib.Payload { - return nil +func (msg *message) WithSender(s lib.Address) lib.Message { + msg.sender = s + return msg } -func StrMessage(action string) lib.Message { - return strMessage(action) +func (msg *message) WithPayload(p lib.Payload) lib.Message { + msg.payload = p + return msg } -var Quit = StrMessage("quit") +func NewMessage(args ...string) lib.Message { + args = append(args, "", "")[:4] + return &message{ + head: &head{ + domain: args[0], + action: args[1], + class: args[2], + item: args[3], + }, + } +} + +func SimpleMessage(action string) lib.Message { + return NewMessage("standard", action) +} + +var Quit = SimpleMessage("quit") diff --git a/tests/scopes_test.go b/tests/scopes_test.go index fb7108c..6c74db5 100644 --- a/tests/scopes_test.go +++ b/tests/scopes_test.go @@ -39,14 +39,14 @@ func ConfigTest(t *testing.T) { func SendTest(t *testing.T) { ctx := t.Ctx rcvr := message.SimpleAddress("testing") - msg := message.StrMessage("demo") + msg := message.SimpleMessage("demo") lib.Send(ctx, rcvr, msg) } func ClientTest(t *testing.T) { ctx := t.Ctx rcvr := message.SimpleAddress("test-client") - msg := message.StrMessage("demo") + msg := message.SimpleMessage("demo") lib.Send(ctx, rcvr, msg) } diff --git a/tests/unit_test.go b/tests/unit_test.go index 6fe874a..3120e87 100644 --- a/tests/unit_test.go +++ b/tests/unit_test.go @@ -12,32 +12,33 @@ import ( func TestUnit(tb *tbase.T) { t := testing.SetUp(tb) t.Run("address", AddressTest) - t.Run("strmsg", StrMessageTest) + t.Run("message", MessageTest) } func AddressTest(t *testing.T) { var ad1 lib.Address = message.SimpleAddress("worker") t.AssertEqual(fmt.Sprint(ad1), "#worker//") - astr := "https://scopes.cy7.eu#server/1abc/2cde" + astr := "@scopes.cy7.eu#server/1abc/2cde" var ad2 lib.Address = message.ParseAddress(astr) t.AssertEqual(fmt.Sprint(ad2), astr) t.AssertEqual(fmt.Sprint(message.ParseAddress("worker")), "#worker//") t.AssertEqual(ad1.Url(), "") - t.AssertEqual(ad2.Url(), "https://scopes.cy7.eu") + t.AssertEqual(ad2.Url(), "@scopes.cy7.eu") sid, iid := ad1.Interaction() t.AssertEqual(sid, "") t.AssertEqual(iid, "") sid, iid = ad2.Interaction() t.AssertEqual(sid, "1abc") t.AssertEqual(iid, "2cde") - ad1.SetUrl("https://scopes.cy7.eu") + ad1.SetUrl("@scopes.cy7.eu") ad1.SetInteraction("1abc", "2cde") - t.AssertEqual(fmt.Sprint(ad1), "https://scopes.cy7.eu#worker/1abc/2cde") + t.AssertEqual(fmt.Sprint(ad1), "@scopes.cy7.eu#worker/1abc/2cde") } -func StrMessageTest(t *testing.T) { +func MessageTest(t *testing.T) { t.AssertEqual(message.Quit.Action(), "quit") - var msg lib.Message = message.StrMessage("doit") + var msg lib.Message = message.SimpleMessage("doit") t.AssertEqual(msg.Action(), "doit") t.AssertEqual(msg.Head().Action(), "doit") + t.AssertEqual(fmt.Sprint(msg), "standard/doit//") }