event

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2023 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PAYLOAD   = "payload"
	METADATA  = "metadata"
	TRACE     = "trace"
	TENANT    = "tenant"
	TIMESTAMP = "timestamp"
)

Variables

This section is empty.

Functions

func GetEventLogger added in v0.4.0

func GetEventLogger() *zerolog.Logger

func SetEventLogger added in v0.4.0

func SetEventLogger(l *zerolog.Logger)

Types

type Event

type Event interface {
	//Get the event payload
	Payload() interface{}

	//Get event id
	Id() string

	//Get the event metadata
	Metadata() map[string]interface{}

	//Get the event tenant
	Tenant() tenant.Id

	//Get the event context
	Context() context.Context

	//Get the event creation time
	Created() time.Time

	//Set the event payload
	//Will return an error if the event is done
	SetPayload(payload interface{}) error

	//Set the event metadata
	//Will return an error if the event is done
	SetMetadata(metadata map[string]interface{}) error

	// SetPathValue sets object value at path in either payload or metadata and returns its parent
	// object and parent key if those exist
	SetPathValue(path string, val interface{}, createPath bool) (interface{}, string, error)

	// GetPathValue finds object at path in either payload or metadata and returns such object
	// if one exists along with its parent object and parent key if those exist
	GetPathValue(path string) (interface{}, interface{}, string)

	//Evaluate processes expressions like "my {payload.some.feature} thing", if the expression is a
	// simple path like "{payload.some.feature}" it will act like GetPathValue()
	Evaluate(expression interface{}) (interface{}, interface{}, string)

	//Replace the current event context
	//Will return an error if the event is done
	SetContext(ctx context.Context) error

	//Acknowledge that the event is handled successfully
	//Further actions on the event are no longer possible
	Ack()

	//Acknowledge that there is an error handling the event
	//Further actions on the event are no longer possible
	Nack(err error)

	//Create a child event with payload shallow-copied
	//Clone fails and return an error if the event is already acknowledged
	Clone(ctx context.Context) (Event, error)

	//Deep copy event payload and metadata
	DeepCopy() error
}

func New

func New(ctx context.Context, payload interface{}, options ...EventOption) (Event, error)

Create a new event given a context, a payload, and other event options

type EventMock

type EventMock struct {
	// AckFunc mocks the Ack method.
	AckFunc func()

	// CloneFunc mocks the Clone method.
	CloneFunc func(ctx context.Context) (Event, error)

	// ContextFunc mocks the Context method.
	ContextFunc func() context.Context

	// CreatedFunc mocks the Created method.
	CreatedFunc func() time.Time

	// DeepCopyFunc mocks the DeepCopy method.
	DeepCopyFunc func() error

	// EvaluateFunc mocks the Evaluate method.
	EvaluateFunc func(expression interface{}) (interface{}, interface{}, string)

	// GetPathValueFunc mocks the GetPathValue method.
	GetPathValueFunc func(path string) (interface{}, interface{}, string)

	// IdFunc mocks the Id method.
	IdFunc func() string

	// MetadataFunc mocks the Metadata method.
	MetadataFunc func() map[string]interface{}

	// NackFunc mocks the Nack method.
	NackFunc func(err error)

	// PayloadFunc mocks the Payload method.
	PayloadFunc func() interface{}

	// SetContextFunc mocks the SetContext method.
	SetContextFunc func(ctx context.Context) error

	// SetMetadataFunc mocks the SetMetadata method.
	SetMetadataFunc func(metadata map[string]interface{}) error

	// SetPathValueFunc mocks the SetPathValue method.
	SetPathValueFunc func(path string, val interface{}, createPath bool) (interface{}, string, error)

	// SetPayloadFunc mocks the SetPayload method.
	SetPayloadFunc func(payload interface{}) error

	// TenantFunc mocks the Tenant method.
	TenantFunc func() tenant.Id
	// contains filtered or unexported fields
}

EventMock is a mock implementation of Event.

func TestSomethingThatUsesEvent(t *testing.T) {

	// make and configure a mocked Event
	mockedEvent := &EventMock{
		AckFunc: func()  {
			panic("mock out the Ack method")
		},
		CloneFunc: func(ctx context.Context) (Event, error) {
			panic("mock out the Clone method")
		},
		ContextFunc: func() context.Context {
			panic("mock out the Context method")
		},
		CreatedFunc: func() time.Time {
			panic("mock out the Created method")
		},
		DeepCopyFunc: func() error {
			panic("mock out the DeepCopy method")
		},
		EvaluateFunc: func(expression interface{}) (interface{}, interface{}, string) {
			panic("mock out the Evaluate method")
		},
		GetPathValueFunc: func(path string) (interface{}, interface{}, string) {
			panic("mock out the GetPathValue method")
		},
		IdFunc: func() string {
			panic("mock out the Id method")
		},
		MetadataFunc: func() map[string]interface{} {
			panic("mock out the Metadata method")
		},
		NackFunc: func(err error)  {
			panic("mock out the Nack method")
		},
		PayloadFunc: func() interface{} {
			panic("mock out the Payload method")
		},
		SetContextFunc: func(ctx context.Context) error {
			panic("mock out the SetContext method")
		},
		SetMetadataFunc: func(metadata map[string]interface{}) error {
			panic("mock out the SetMetadata method")
		},
		SetPathValueFunc: func(path string, val interface{}, createPath bool) (interface{}, string, error) {
			panic("mock out the SetPathValue method")
		},
		SetPayloadFunc: func(payload interface{}) error {
			panic("mock out the SetPayload method")
		},
		TenantFunc: func() tenant.Id {
			panic("mock out the Tenant method")
		},
	}

	// use mockedEvent in code that requires Event
	// and then make assertions.

}

func (*EventMock) Ack

func (mock *EventMock) Ack()

Ack calls AckFunc.

func (*EventMock) AckCalls

func (mock *EventMock) AckCalls() []struct {
}

AckCalls gets all the calls that were made to Ack. Check the length with:

len(mockedEvent.AckCalls())

func (*EventMock) Clone

func (mock *EventMock) Clone(ctx context.Context) (Event, error)

Clone calls CloneFunc.

func (*EventMock) CloneCalls

func (mock *EventMock) CloneCalls() []struct {
	Ctx context.Context
}

CloneCalls gets all the calls that were made to Clone. Check the length with:

len(mockedEvent.CloneCalls())

func (*EventMock) Context

func (mock *EventMock) Context() context.Context

Context calls ContextFunc.

func (*EventMock) ContextCalls

func (mock *EventMock) ContextCalls() []struct {
}

ContextCalls gets all the calls that were made to Context. Check the length with:

len(mockedEvent.ContextCalls())

func (*EventMock) Created added in v0.4.0

func (mock *EventMock) Created() time.Time

Created calls CreatedFunc.

func (*EventMock) CreatedCalls added in v0.4.0

func (mock *EventMock) CreatedCalls() []struct {
}

CreatedCalls gets all the calls that were made to Created. Check the length with:

len(mockedEvent.CreatedCalls())

func (*EventMock) DeepCopy added in v1.1.0

func (mock *EventMock) DeepCopy() error

DeepCopy calls DeepCopyFunc.

func (*EventMock) DeepCopyCalls added in v1.1.0

func (mock *EventMock) DeepCopyCalls() []struct {
}

DeepCopyCalls gets all the calls that were made to DeepCopy. Check the length with:

len(mockedEvent.DeepCopyCalls())

func (*EventMock) Evaluate added in v1.1.0

func (mock *EventMock) Evaluate(expression interface{}) (interface{}, interface{}, string)

Evaluate calls EvaluateFunc.

func (*EventMock) EvaluateCalls added in v1.1.0

func (mock *EventMock) EvaluateCalls() []struct {
	Expression interface{}
}

EvaluateCalls gets all the calls that were made to Evaluate. Check the length with:

len(mockedEvent.EvaluateCalls())

func (*EventMock) GetPathValue

func (mock *EventMock) GetPathValue(path string) (interface{}, interface{}, string)

GetPathValue calls GetPathValueFunc.

func (*EventMock) GetPathValueCalls

func (mock *EventMock) GetPathValueCalls() []struct {
	Path string
}

GetPathValueCalls gets all the calls that were made to GetPathValue. Check the length with:

len(mockedEvent.GetPathValueCalls())

func (*EventMock) Id added in v0.9.0

func (mock *EventMock) Id() string

Id calls IdFunc.

func (*EventMock) IdCalls added in v0.9.0

func (mock *EventMock) IdCalls() []struct {
}

IdCalls gets all the calls that were made to Id. Check the length with:

len(mockedEvent.IdCalls())

func (*EventMock) Metadata

func (mock *EventMock) Metadata() map[string]interface{}

Metadata calls MetadataFunc.

func (*EventMock) MetadataCalls

func (mock *EventMock) MetadataCalls() []struct {
}

MetadataCalls gets all the calls that were made to Metadata. Check the length with:

len(mockedEvent.MetadataCalls())

func (*EventMock) Nack

func (mock *EventMock) Nack(err error)

Nack calls NackFunc.

func (*EventMock) NackCalls

func (mock *EventMock) NackCalls() []struct {
	Err error
}

NackCalls gets all the calls that were made to Nack. Check the length with:

len(mockedEvent.NackCalls())

func (*EventMock) Payload

func (mock *EventMock) Payload() interface{}

Payload calls PayloadFunc.

func (*EventMock) PayloadCalls

func (mock *EventMock) PayloadCalls() []struct {
}

PayloadCalls gets all the calls that were made to Payload. Check the length with:

len(mockedEvent.PayloadCalls())

func (*EventMock) SetContext

func (mock *EventMock) SetContext(ctx context.Context) error

SetContext calls SetContextFunc.

func (*EventMock) SetContextCalls

func (mock *EventMock) SetContextCalls() []struct {
	Ctx context.Context
}

SetContextCalls gets all the calls that were made to SetContext. Check the length with:

len(mockedEvent.SetContextCalls())

func (*EventMock) SetMetadata

func (mock *EventMock) SetMetadata(metadata map[string]interface{}) error

SetMetadata calls SetMetadataFunc.

func (*EventMock) SetMetadataCalls

func (mock *EventMock) SetMetadataCalls() []struct {
	Metadata map[string]interface{}
}

SetMetadataCalls gets all the calls that were made to SetMetadata. Check the length with:

len(mockedEvent.SetMetadataCalls())

func (*EventMock) SetPathValue

func (mock *EventMock) SetPathValue(path string, val interface{}, createPath bool) (interface{}, string, error)

SetPathValue calls SetPathValueFunc.

func (*EventMock) SetPathValueCalls

func (mock *EventMock) SetPathValueCalls() []struct {
	Path       string
	Val        interface{}
	CreatePath bool
}

SetPathValueCalls gets all the calls that were made to SetPathValue. Check the length with:

len(mockedEvent.SetPathValueCalls())

func (*EventMock) SetPayload

func (mock *EventMock) SetPayload(payload interface{}) error

SetPayload calls SetPayloadFunc.

func (*EventMock) SetPayloadCalls

func (mock *EventMock) SetPayloadCalls() []struct {
	Payload interface{}
}

SetPayloadCalls gets all the calls that were made to SetPayload. Check the length with:

len(mockedEvent.SetPayloadCalls())

func (*EventMock) Tenant added in v0.4.0

func (mock *EventMock) Tenant() tenant.Id

Tenant calls TenantFunc.

func (*EventMock) TenantCalls added in v0.4.0

func (mock *EventMock) TenantCalls() []struct {
}

TenantCalls gets all the calls that were made to Tenant. Check the length with:

len(mockedEvent.TenantCalls())

type EventOption

type EventOption func(*event) error

func FailOnAck

func FailOnAck(t *testing.T) EventOption

func FailOnNack

func FailOnNack(t *testing.T) EventOption

func WithAck

func WithAck(handledFn func(Event), errFn func(Event, error)) EventOption

event acknowledge option with two completion functions, handledFn and errFn. An event with acknowledgement option will be notified through the handledFn when an event is handled, or through the errFn when there is an error handling it. An event is considered handled when it and all its child events (derived from the Clone function) have called the Ack function. An event is considered to have an error if it or any of its child events (derived from the Clone function) has called the Nack function. An event can also error out if it does not receive all the acknowledgements before the context timeout/cancellation.

func WithId added in v0.9.0

func WithId(eid string) EventOption

func WithMetadata

func WithMetadata(metadata map[string]interface{}) EventOption

func WithMetadataKeyValue added in v0.6.0

func WithMetadataKeyValue(key string, val interface{}) EventOption

func WithOtelTracing added in v0.7.0

func WithOtelTracing(spanName string) EventOption

WithOtelTracing enables opentelemtry tracing for the event

func WithTenant added in v0.4.0

func WithTenant(tid tenant.Id) EventOption

func WithTracePayloadOnNack added in v0.7.0

func WithTracePayloadOnNack(tracePayloadOnNack bool) EventOption

type NoAckHandlersError

type NoAckHandlersError struct {
}

func (*NoAckHandlersError) Error

func (e *NoAckHandlersError) Error() string

Jump to

Keyboard shortcuts

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