-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
50 lines (40 loc) · 1.1 KB
/
command.go
File metadata and controls
50 lines (40 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package eventsourcerer
import (
"fmt"
"reflect"
)
type Command interface {
Reject(err error) error
}
// The CommandHandler processes the Command. After checking the validity of the state
// transition it applies the the Command to the Aggregate, which itself creates a
// transient event from it.
type CommandHandler interface {
Handle(Command) error
}
type CommandRejectedError struct {
Command Command
Err error
}
func (e CommandRejectedError) Error() string {
return fmt.Sprintf("%s: ", reflect.TypeOf(e.Command).String()) + e.Err.Error()
}
func NewCommandRejectedError(cmd Command, err error) CommandRejectedError {
return CommandRejectedError{
Command: cmd,
Err: err,
}
}
// The BaseCommand is the foundation for every command
type BaseCommand struct {
}
// Reject issues a CommandRejectedError
func (bc BaseCommand) Reject(err error) error {
return NewCommandRejectedError(bc, err)
}
// The BaseCommandHandler is the foundation for a command handler
type BaseCommandHandler struct {
}
func (h *BaseCommandHandler) Reject(cmd Command, err error) {
fmt.Printf("REJECT: %+v %s\n", cmd, err)
}