message interface and implementation bascically working
This commit is contained in:
parent
8d8b64f8e7
commit
b813dd40db
4 changed files with 83 additions and 21 deletions
|
@ -54,7 +54,12 @@ type Context interface {
|
||||||
// message, address
|
// message, address
|
||||||
|
|
||||||
type MsgHead interface {
|
type MsgHead interface {
|
||||||
|
fmt.Stringer
|
||||||
|
Slice() []string
|
||||||
|
Domain() string
|
||||||
Action() string
|
Action() string
|
||||||
|
Class() string
|
||||||
|
Item() string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Message interface {
|
type Message interface {
|
||||||
|
@ -62,6 +67,8 @@ type Message interface {
|
||||||
Head() MsgHead
|
Head() MsgHead
|
||||||
Sender() Address
|
Sender() Address
|
||||||
Payload() Payload
|
Payload() Payload
|
||||||
|
WithSender(Address) Message
|
||||||
|
WithPayload(Payload) Message
|
||||||
}
|
}
|
||||||
|
|
||||||
type Address interface {
|
type Address interface {
|
||||||
|
|
|
@ -1,31 +1,85 @@
|
||||||
package message
|
package message
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git.sr.ht/~cco/go-scopes/lib"
|
"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
|
return msg
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg strMessage) Action() string {
|
func (msg *message) Sender() lib.Address {
|
||||||
return string(msg)
|
return msg.sender
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg strMessage) Sender() lib.Address {
|
func (msg *message) Payload() lib.Payload {
|
||||||
return nil
|
return msg.payload
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg strMessage) Payload() lib.Payload {
|
func (msg *message) WithSender(s lib.Address) lib.Message {
|
||||||
return nil
|
msg.sender = s
|
||||||
|
return msg
|
||||||
}
|
}
|
||||||
|
|
||||||
func StrMessage(action string) lib.Message {
|
func (msg *message) WithPayload(p lib.Payload) lib.Message {
|
||||||
return strMessage(action)
|
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")
|
||||||
|
|
|
@ -39,14 +39,14 @@ func ConfigTest(t *testing.T) {
|
||||||
func SendTest(t *testing.T) {
|
func SendTest(t *testing.T) {
|
||||||
ctx := t.Ctx
|
ctx := t.Ctx
|
||||||
rcvr := message.SimpleAddress("testing")
|
rcvr := message.SimpleAddress("testing")
|
||||||
msg := message.StrMessage("demo")
|
msg := message.SimpleMessage("demo")
|
||||||
lib.Send(ctx, rcvr, msg)
|
lib.Send(ctx, rcvr, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ClientTest(t *testing.T) {
|
func ClientTest(t *testing.T) {
|
||||||
ctx := t.Ctx
|
ctx := t.Ctx
|
||||||
rcvr := message.SimpleAddress("test-client")
|
rcvr := message.SimpleAddress("test-client")
|
||||||
msg := message.StrMessage("demo")
|
msg := message.SimpleMessage("demo")
|
||||||
lib.Send(ctx, rcvr, msg)
|
lib.Send(ctx, rcvr, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,32 +12,33 @@ import (
|
||||||
func TestUnit(tb *tbase.T) {
|
func TestUnit(tb *tbase.T) {
|
||||||
t := testing.SetUp(tb)
|
t := testing.SetUp(tb)
|
||||||
t.Run("address", AddressTest)
|
t.Run("address", AddressTest)
|
||||||
t.Run("strmsg", StrMessageTest)
|
t.Run("message", MessageTest)
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddressTest(t *testing.T) {
|
func AddressTest(t *testing.T) {
|
||||||
var ad1 lib.Address = message.SimpleAddress("worker")
|
var ad1 lib.Address = message.SimpleAddress("worker")
|
||||||
t.AssertEqual(fmt.Sprint(ad1), "#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)
|
var ad2 lib.Address = message.ParseAddress(astr)
|
||||||
t.AssertEqual(fmt.Sprint(ad2), astr)
|
t.AssertEqual(fmt.Sprint(ad2), astr)
|
||||||
t.AssertEqual(fmt.Sprint(message.ParseAddress("worker")), "#worker//")
|
t.AssertEqual(fmt.Sprint(message.ParseAddress("worker")), "#worker//")
|
||||||
t.AssertEqual(ad1.Url(), "")
|
t.AssertEqual(ad1.Url(), "")
|
||||||
t.AssertEqual(ad2.Url(), "https://scopes.cy7.eu")
|
t.AssertEqual(ad2.Url(), "@scopes.cy7.eu")
|
||||||
sid, iid := ad1.Interaction()
|
sid, iid := ad1.Interaction()
|
||||||
t.AssertEqual(sid, "")
|
t.AssertEqual(sid, "")
|
||||||
t.AssertEqual(iid, "")
|
t.AssertEqual(iid, "")
|
||||||
sid, iid = ad2.Interaction()
|
sid, iid = ad2.Interaction()
|
||||||
t.AssertEqual(sid, "1abc")
|
t.AssertEqual(sid, "1abc")
|
||||||
t.AssertEqual(iid, "2cde")
|
t.AssertEqual(iid, "2cde")
|
||||||
ad1.SetUrl("https://scopes.cy7.eu")
|
ad1.SetUrl("@scopes.cy7.eu")
|
||||||
ad1.SetInteraction("1abc", "2cde")
|
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")
|
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.Action(), "doit")
|
||||||
t.AssertEqual(msg.Head().Action(), "doit")
|
t.AssertEqual(msg.Head().Action(), "doit")
|
||||||
|
t.AssertEqual(fmt.Sprint(msg), "standard/doit//")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue