goeh

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2021 License: MIT Imports: 3 Imported by: 11

README

go-eh

An event handling library in Go

Installation

go get github.com/hetacode/go-eh

Example event implementation:
type UserEvent struct {
  *goeh.EventData
  FirstName string `json:"first_name"`
}

func (e *UserEvent) GetType() string {
  return "UserEvent"
}

This event should be register in the EventsMapper:

eventsMapper := new(goeh.EventsMapper)
eventsMapper.Register(new(UserEvent))

Now, we can resolve event instance by json string:

json := `{
  "id": "1",
  "type": "UserEvent",
  "first_name": "John"
}`

event, err := eventsMapper.Resolve(json)
if err != nil {
	t.Fatal(err)
}
  
trueEvent := event.(*UserEvent)
// trueEvent.FirstName == "John"
EventHandler part

Event handler struct should implement an EventHandler interface with Handle function.

type UserEventHandler struct {
}

// Execute message
func (e *UserEventHandler) Handle(event goeh.Event) {
  e := event.(*UserEvent)
	/// some magic tricks here, processing and more...
}

EventHandler and his Event should be register in EventsHandlerManager:

manager := goeh.NewEventsHandlerManager()
if err := manager.Register(new(UserEvent), new(UserEventHandler); err != nil {
  t.Fatal(err)
}

In order to Execute appropriate Event, just call Execute function in EventsHandlerManager:

if err := manager.Execute(event); err != nil {
	t.Fatal(err)
}

And the same with EventsMapper together:

json := `{
  "id": "1",
  "type": "UserEvent",
  "first_name": "John"
}`

event, err := eventsMapper.Resolve(json)
if err != nil {
	t.Fatal(err)
}

if err := manager.Execute(event); err != nil {
	t.Fatal(err)
}

That's all!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event interface {
	GetID() string
	GetCorrelationID() string
	GetType() string
	GetPayload() string
	LoadPayload() error
	SavePayload(event Event) error
}

Event abstraction

type EventData

type EventData struct {
	Event
	ID            string `json:"id"`
	CorrelationID string `json:"corr_id"`
	Type          string `json:"type"`
	Payload       string `json:"payload"`
}

EventData basic event structure which is use as base of all events

func (*EventData) GetCorrelationID added in v0.0.3

func (m *EventData) GetCorrelationID() string

GetCorrelationID of event

func (*EventData) GetID added in v0.0.2

func (m *EventData) GetID() string

GetID of event

func (*EventData) GetPayload

func (m *EventData) GetPayload() string

GetPayload of event

func (*EventData) GetType

func (m *EventData) GetType() string

GetType of event

func (*EventData) LoadPayload

func (m *EventData) LoadPayload() error

LoadPayload : fetch event type from payload

func (*EventData) SavePayload

func (m *EventData) SavePayload(event Event) error

SavePayload save event structure to payload json string

type EventHandler

type EventHandler interface {
	Handle(event Event)
}

EventHandler an abstraction to processing specific event

type EventsHandlerManager

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

EventsHandlerManager keep all bindings event <-> eventHandler

func NewEventsHandlerManager

func NewEventsHandlerManager() *EventsHandlerManager

NewEventsHandlerManager create new instance

func (*EventsHandlerManager) Execute

func (h *EventsHandlerManager) Execute(event Event) error

Execute : find eventhandler and then execute an event

func (*EventsHandlerManager) Register

func (h *EventsHandlerManager) Register(event Event, handler EventHandler) error

Register commandHandler event - should by instacne of Event handler - just EventHandler

type EventsMapper

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

EventsMapper keep all registered events with their keys

func (*EventsMapper) Register

func (m *EventsMapper) Register(event Event)

Register an event as pair string - event

func (*EventsMapper) Resolve

func (m *EventsMapper) Resolve(jsonData string) (Event, error)

Resolve and find event by json data

Jump to

Keyboard shortcuts

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