2020-10-27 15:03:17 +00:00
|
|
|
package eventstore
|
|
|
|
|
|
|
|
import (
|
2023-10-19 10:19:10 +00:00
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
2020-10-27 15:03:17 +00:00
|
|
|
"time"
|
2023-10-19 10:19:10 +00:00
|
|
|
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-10-27 15:03:17 +00:00
|
|
|
)
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
type action interface {
|
|
|
|
Aggregate() *Aggregate
|
|
|
|
|
|
|
|
// Creator is the userid of the user which created the action
|
|
|
|
Creator() string
|
|
|
|
// Type describes the action
|
|
|
|
Type() EventType
|
|
|
|
// Revision of the action
|
|
|
|
Revision() uint16
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
// Command is the intend to store an event into the eventstore
|
|
|
|
type Command interface {
|
2023-10-19 10:19:10 +00:00
|
|
|
action
|
|
|
|
// Payload returns the payload of the event. It represent the changed fields by the event
|
2020-10-27 15:03:17 +00:00
|
|
|
// valid types are:
|
2023-10-19 10:19:10 +00:00
|
|
|
// * nil: no payload
|
|
|
|
// * struct: which can be marshalled to json
|
|
|
|
// * pointer: to struct which can be marshalled to json
|
|
|
|
// * []byte: json marshalled data
|
|
|
|
Payload() any
|
|
|
|
// UniqueConstraints should be added for unique attributes of an event, if nil constraints will not be checked
|
|
|
|
UniqueConstraints() []*UniqueConstraint
|
2024-07-03 15:00:56 +00:00
|
|
|
// Fields should be added for fields which should be indexed for lookup, if nil fields will not be indexed
|
|
|
|
Fields() []*FieldOperation
|
2020-10-27 15:03:17 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
// Event is a stored activity
|
|
|
|
type Event interface {
|
2023-10-19 10:19:10 +00:00
|
|
|
action
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
// Sequence of the event in the aggregate
|
2020-11-06 12:47:27 +00:00
|
|
|
Sequence() uint64
|
2023-10-19 10:19:10 +00:00
|
|
|
// CreatedAt is the time the event was created at
|
|
|
|
CreatedAt() time.Time
|
|
|
|
// Position is the global position of the event
|
|
|
|
Position() float64
|
|
|
|
|
|
|
|
// Unmarshal parses the payload and stores the result
|
|
|
|
// in the value pointed to by ptr. If ptr is nil or not a pointer,
|
|
|
|
// Unmarshal returns an error
|
|
|
|
Unmarshal(ptr any) error
|
|
|
|
|
|
|
|
// Deprecated: only use for migration
|
2021-02-24 12:24:33 +00:00
|
|
|
DataAsBytes() []byte
|
2020-10-27 15:03:17 +00:00
|
|
|
}
|
2022-03-28 08:05:09 +00:00
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
type EventType string
|
|
|
|
|
|
|
|
func EventData(event Command) ([]byte, error) {
|
|
|
|
switch data := event.Payload().(type) {
|
|
|
|
case nil:
|
|
|
|
return nil, nil
|
|
|
|
case []byte:
|
|
|
|
if json.Valid(data) {
|
|
|
|
return data, nil
|
|
|
|
}
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "V2-6SbbS", "data bytes are not json")
|
2023-10-19 10:19:10 +00:00
|
|
|
}
|
|
|
|
dataType := reflect.TypeOf(event.Payload())
|
|
|
|
if dataType.Kind() == reflect.Ptr {
|
|
|
|
dataType = dataType.Elem()
|
|
|
|
}
|
|
|
|
if dataType.Kind() == reflect.Struct {
|
|
|
|
dataBytes, err := json.Marshal(event.Payload())
|
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(err, "V2-xG87M", "could not marshal data")
|
2023-10-19 10:19:10 +00:00
|
|
|
}
|
|
|
|
return dataBytes, nil
|
|
|
|
}
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "V2-91NRm", "wrong type of event data")
|
2023-10-19 10:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type BaseEventSetter[T any] interface {
|
|
|
|
Event
|
|
|
|
SetBaseEvent(*BaseEvent)
|
|
|
|
*T
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenericEventMapper[T any, PT BaseEventSetter[T]](event Event) (Event, error) {
|
|
|
|
e := PT(new(T))
|
|
|
|
e.SetBaseEvent(BaseEventFromRepo(event))
|
|
|
|
|
|
|
|
err := event.Unmarshal(e)
|
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "ES-Thai6", "unable to unmarshal event")
|
2023-10-19 10:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2023-12-01 12:25:41 +00:00
|
|
|
func isEventTypes(command Command, types ...EventType) bool {
|
2022-03-28 08:05:09 +00:00
|
|
|
for _, typ := range types {
|
2023-12-01 12:25:41 +00:00
|
|
|
if command.Type() == typ {
|
2022-03-28 08:05:09 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|