2024-04-26 15:05:21 +00:00
|
|
|
package eventstore
|
|
|
|
|
2024-05-23 04:36:08 +00:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Unmarshal func(ptr any) error
|
|
|
|
|
|
|
|
type Payload interface {
|
|
|
|
Unmarshal | any
|
|
|
|
}
|
|
|
|
|
|
|
|
type Action[P Payload] struct {
|
|
|
|
Creator string
|
|
|
|
Type string
|
|
|
|
Revision uint16
|
|
|
|
Payload P
|
|
|
|
}
|
|
|
|
|
|
|
|
type Command struct {
|
|
|
|
Action[any]
|
|
|
|
UniqueConstraints []*UniqueConstraint
|
|
|
|
}
|
|
|
|
|
|
|
|
type StorageEvent struct {
|
|
|
|
Action[Unmarshal]
|
2024-04-26 15:05:21 +00:00
|
|
|
|
|
|
|
Aggregate Aggregate
|
|
|
|
CreatedAt time.Time
|
|
|
|
Position GlobalPosition
|
|
|
|
Sequence uint32
|
|
|
|
}
|
|
|
|
|
2024-05-23 04:36:08 +00:00
|
|
|
type Event[P any] struct {
|
|
|
|
*StorageEvent
|
|
|
|
Payload P
|
2024-04-26 15:05:21 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 04:36:08 +00:00
|
|
|
func UnmarshalPayload[P any](unmarshal Unmarshal) (P, error) {
|
2024-04-26 15:05:21 +00:00
|
|
|
var payload P
|
2024-05-23 04:36:08 +00:00
|
|
|
err := unmarshal(&payload)
|
|
|
|
return payload, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type EmptyPayload struct{}
|
|
|
|
|
|
|
|
type TypeChecker interface {
|
|
|
|
ActionType() string
|
|
|
|
}
|
2024-04-26 15:05:21 +00:00
|
|
|
|
2024-05-23 04:36:08 +00:00
|
|
|
func Type[T TypeChecker]() string {
|
|
|
|
var t T
|
|
|
|
return t.ActionType()
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsType[T TypeChecker](types ...string) bool {
|
|
|
|
gotten := Type[T]()
|
|
|
|
|
|
|
|
for _, typ := range types {
|
|
|
|
if gotten == typ {
|
|
|
|
return true
|
|
|
|
}
|
2024-04-26 15:05:21 +00:00
|
|
|
}
|
2024-05-23 04:36:08 +00:00
|
|
|
|
|
|
|
return false
|
2024-04-26 15:05:21 +00:00
|
|
|
}
|