This commit is contained in:
@@ -233,3 +233,33 @@ func (p *property[T]) SetEventMeta(x T) {
|
||||
p.v = x
|
||||
}
|
||||
}
|
||||
|
||||
func AsEvent[T any](e T) Event {
|
||||
return &asEvent[T]{payload: e}
|
||||
}
|
||||
|
||||
type asEvent [T any] struct {
|
||||
payload T
|
||||
IsEvent
|
||||
}
|
||||
func (e asEvent[T]) Payload() T {
|
||||
return e.payload
|
||||
}
|
||||
|
||||
|
||||
type AGG interface{ApplyEvent(...Event)}
|
||||
|
||||
func AsAggregate[T AGG](e T) Aggregate {
|
||||
return &asAggregate[T]{payload: e}
|
||||
}
|
||||
|
||||
type asAggregate [T AGG] struct {
|
||||
payload T
|
||||
IsAggregate
|
||||
}
|
||||
func (e *asAggregate[T]) Payload() T {
|
||||
return e.payload
|
||||
}
|
||||
func (e *asAggregate[T]) ApplyEvent(lis ...Event) {
|
||||
e.payload.ApplyEvent(lis...)
|
||||
}
|
||||
@@ -55,3 +55,28 @@ func TestEventEncode(t *testing.T) {
|
||||
is.Equal(lis[i], chk[i])
|
||||
}
|
||||
}
|
||||
|
||||
type exampleAgg struct{ value string }
|
||||
|
||||
func (a *exampleAgg) ApplyEvent(lis ...event.Event) {
|
||||
for _, e := range lis {
|
||||
switch e := e.(type) {
|
||||
case interface{ Payload() exampleEvSetValue }:
|
||||
a.value = e.Payload().value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type exampleEvSetValue struct{ value string }
|
||||
|
||||
func TestApplyEventGeneric(t *testing.T) {
|
||||
payload := &exampleAgg{}
|
||||
var agg = event.AsAggregate(payload)
|
||||
|
||||
agg.ApplyEvent(event.NewEvents(
|
||||
event.AsEvent(exampleEvSetValue{"hello"}),
|
||||
)...)
|
||||
|
||||
is := is.New(t)
|
||||
is.Equal(payload.value, "hello")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user