Skip to content

Commit

Permalink
Add CommandIDer and FromCommand event option
Browse files Browse the repository at this point in the history
  • Loading branch information
maxekman committed Dec 21, 2020
1 parent 7de70c6 commit e9410af
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
6 changes: 6 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ func (ct CommandType) String() string {
return string(ct)
}

// CommandIDer provides a unique command ID to be used for request tracking etc.
type CommandIDer interface {
// CommandID returns the ID of the command instance being handled.
CommandID() uuid.UUID
}

// ErrCommandNotRegistered is when no command factory was registered.
var ErrCommandNotRegistered = errors.New("command not registered")

Expand Down
13 changes: 13 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ func WithMetadata(metadata map[string]interface{}) EventOption {
}
}

// FromCommand adds metadat for the originating command when crating an event.
// Currently it adds the command type and optionally a command ID (if the
// CommandIDer interface is implemented).
func FromCommand(cmd Command) EventOption {
md := map[string]interface{}{
"command_type": cmd.CommandType().String(),
}
if c, ok := cmd.(CommandIDer); ok {
md["command_id"] = c.CommandID().String()
}
return WithMetadata(md)
}

// NewEvent creates a new event with a type and data, setting its timestamp.
func NewEvent(eventType EventType, data EventData, timestamp time.Time, options ...EventOption) Event {
e := &event{
Expand Down
28 changes: 27 additions & 1 deletion event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,14 @@ func TestNewEvent(t *testing.T) {
}

id := uuid.New()
cmd := TestCommandID{
TestID: id,
Content: "content",
CmdID: uuid.New(),
}
event = NewEvent(TestEventType, &TestEventData{"event1"}, timestamp,
ForAggregate(TestAggregateType, id, 3),
FromCommand(cmd),
WithMetadata(map[string]interface{}{"meta": "data", "num": 42}),
)
if event.EventType() != TestEventType {
Expand All @@ -64,7 +70,12 @@ func TestNewEvent(t *testing.T) {
if event.Version() != 3 {
t.Error("the version should be zero:", event.Version())
}
if !reflect.DeepEqual(event.Metadata(), map[string]interface{}{"meta": "data", "num": 42}) {
if !reflect.DeepEqual(event.Metadata(), map[string]interface{}{
"meta": "data",
"num": 42,
"command_type": cmd.CommandType().String(),
"command_id": cmd.CmdID.String(),
}) {
t.Error("the metadata should be correct:", event.Metadata())
}
if event.String() != "TestEvent@3" {
Expand Down Expand Up @@ -160,3 +171,18 @@ type TestEventRegisterEmptyData struct{}
type TestEventRegisterTwiceData struct{}

type TestEventUnregisterTwiceData struct{}

type TestCommandID struct {
TestID uuid.UUID
Content string
CmdID uuid.UUID
}

var _ = Command(TestCommandID{})

func (t TestCommandID) AggregateID() uuid.UUID { return t.TestID }
func (t TestCommandID) AggregateType() AggregateType { return TestAggregateType }
func (t TestCommandID) CommandType() CommandType {
return CommandType("TestCommandID")
}
func (t TestCommandID) CommandID() uuid.UUID { return t.CmdID }

0 comments on commit e9410af

Please sign in to comment.