behavior

package
v0.0.0-...-42841ab Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2022 License: MIT Imports: 10 Imported by: 16

Documentation

Index

Constants

View Source
const (
	NoTopic                          = "No topic"
	InvalidEventVersion              = "invalid event version"
	InvalidBehaviorID                = "invalid behavior id"
	InvalidBehavior                  = "invalid behavior"
	InvalidBehaviorType              = "invalid behavior type"
	InvalidCommandType               = "invalid command type"
	InvalidEventType                 = "invalid event type"
	BehaviorNotFound                 = "behavior not found"
	AlreadyExists                    = "already exists"
	PayloadMustNotBeEmpty            = "payload cmd_must not be empty"
	CommandCannotBeNil               = "command cannot be nil"
	CommandMustHaveBehaviorID        = "command cmd_must have behavior id"
	FailedToGetJsonData              = "failed to get json data"
	CommandTypeMustNotBeEmpty        = "command type cmd_must not be empty"
	EventHasNoBehaviorID             = "event has no behavior id"
	NoApplierForEvent                = "no F(apply) for event [%+v]"
	NoExecuterForCommand             = "no F(try) for command [%+v]"
	HopeCannotBeNil                  = "Hope cannot be nil"
	ExecuteDidNotReturnAnEvent       = "[%+v].execute did not return an event"
	TheBehaviorHasNoIDPleaseUseSetID = "the behavior has no Id, please use SetID()"
	BehaviorIDCannotBeNil            = "behaviorID cannot be nil"
)
View Source
const (
	CannotBeNil = "behavior cannot be nil"
)

Variables

View Source
var (
	ErrCommandCannotBeNil        = errors.New(CommandCannotBeNil)
	ErrPayloadMustNotBeEmpty     = errors.New(PayloadMustNotBeEmpty)
	ErrCommandMustHaveBehaviorID = errors.New(CommandMustHaveBehaviorID)
	ErrFailedToGetJsonData       = errors.New(FailedToGetJsonData)
	ErrCommandTypeMustNotBeEmpty = errors.New(CommandTypeMustNotBeEmpty)
	ErrAlreadyExists             = errors.New(AlreadyExists)
	ErrBehaviorNotFound          = errors.New(BehaviorNotFound)
	ErrInvalidEventType          = errors.New(InvalidEventType)
	ErrInvalidCommandType        = errors.New(InvalidCommandType)
	ErrInvalidBehavior           = errors.New(InvalidBehavior)
	ErrInvalidBehaviorID         = errors.New(InvalidBehaviorID)
	ErrInvalidEventVersion       = errors.New(InvalidEventVersion)
	ErrEventHasNoBehaviorID      = errors.New(EventHasNoBehaviorID)
	ErrInvalidBehaviorType       = errors.New(InvalidBehaviorType)
	ErrHopeCannotBeNil           = errors.New(HopeCannotBeNil)
	ErrNoTopic                   = errors.New(NoTopic)
	ErrTheBehaviorHasNoID        = errors.New(TheBehaviorHasNoIDPleaseUseSetID)
	ErrBehaviorIDCannotBeNil     = errors.New(BehaviorIDCannotBeNil)
)
View Source
var (
	ErrBehaviorCannotBeNil = fmt.Errorf(CannotBeNil)
)

Functions

func ErrExecuteDidNotReturnAnEvent

func ErrExecuteDidNotReturnAnEvent(commandType string) string

func ImplementsITopic

func ImplementsITopic(topic ITopic) bool

func IsBehaviorFound

func IsBehaviorFound(behavior IBehavior) bool

IsBehaviorFound checks the version. If it is != 0, the behavior is found.

func NewDomainMsg

func NewDomainMsg[TID schema.IIdentity, TP schema.IPayload](aID TID, payload TP) *domainMsg[TID, TP]

Types

type ApplyEvt

type ApplyEvt struct {
	// contains filtered or unexported fields
}

func NewApplyEvt

func NewApplyEvt(
	eventType EventType,
	fApply FApply,
) *ApplyEvt

NewApplyEvt lets you create an Event Applier and requires that you pass an FApply function. your Event Applier is automatically injected into the Aggregate.

func (*ApplyEvt) ApplyEvent

func (a *ApplyEvt) ApplyEvent(state schema.ISchema, event IEvt) error

func (*ApplyEvt) GetAggregate

func (a *ApplyEvt) GetAggregate() IBehavior

func (*ApplyEvt) GetEventType

func (a *ApplyEvt) GetEventType() EventType

func (*ApplyEvt) SetBehavior

func (a *ApplyEvt) SetBehavior(agg IBehavior)

type BehaviorBuilder

type BehaviorBuilder func() IBehavior

type BehaviorFtor

type BehaviorFtor func() IBehavior

type BehaviorPluginFtor

type BehaviorPluginFtor func() IBehaviorPlugin

type BehaviorType

type BehaviorType string

type Cmd

type Cmd struct {
	// contains filtered or unexported fields
}

func NewCmd

func NewCmd(behaviorID schema.IIdentity, commandType CommandType, payload []byte) (*Cmd, error)

func (*Cmd) GetBehaviorID

func (c *Cmd) GetBehaviorID() schema.IIdentity

func (*Cmd) GetCommandType

func (c *Cmd) GetCommandType() CommandType

func (*Cmd) GetJsonPayload

func (c *Cmd) GetJsonPayload(pl schema.IPayload) error

GetJsonPayload serializes the payload []byte and returns a reference to an IPayload struct.

func (*Cmd) GetPayload

func (c *Cmd) GetPayload() []byte

func (*Cmd) SetJsonPayload

func (c *Cmd) SetJsonPayload(pl schema.IPayload) error

SetJsonPayload accepts the payload and serializes it to the payload []byte

type CommandType

type CommandType string

type Event

type Event struct {
	EventId       string
	EventType     EventType
	Data          []byte
	Timestamp     time.Time
	AggregateType BehaviorType
	AggregateID   string
	Version       int64
	Metadata      []byte
}

func (*Event) CreatedDate

func (e *Event) CreatedDate() time.Time

func (*Event) EventNumber

func (e *Event) EventNumber() uint64

func (*Event) GetBehaviorID

func (e *Event) GetBehaviorID() (schema.IIdentity, error)

func (*Event) GetBehaviorId

func (e *Event) GetBehaviorId() string

GeTAID is the Id of the IBehavior that the Event belongs to

func (*Event) GetBehaviorType

func (e *Event) GetBehaviorType() BehaviorType

GetAggregateType is the BehaviorType that the Event can be applied to.

func (*Event) GetData

func (e *Event) GetData() []byte

GetData The data attached to the Event serialized to bytes.

func (*Event) GetEventId

func (e *Event) GetEventId() string

func (*Event) GetEventType

func (e *Event) GetEventType() EventType

func (*Event) GetEventTypeString

func (e *Event) GetEventTypeString() string

GetEventType returns the EventType of the event.

func (*Event) GetJsonMetadata

func (e *Event) GetJsonMetadata(metaData interface{}) error

GetJsonMetadata unmarshal domain-specific metadata serialized as json for the Event.

func (*Event) GetMetadata

func (e *Event) GetMetadata() []byte

GetMetadata is domain-specific metadata such as request Id, originating user etc.

func (*Event) GetPayload

func (e *Event) GetPayload(data interface{}) error

GetPayload json unmarshal data attached to the Event.

func (*Event) GetStreamId

func (e *Event) GetStreamId() string

func (*Event) GetString

func (e *Event) GetString() string

GetString A string representation of the Event.

func (*Event) GetTimeStamp

func (e *Event) GetTimeStamp() time.Time

GetTimeStamp get timestamp of the Event.

func (*Event) GetVersion

func (e *Event) GetVersion() int64

GetVersion is the version of the IBehavior after the Event has been applied.

func (*Event) SetBehaviorId

func (e *Event) SetBehaviorId(id string)

func (*Event) SetBehaviorType

func (e *Event) SetBehaviorType(aggregateType BehaviorType)

SetAggregateType set the BehaviorType that the Event can be applied to.

func (*Event) SetData

func (e *Event) SetData(data []byte) *Event

SetData add the data attached to the Event serialized to bytes.

func (*Event) SetMetadata

func (e *Event) SetMetadata(metaData interface{}) error

SetMetadata add domain-specific metadata serialized as json for the Event.

func (*Event) SetPayload

func (e *Event) SetPayload(data interface{}) error

SetPayload serialize to json and set data attached to the Event.

func (*Event) SetVersion

func (e *Event) SetVersion(aggregateVersion int64)

SetVersion set the version of the IBehavior.

func (*Event) String

func (e *Event) String() string

type EventType

type EventType string
const (
	AllTopics EventType = "*"
)

type Evt2CmdFunc

type Evt2CmdFunc func(evt IEvt) (ICmd, error)

type Evt2DocFtor

type Evt2DocFtor[TEvt IEvt, TDoc schema.ISchema] func() Evt2DocFunc[TEvt, TDoc]

type Evt2DocFunc

type Evt2DocFunc[TEvt IEvt, TDoc schema.ISchema] func(evt TEvt, model *TDoc) error

type Evt2FactFunc

type Evt2FactFunc func(evt IEvt) (contract.IFact, error)

type FApply

type FApply func(state schema.ISchema, evt IEvt) error

type Fact2CmdFunc

type Fact2CmdFunc[TFact contract.IFact, TCmd ICmd] func(fact *contract.Dto) (TCmd, error)

type GenBehaviorFtor

type GenBehaviorFtor[TDoc schema.ISchema] func() IBehavior

type GenData2CmdFunc

type GenData2CmdFunc[TCmd ICmd] func([]byte) (TCmd, error)

type GenEvt2FactFunc

type GenEvt2FactFunc[TFact contract.IFact] func(evt IEvt) (TFact, error)

type GenReacter

type GenReacter[TEvt IEvt] interface {
	React(ctx context.Context, evt TEvt) error
}

type Hope2CmdFunc

type Hope2CmdFunc[THope contract.IHope, TCmd ICmd] func(hope *contract.Dto) (TCmd, error)

type IApplyEvt

type IApplyEvt interface {
	IBehaviorPlugin
	IGetEvtType
	ApplyEvent(state schema.ISchema, evt IEvt) error
}

IApplyEvt is an IBehaviorPlugin injector that allows us to inject Event Appliers into the Aggregate

type IBehavior

type IBehavior interface {
	ISimpleBehavior
	Inject(agg IBehavior, actors ...BehaviorPluginFtor) IBehavior
	KnowsCmd(topic CommandType) bool
	KnowsEvt(topic EventType) bool
}

IBehavior is the injector for Aggregates In an Event Sourced application, the Aggregate can be considered as the equivalent of the ActiveRecord in classic CrUD (Create, Upsert, Delete) applications. The Aggregate can be considered as the heart of an ES system, that unites State with Behavior. Its main responsibilities are:

  1. to build a (volatile) State from an ordered list of previously committed Events, that are sourced from the Event Stream that is identified by the Id.
  2. CanAcceptName Command requests and applying business logic that checks whether the Command (ICmd) is allowed to be executed or not, according to a number of Specifications, the Current State (see 1.) and the Command's FactPayload
  3. If Command execution is allowed, Raise a new Event and ApplyEvent it to itself, as to update the Current State to the New State.

Specifically for GO-SCREAM CMD Applications, given their modular nature, we rely on Aggregate Composition. Aggregate Composition is a technique that allows us to inject a series of BehaviorPluginFtor functors that create a feature's IApplyEvt and ITryCmd injectors, in order to compose an Aggregate that has all the capabilities required to process the Event Stream.

func NewBehavior

func NewBehavior(behaviorType BehaviorType, state schema.IModel) IBehavior

NewBehavior initializes a new empty Aggregate

type IBehaviorPlugin

type IBehaviorPlugin interface {
	ISetBehavior
}

IBehaviorPlugin is an injector that allows us to inject ITryCmd and IApplyEvt injectors into the Aggregate in an elegant way.

type ICmd

type ICmd interface {
	IGetCmdType
	GetBehaviorID() schema.IIdentity
	GetPayload() []byte                      //
	GetJsonPayload(pl schema.IPayload) error //
	SetJsonPayload(pl schema.IPayload) error
}

ICmd is an injector that represents a behavior.Cmd

type IEvt

type IEvt interface {
	IGetEvtType
	IGetBehaviorType
	ISetBehaviorType
	GetBehaviorID() (schema.IIdentity, error)
	GetStreamId() string
	EventNumber() uint64
	CreatedDate() time.Time
	GetEventId() string
	GetTimeStamp() time.Time
	GetData() []byte
	SetData(data []byte) *Event
	GetPayload(data interface{}) error
	SetPayload(data interface{}) error
	GetEventTypeString() string
	GetBehaviorId() string
	GetVersion() int64
	SetVersion(version int64)
	GetMetadata() []byte
	SetMetadata(metaData interface{}) error
	GetJsonMetadata(metaData interface{}) error
	GetString() string
	String() string
	SetBehaviorId(id string)
}

func NewEvt

func NewEvt(aggregate IBehavior, eventType EventType) IEvt

type IGenReacter

type IGenReacter[TEvt IEvt] interface {
	GenReacter[TEvt]
}

type IGetBehavior

type IGetBehavior interface {
	GetBehavior() IBehavior
}

type IGetBehaviorType

type IGetBehaviorType interface {
	GetBehaviorType() BehaviorType
}

type IGetCmdType

type IGetCmdType interface {
	GetCommandType() CommandType
}

type IGetEvtType

type IGetEvtType interface {
	GetEventType() EventType
}

type ILoadEvents

type ILoadEvents interface {
	Load(events []Event) error
}

type IModelStore

type IModelStore[T schema.ISchema] interface {
	IStore
	Exists(ctx context.Context, key string) (bool, error)
	Get(ctx context.Context, key string) (*T, error)
	Set(ctx context.Context, key string, data T) (string, error)
	Delete(ctx context.Context, key string) (*T, error)
}

IModelStore is the Injector for a Store that is discriminated by the Read-Model Type Injector

type ISetBehavior

type ISetBehavior interface {
	SetBehavior(a IBehavior)
}

type ISetBehaviorType

type ISetBehaviorType interface {
	SetBehaviorType(behaviorType BehaviorType)
}

type ISimpleBehavior

type ISimpleBehavior interface {
	IGetBehaviorType
	String() string
	TryCommand(ctx context.Context, command ICmd) (IEvt, contract.IFbk)
	ApplyEvent(event IEvt, isCommitted bool) error
	GetState() schema.IModel
	GetUncommittedEvents() []IEvt
	ClearUncommittedEvents()
	SetAppliedEvents(events []IEvt)
	GetAppliedEvents() []IEvt
	ToSnapshot()
	RaiseEvent(event IEvt) error
	GetVersion() int64
	GetID() schema.IIdentity
	SetID(identity schema.IIdentity) IBehavior
}

ISimpleBehavior is an injector that abstracts the implementation of a traditional monolithic Aggregate.

type IStore

type IStore interface{}

IStore is an untyped Injector for the cache

type ITopic

type ITopic interface {
	Topic() Topic
}

type ITryCmd

type ITryCmd interface {
	IBehaviorPlugin
	IGetCmdType
	TryCommand(ctx context.Context, command ICmd) (IEvt, contract.IFbk)
}

ITryCmd is an IBehaviorPlugin injector that allows us to inject Command Executors into the Aggregate

type MediatorTopic

type MediatorTopic string

type Position

type Position struct {
	Commit  uint64
	Prepare uint64
}

type RaiseEventsFunc

type RaiseEventsFunc func(context.Context, ICmd) (IEvt, contract.IFbk)

type Reacter

type Reacter interface {
	React(ctx context.Context, evt IEvt) error
}

type Snapshot

type Snapshot struct {
	Id      string       `json:"id"`
	Type    BehaviorType `json:"type"`
	State   []byte       `json:"state"`
	Version uint64       `json:"version"`
}

Snapshot Event Sourcing Snapshotting is an optimisation that reduces time spent on reading event from an event store.

func ToSnapshot

func ToSnapshot(behavior IBehavior) (*Snapshot, error)

ToSnapshot create new snapshot from the Aggregate state.

type StoreFtor

type StoreFtor[T schema.ISchema] func() IModelStore[T]

StoreFtor of Type ISchema is a functor type for functions that return an IModelStore that returns a

type Topic

type Topic string

type TryCmd

type TryCmd struct {
	// contains filtered or unexported fields
}

TryCmd is the -base receiver for executor extensions

func NewTryCmd

func NewTryCmd(cmdType CommandType, raise RaiseEventsFunc) *TryCmd

NewTryCmd returns a new Command Executor for cndType and allows you to supply an executor function. It automatically registers the TryCmd into the Aggregate.

func (*TryCmd) GetBehavior

func (e *TryCmd) GetBehavior() IBehavior

GetBehavior returns the Aggregate tha this TryCmd is part of.

func (*TryCmd) GetCommandType

func (e *TryCmd) GetCommandType() CommandType

func (*TryCmd) SetBehavior

func (e *TryCmd) SetBehavior(beh IBehavior)

SetBehavior sets the behavior of this TryCmd

func (*TryCmd) TryCommand

func (e *TryCmd) TryCommand(ctx context.Context, command ICmd) (IEvt, contract.IFbk)

TryCommand is the entry point for the TryCmd and is called by the composed behavior

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL