zitadel/internal/eventstore/event.go

108 lines
2.6 KiB
Go
Raw Normal View History

2020-10-27 15:03:17 +00:00
package eventstore
import (
"encoding/json"
"reflect"
2020-10-27 15:03:17 +00:00
"time"
"github.com/zitadel/zitadel/internal/errors"
2020-10-27 15:03:17 +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
}
// Command is the intend to store an event into the eventstore
type Command interface {
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:
// * 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
2020-10-27 15:03:17 +00:00
}
// Event is a stored activity
type Event interface {
action
// Sequence of the event in the aggregate
2020-11-06 12:47:27 +00:00
Sequence() uint64
// 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
DataAsBytes() []byte
2020-10-27 15:03:17 +00:00
}
feat(cli): setup (#3267) * commander * commander * selber! * move to packages * fix(errors): implement Is interface * test: command * test: commands * add init steps * setup tenant * add default step yaml * possibility to set password * merge v2 into v2-commander * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: search query builder can filter events in memory * fix: filters for add member * fix(setup): add `ExternalSecure` to config * chore: name iam to instance * fix: matching * remove unsued func * base url * base url * test(command): filter funcs * test: commands * fix: rename orgiampolicy to domain policy * start from init * commands * config * fix indexes and add constraints * fixes * fix: merge conflicts * fix: protos * fix: md files * setup * add deprecated org iam policy again * typo * fix search query * fix filter * Apply suggestions from code review * remove custom org from org setup * add todos for verification * change apps creation * simplify package structure * fix error * move preparation helper for tests * fix unique constraints * fix config mapping in setup * fix error handling in encryption_keys.go * fix projection config * fix query from old views to projection * fix setup of mgmt api * set iam project and fix instance projection * imports Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: fabi <fabienne.gerschwiler@gmail.com>
2022-03-28 08:05:09 +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
}
return nil, errors.ThrowInvalidArgument(nil, "V2-6SbbS", "data bytes are not json")
}
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 {
return nil, errors.ThrowInvalidArgument(err, "V2-xG87M", "could not marshal data")
}
return dataBytes, nil
}
return nil, errors.ThrowInvalidArgument(nil, "V2-91NRm", "wrong type of event data")
}
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 {
return nil, errors.ThrowInternal(err, "ES-Thai6", "unable to unmarshal event")
}
return e, nil
}
feat(cli): setup (#3267) * commander * commander * selber! * move to packages * fix(errors): implement Is interface * test: command * test: commands * add init steps * setup tenant * add default step yaml * possibility to set password * merge v2 into v2-commander * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: search query builder can filter events in memory * fix: filters for add member * fix(setup): add `ExternalSecure` to config * chore: name iam to instance * fix: matching * remove unsued func * base url * base url * test(command): filter funcs * test: commands * fix: rename orgiampolicy to domain policy * start from init * commands * config * fix indexes and add constraints * fixes * fix: merge conflicts * fix: protos * fix: md files * setup * add deprecated org iam policy again * typo * fix search query * fix filter * Apply suggestions from code review * remove custom org from org setup * add todos for verification * change apps creation * simplify package structure * fix error * move preparation helper for tests * fix unique constraints * fix config mapping in setup * fix error handling in encryption_keys.go * fix projection config * fix query from old views to projection * fix setup of mgmt api * set iam project and fix instance projection * imports Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: fabi <fabienne.gerschwiler@gmail.com>
2022-03-28 08:05:09 +00:00
func isEventTypes(event Event, types ...EventType) bool {
for _, typ := range types {
if event.Type() == typ {
return true
}
}
return false
}