gobus

package module
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2024 License: MIT Imports: 6 Imported by: 0

README

GO CommandBus

A simple and easy to plug-in command bus for Go.

Install

Use go get.

$ go get github.com/assurrussa/gobus@v0.9.0

Then import the package into your own code:

import "github.com/assurrussa/gobus"

Usage

package main

import (
	"context"
	"log"

	"github.com/assurrussa/gobus"
)

type User struct {
	Name string
}

type GetUserHandler struct{}

func (h *GetUserHandler) Execute(_ context.Context, dto *User) (int, error) {
	log.Printf("user %s getting", dto.Name)

	return len(dto.Name), nil
}

type CreateUserHandler struct{}

func (h *CreateUserHandler) Execute(_ context.Context, dto *User) error {
	log.Printf("user %s created", dto.Name)

	return nil
}

func main() {
	ctx := context.Background()

	gobus.RegisterResult[*User, int](&GetUserHandler{})
	gobus.Register[*User](&CreateUserHandler{})

	// action sync.
	result, err := gobus.DispatchResult[*User, int](ctx, &User{"gobus test"})
	log.Printf("dispatch resulting command - result: %v, err: %v", result, err)

	err = gobus.Dispatch[*User](ctx, &User{"gobus test"})
	log.Printf("dispatch command - err: %v", err)

	// action async.
	out := <-gobus.DispatchResultAsync[*User, int](ctx, &User{"gobus test async"})
	log.Printf("dispatch resulting command async - result: %+v", out)

	err = <-gobus.DispatchAsync[*User](ctx, &User{"gobus test async"})
	log.Printf("dispatch command async - %v", err)
}

License

This project is released under the MIT licence. See LICENSE for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dispatch

func Dispatch[Q ObjectIn](ctx context.Context, dto Q) error

func DispatchAsync

func DispatchAsync[Q ObjectIn](ctx context.Context, dto Q) <-chan error

func DispatchResult

func DispatchResult[Q ObjectIn, T ObjectOut](ctx context.Context, dto Q) (T, error)

func DispatchResultAsync

func DispatchResultAsync[Q ObjectIn, T ObjectOut](ctx context.Context, dto Q) <-chan Envelope[T]

func InitCommand

func InitCommand()

func InitResultCommand

func InitResultCommand()

func Register

func Register[Q ObjectIn](handler CommandExecutor[Q])

func RegisterResult

func RegisterResult[Q ObjectIn, T ObjectOut](handler ResultCommandExecutor[Q, T])

Types

type CommandExecutor

type CommandExecutor[Q ObjectIn] interface {
	Execute(ctx context.Context, dto Q) error
}

type Envelope

type Envelope[T ObjectOut] struct {
	Result T
	Error  error
}

type ObjectIn

type ObjectIn interface {
}

type ObjectOut

type ObjectOut interface {
}

type ResultCommandExecutor

type ResultCommandExecutor[Q ObjectIn, T ObjectOut] interface {
	Execute(ctx context.Context, dto Q) (T, error)
}

Directories

Path Synopsis
internal
example/cmd command
mocks
Package mocksgobus is a generated GoMock package.
Package mocksgobus is a generated GoMock package.
performance/buscontract
Package bus is a generated GoMock package.
Package bus is a generated GoMock package.
performance/busevent
Package busevent is a generated GoMock package.
Package busevent is a generated GoMock package.

Jump to

Keyboard shortcuts

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