2023-02-09 12:16:36 +01:00
|
|
|
package bus
|
|
|
|
|
|
|
|
import (
|
2023-11-28 16:35:49 +01:00
|
|
|
"context"
|
2023-02-09 12:16:36 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
MessageNamespace string
|
2023-11-28 16:35:49 +01:00
|
|
|
Address string
|
|
|
|
Message any
|
2023-02-09 12:16:36 +01:00
|
|
|
)
|
|
|
|
|
2023-11-28 16:35:49 +01:00
|
|
|
type Envelope interface {
|
|
|
|
Message() any
|
|
|
|
Context() context.Context
|
|
|
|
Address() Address
|
|
|
|
}
|
|
|
|
|
|
|
|
type BaseEnvelope struct {
|
|
|
|
msg Message
|
|
|
|
ctx context.Context
|
|
|
|
addr Address
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEnvelope(ctx context.Context, addr Address, msg Message) *BaseEnvelope {
|
|
|
|
return &BaseEnvelope{
|
|
|
|
ctx: ctx,
|
|
|
|
addr: addr,
|
|
|
|
msg: msg,
|
|
|
|
}
|
2023-02-09 12:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMessageNamespace(namespaces ...MessageNamespace) MessageNamespace {
|
|
|
|
var sb strings.Builder
|
|
|
|
|
|
|
|
for i, ns := range namespaces {
|
|
|
|
if i != 0 {
|
|
|
|
if _, err := sb.WriteString(":"); err != nil {
|
|
|
|
panic(errors.Wrap(err, "could not build new message namespace"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := sb.WriteString(string(ns)); err != nil {
|
|
|
|
panic(errors.Wrap(err, "could not build new message namespace"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return MessageNamespace(sb.String())
|
|
|
|
}
|