commands

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDecoderMissing = errors.New("decoder missing")
	ErrDecoderFailure = errors.New("decoder failure")
)
View Source
var (
	ErrHandlerMissing = errors.New("handler missing")
	ErrInvalidReqType = errors.New("invalid req type")
	ErrInvalidResType = errors.New("invalid res type")
)
View Source
var (
	ErrMappingMissing = errors.New("mapping missing")
)

Functions

func Future added in v0.1.4

func Future[TReq CommandReq[TRes], TRes CommandRes](ctx context.Context, catalog *DefaultHandlerCatalog, req TReq) futures.Future[util.Tuple2[TRes, error]]

Future creates a futures.Future that asynchronously processes a command request.

Type Parameters:

  • TReq: The type of the command request, which must implement the CommandReq interface.
  • TRes: The type of the command response, which must implement the CommandRes interface.

Parameters:

  • ctx: A context.Context providing context for the request processing.
  • catalog: A pointer to the DefaultHandlerCatalog containing the cataloged handlers.
  • req: A TReq representing the command request to be processed.

Returns:

  • A futures.Future containing a util.Tuple2 where:
  • Val1 is the TRes representing the result of the command processing.
  • Val2 is an error if the processing fails.

func Handle added in v0.1.4

func Handle[TReq CommandReq[TRes], TRes CommandRes](ctx context.Context, catalog *DefaultHandlerCatalog, req TReq) (typedRes TRes, err error)

Handle processes a command request using the cataloged handler.

Type Parameters:

  • TReq: The type of the command request, which must implement the CommandReq interface.
  • TRes: The type of the command response, which must implement the CommandRes interface.

Parameters:

  • ctx: A context.Context providing context for the request processing.
  • catalog: A pointer to the DefaultHandlerCatalog containing the cataloged handlers.
  • req: A TReq representing the command request to be processed.

Returns:

  • res: A TRes representing the result of the command processing.
  • err: An error if the request type does not match the expected type or if the handler fails.

func InsertDecoder

func InsertDecoder[TReq CommandReq[CommandRes]](catalog DecoderCatalog, decoder Decoder)

InsertDecoder is a generic function that catalogs a decoder for a specific command request type.

Parameters:

  • catalog: A pointer to the DecoderCatalog where the decoder will be cataloged.
  • reqName: The name of the request type to catalog.
  • decoder: A Decoder function that decodes serialized data into the specified command request type.

func InsertHandler

func InsertHandler[TReq CommandReq[TRes], TRes CommandRes](catalog *DefaultHandlerCatalog, factory HandlerFactory[TReq, TRes])

InsertHandler is a generic function that catalogs a handler for a specific command request type.

Type Parameters:

  • TReq: The type of the command request, which must implement the CommandReq interface.
  • TRes: The type of the command response, which must implement the CommandRes interface.

Parameters:

  • catalog: A pointer to the DefaultHandlerCatalog where the handler will be cataloged.
  • factory: A HandlerFactory function that creates a new instance of a Handler for the specified request and response types.

func InsertMapping

func InsertMapping[TReq CommandReq[CommandRes]](catalog *DefaultMappingCatalog, reqName string)

InsertMapping catalogs a mapping between a request name and its corresponding type.

Type Parameters:

  • TReq: The type of the command request, which must implement the CommandReq interface.

Parameters:

  • catalog: A pointer to the DefaultMappingCatalog where the mapping will be cataloged.
  • reqName: A string representing the name of the request.

Types

type CommandReq

type CommandReq[TRes CommandRes] any

CommandReq is a generic interface representing a request for a command. It is parameterized by TRes, which must implement the CommandRes interface.

type CommandRes

type CommandRes any

CommandRes is an interface that represents the result of a command. It can be implemented by any type that represents the output of a command.

type Decoder

type Decoder func([]byte) (CommandReq[CommandRes], error)

Decoder is a function type that takes a byte slice as input and returns a CommandReq[CommandRes] and an error. It is used to decode serialized command request data into a specific command request type.

func DefaultDecoder

func DefaultDecoder[TReq CommandReq[CommandRes]]() Decoder

DefaultDecoder returns a Decoder function for decoding serialized command request data into a specific command request type. The generic type TReq must implement the CommandReq[CommandRes] interface.

The returned decoder function takes a byte slice as input, attempts to unmarshal it into the specified TReq type, and returns the decoded command request or an error if unmarshalling fails.

type DecoderCatalog

type DecoderCatalog interface {
	Insert(reqType reflect.Type, decoder Decoder)
	Decode(reqType reflect.Type, reqJSON []byte) (CommandReq[CommandRes], error)
}

type DefaultDecoderCatalog added in v0.4.0

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

DecoderCatalog is a catalog for managing nameMappings between request names, their corresponding types, and decoders. It allows decoding serialized command request data into specific command request types.

Fields:

  • nameMappings: A map that associates request names (strings) with their corresponding reflect.Type.
  • decoders: A map that associates reflect.Type with functions that decode serialized data into CommandReq[CommandRes].

func NewDefaultDecoderCatalog added in v0.4.0

func NewDefaultDecoderCatalog(options ...NewDefaultDecoderCatalogOption) (catalog *DefaultDecoderCatalog)

NewDefaultDecoderCatalog creates and returns a new instance of DecoderCatalog. The catalog is initialized with an empty map for decoders, which associates reflect.Type with functions that decode serialized data into CommandReq[CommandRes].

func (*DefaultDecoderCatalog) Decode added in v0.4.0

func (d *DefaultDecoderCatalog) Decode(reqType reflect.Type, reqJSON []byte) (req CommandReq[CommandRes], err error)

Decode attempts to decode serialized command request data into a specific command request type.

Parameters:

  • reqName: The name of the request type to decode.
  • reqJSON: A byte slice containing the serialized command request data.

Returns:

  • A CommandReq[CommandRes] representing the decoded command request.
  • An error if the decoding fails or if no decoder is cataloged for the given request name.

func (*DefaultDecoderCatalog) Insert added in v0.4.0

func (d *DefaultDecoderCatalog) Insert(reqType reflect.Type, decoder Decoder)

Insert catalogs a decoder for a specific command request type.

Parameters:

  • reqName: The name of the request type to catalog.
  • reqType: The reflect.Type of the request type.
  • decoder: A Decoder function that decodes serialized data into the specified command request type.

type DefaultHandlerAdapter

type DefaultHandlerAdapter[TReq CommandReq[TRes], TRes CommandRes] struct {
	// contains filtered or unexported fields
}

DefaultHandlerAdapter is a generic adapter for handling commands.

Type Parameters:

  • TReq: The type of the command request, which must implement the CommandReq interface.
  • TRes: The type of the command response, which must implement the CommandRes interface.

Fields:

  • handler: An instance of the Handler that processes the command request.
  • handlerFactory: A factory function that creates a new instance of the Handler.

func NewDefaultHandlerAdapter

func NewDefaultHandlerAdapter[TReq CommandReq[TRes], TRes CommandRes](factory func() Handler[TReq, TRes]) *DefaultHandlerAdapter[TReq, TRes]

NewDefaultHandlerAdapter creates a new instance of DefaultHandlerAdapter.

Type Parameters:

  • TReq: The type of the command request, which must implement the CommandReq interface.
  • TRes: The type of the command response, which must implement the CommandRes interface.

Parameters:

  • factory: A function that creates a new instance of a Handler for the specified request and response types.

Returns:

  • A pointer to a DefaultHandlerAdapter instance, initialized with the provided factory function.

func (*DefaultHandlerAdapter[TReq, TRes]) Handle

func (a *DefaultHandlerAdapter[TReq, TRes]) Handle(ctx context.Context, req CommandReq[CommandRes]) (res CommandRes, err error)

Handle processes the given request (req) within the provided context (ctx).

Type Parameters:

  • TReq: The type of the command request, which must implement the CommandReq interface.
  • TRes: The type of the command response, which must implement the CommandRes interface.

Parameters:

  • ctx: A context.Context providing context for the request processing.
  • req: A CommandReq[CommandRes] representing the command request to be processed.

Returns:

  • res: A CommandRes representing the result of the command processing.
  • err: An error if the request type does not match the expected type or if the handler fails.

func (*DefaultHandlerAdapter[TReq, TRes]) ReqType

func (a *DefaultHandlerAdapter[TReq, TRes]) ReqType() reflect.Type

ReqType returns the reflect.Type of the request handled by the adapter.

Type Parameters:

  • TReq: The type of the command request, which must implement the CommandReq interface.

Returns:

  • A reflect.Type representing the type of the request handled by the adapter.

func (*DefaultHandlerAdapter[TReq, TRes]) ResType

func (a *DefaultHandlerAdapter[TReq, TRes]) ResType() reflect.Type

ResType returns the reflect.Type of the response produced by the adapter.

Type Parameters:

  • TRes: The type of the command response, which must implement the CommandRes interface.

Returns:

  • A reflect.Type representing the type of the response produced by the adapter.

type DefaultHandlerCatalog added in v0.4.0

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

DefaultHandlerCatalog is a catalog for managing nameMappings between request types and their corresponding handler adapters.

Fields:

  • adapters: A map that associates reflect.Type with HandlerAdapter instances, enabling the handling of specific request types.

func NewDefaultHandlerCatalog added in v0.4.0

func NewDefaultHandlerCatalog(options ...NewDefaultHandlerCatalogOption) *DefaultHandlerCatalog

NewDefaultHandlerCatalog creates and returns a new instance of DefaultHandlerCatalog.

The catalog is initialized with an empty map for adapters, which associates reflect.Type with HandlerAdapter instances, enabling the handling of specific request types.

Returns:

  • A pointer to a DefaultHandlerCatalog instance.

func (*DefaultHandlerCatalog) Future added in v0.4.0

Future creates a futures.Future that asynchronously processes a command request.

Parameters:

  • ctx: A context.Context providing context for the request processing.
  • req: A CommandReq[CommandRes] representing the command request to be processed.

Returns:

  • A futures.Future containing a util.Tuple2 where:
  • Val1 is the CommandRes representing the result of the command processing.
  • Val2 is an error if the processing fails.

func (*DefaultHandlerCatalog) Handle added in v0.4.0

func (r *DefaultHandlerCatalog) Handle(ctx context.Context, req CommandReq[CommandRes]) (res CommandRes, err error)

Handle processes a command request using the cataloged handler.

Parameters:

  • req: A CommandReq[CommandRes] representing the command request to be processed.
  • ctx: A context.Context providing context for the request processing.

Returns:

  • res: A CommandRes representing the result of the command processing.
  • err: An error if no handler is cataloged for the request type or if the handler fails.

func (*DefaultHandlerCatalog) Insert added in v0.4.0

func (r *DefaultHandlerCatalog) Insert(adapter HandlerAdapter)

Insert adds a HandlerAdapter to the DefaultHandlerCatalog.

Parameters:

  • adapter: The HandlerAdapter instance to catalog.

func (*DefaultHandlerCatalog) TypeMap added in v0.4.0

func (r *DefaultHandlerCatalog) TypeMap() (typeMap map[reflect.Type]reflect.Type)

TypeMap returns a mapping of request types to their corresponding response types.

The method iterates over the cataloged adapters in the DefaultHandlerCatalog and constructs a map where the keys are the request types (reflect.Type) and the values are the response types (reflect.Type) produced by the adapters.

Returns:

  • typeMap: A map associating request types with their corresponding response types.

type DefaultMappingCatalog added in v0.4.0

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

DefaultMappingCatalog is a catalog for managing mappings between request names and types.

Fields:

  • mutex: A sync.RWMutex used to ensure thread-safe access to the catalog.
  • nameMappings: A map that associates request names (strings) with their corresponding reflect.Type.
  • typeMappings: A map that associates reflect.Type with their corresponding request names (strings).

func NewMappingCatalog

func NewMappingCatalog(options ...NewMappingCatalogOption) (catalog *DefaultMappingCatalog)

NewMappingCatalog creates and returns a new instance of DefaultMappingCatalog.

The catalog is initialized with:

  • A sync.RWMutex for thread-safe access.
  • nameMappings: A map associating request names (strings) with their corresponding reflect.Type.
  • typeMappings: A map associating reflect.Type with their corresponding request names (strings).

Returns:

  • A pointer to a DefaultMappingCatalog instance.

func (*DefaultMappingCatalog) ByName added in v0.4.0

func (m *DefaultMappingCatalog) ByName(reqName string) (reqType reflect.Type, err error)

ByName retrieves the reflect.Type associated with the given request name (reqName).

Parameters:

  • reqName: A string representing the name of the request.

Returns:

  • reqType: The reflect.Type associated with the given request name.
  • err: An error if no mapping is cataloged for the given request name.

func (*DefaultMappingCatalog) ByType added in v0.4.0

func (m *DefaultMappingCatalog) ByType(reqType reflect.Type) (reqName string, err error)

ByType retrieves the request name associated with the given request type (reqType).

Parameters:

  • reqType: A reflect.Type representing the type of the request.

Returns:

  • reqName: A string representing the name of the request associated with the given type.
  • err: An error if no mapping is cataloged for the given request type.

func (*DefaultMappingCatalog) Insert added in v0.4.0

func (m *DefaultMappingCatalog) Insert(reqName string, reqType reflect.Type)

Insert adds a mapping between a request name and its corresponding type.

Parameters:

  • reqName: A string representing the name of the request.
  • reqType: A reflect.Type representing the type of the request.

type Handler

type Handler[TReq CommandReq[TRes], TRes CommandRes] interface {
	Handle(ctx context.Context, req TReq) (res TRes, err error)
}

Handler is a generic interface for handling commands.

Type Parameters:

  • TReq: The type of the command request, which must implement the CommandReq interface.
  • TRes: The type of the command response, which must implement the CommandRes interface.

Methods:

  • Handle(req TReq, ctx context.Context) (res TRes, err error): Processes the given command request (req) within the provided context (ctx). Returns the command response (res) and an error (err) if the handling fails.

type HandlerAdapter

type HandlerAdapter interface {
	ReqType() reflect.Type
	ResType() reflect.Type
	Handle(ctx context.Context, req CommandReq[CommandRes]) (res CommandRes, err error)
}

HandlerAdapter is an interface for adapting handlers to a common structure.

Methods:

  • ReqType(): Returns the reflect.Type of the request handled by the adapter.
  • ResType(): Returns the reflect.Type of the response produced by the adapter.
  • Handle(req CommandReq[CommandRes], ctx context.Context): Processes the given request (req) within the provided context (ctx), returning the response (res) or an error (err) if the handling fails.

type HandlerCatalog

type HandlerCatalog interface {
	Insert(adapter HandlerAdapter)
	Handle(ctx context.Context, req CommandReq[CommandRes]) (res CommandRes, err error)
	Future(ctx context.Context, req CommandReq[CommandRes]) futures.Future[util.Tuple2[CommandRes, error]]
	TypeMap() map[reflect.Type]reflect.Type
}

type HandlerFactory

type HandlerFactory[TReq CommandReq[TRes], TRes CommandRes] func() Handler[TReq, TRes]

HandlerFactory is a type alias for a function that creates a new instance of a Handler.

Type Parameters:

  • TReq: The type of the command request, which must implement the CommandReq interface.
  • TRes: The type of the command response, which must implement the CommandRes interface.

Returns:

  • A Handler instance capable of processing the specified request and producing the corresponding response.

type MappingCatalog

type MappingCatalog interface {
	Insert(reqName string, reqType reflect.Type)
	ByName(reqName string) (reqType reflect.Type, err error)
	ByType(reqType reflect.Type) (reqName string, err error)
}

type NewDefaultDecoderCatalogOption added in v0.4.0

type NewDefaultDecoderCatalogOption = util.Option[*DefaultDecoderCatalog]

type NewDefaultHandlerCatalogOption added in v0.4.0

type NewDefaultHandlerCatalogOption = util.Option[*DefaultHandlerCatalog]

type NewMappingCatalogOption

type NewMappingCatalogOption = util.Option[*DefaultMappingCatalog]

Jump to

Keyboard shortcuts

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