go-scopes/lib/message/message.go

85 lines
1.3 KiB
Go

package message
import (
"strings"
"git.sr.ht/~cco/go-scopes/lib"
)
// message head
type head struct {
domain, action, class, item string
}
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 *message) Sender() lib.Address {
return msg.sender
}
func (msg *message) Payload() lib.Payload {
return msg.payload
}
func (msg *message) WithSender(s lib.Address) lib.Message {
msg.sender = s
return msg
}
func (msg *message) WithPayload(p lib.Payload) lib.Message {
msg.payload = p
return msg
}
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")