Documentation
¶
Index ¶
- Constants
- Variables
- func IfContextNotSetWith[T any](ctx context.Context, key ContextKey, fn func())
- func IfContextSetWith[T any](ctx context.Context, key ContextKey, fn func(value T))
- func IfContextValueEquals[T comparable](ctx context.Context, key ContextKey, expected T, fn func())
- type BrokerController
- type BrokerMessage
- type ContextKey
- type DummyLogger
- type LogInfo
- type Logger
- type Middleware
- type NextMiddleware
Constants ¶
const Prefix = "asyncapi-"
Prefix is the prefix used for all context keys in order to avoid collision with other keys that can be present in context.
Variables ¶
var ( // ErrAsyncAPI is the generic error for AsyncAPI generated code. ErrAsyncAPI = errors.New("error when using AsyncAPI") // ErrContextCanceled is given when a given context is canceled. ErrContextCanceled = fmt.Errorf("%w: context canceled", ErrAsyncAPI) // ErrNilBrokerController is raised when a nil broker controller is user. ErrNilBrokerController = fmt.Errorf("%w: nil broker controller has been used", ErrAsyncAPI) // ErrNilAppSubscriber is raised when a nil app subscriber is user. ErrNilAppSubscriber = fmt.Errorf("%w: nil app subscriber has been used", ErrAsyncAPI) // ErrNilUserSubscriber is raised when a nil user subscriber is user. ErrNilUserSubscriber = fmt.Errorf("%w: nil user subscriber has been used", ErrAsyncAPI) // ErrAlreadySubscribedChannel is raised when a subscription is done twice // or more without unsubscribing. ErrAlreadySubscribedChannel = fmt.Errorf("%w: the channel has already been subscribed", ErrAsyncAPI) // ErrSubscriptionCanceled is raised when expecting something and the subscription has been canceled before it happens. ErrSubscriptionCanceled = fmt.Errorf("%w: the subscription has been canceled", ErrAsyncAPI) )
Functions ¶
func IfContextNotSetWith ¶ added in v0.23.0
func IfContextNotSetWith[T any](ctx context.Context, key ContextKey, fn func())
IfContextNotSetWith executes the function if the key is not set in the context.
func IfContextSetWith ¶
func IfContextSetWith[T any](ctx context.Context, key ContextKey, fn func(value T))
IfContextSetWith executes the function if the key is set in the context.
func IfContextValueEquals ¶
func IfContextValueEquals[T comparable](ctx context.Context, key ContextKey, expected T, fn func())
IfContextValueEquals executes the function if the key is set in the context as a given type and the value is equal to the expected value.
Types ¶
type BrokerController ¶
type BrokerController interface {
// Publish a message to the broker
Publish(ctx context.Context, channel string, mw BrokerMessage) error
// Subscribe to messages from the broker
Subscribe(ctx context.Context, channel string) (messages chan BrokerMessage, cancel chan any, err error)
}
BrokerController represents the functions that should be implemented to connect the broker to the application or the user.
type BrokerMessage ¶
BrokerMessage is a wrapper that will contain all information regarding a message.
func (BrokerMessage) IsUninitialized ¶ added in v0.24.0
func (bm BrokerMessage) IsUninitialized() bool
IsUninitialized check if the BrokerMessage is at zero value, i.e. the uninitialized structure. It can be used to check that a channel is closed.
type ContextKey ¶
type ContextKey string
ContextKey is the type of the keys used in the context.
const ( // ContextKeyIsVersion is the AsyncAPI specification version. ContextKeyIsVersion ContextKey = Prefix + "version" // ContextKeyIsProvider is the name of the provider this data is coming from. // When coming from a generated user, it is `asyncapi`. ContextKeyIsProvider ContextKey = Prefix + "provider" // ContextKeyIsChannel is the name of the channel this data is coming from. ContextKeyIsChannel ContextKey = Prefix + "channel" // ContextKeyIsMessageDirection is the direction this data is coming from. // It can be either "publication" or "reception". ContextKeyIsMessageDirection ContextKey = Prefix + "operation" // ContextKeyIsBrokerMessage is the message that has been sent or received from/to the broker. ContextKeyIsBrokerMessage ContextKey = Prefix + "broker-message" // ContextKeyIsMessage is the message that has been sent or received. ContextKeyIsMessage ContextKey = Prefix + "message" // ContextKeyIsCorrelationID is the correlation ID of the message. ContextKeyIsCorrelationID ContextKey = Prefix + "correlationID" )
func (ContextKey) String ¶
func (k ContextKey) String() string
String returns the string representation of the key.
type DummyLogger ¶
type DummyLogger struct {
}
DummyLogger is a logger that does not log anything.
func (DummyLogger) Error ¶
func (dl DummyLogger) Error(_ context.Context, _ string, _ ...LogInfo)
Error logs error based on a message and key-value elements.
type Logger ¶
type Logger interface {
// Info logs information based on a message and key-value elements
Info(ctx context.Context, msg string, info ...LogInfo)
// Warning logs information based on a message and key-value elements
// This levels indicates a non-expected state but that does not prevent the
// application to work properly
Warning(ctx context.Context, msg string, info ...LogInfo)
// Error logs error based on a message and key-value elements
Error(ctx context.Context, msg string, info ...LogInfo)
}
Logger is the interface that must be implemented by a logger.
type Middleware ¶
type Middleware func(ctx context.Context, next NextMiddleware) context.Context
Middleware is the signature of the function that needs to be implemented to use middleware functionnality
You can call the next middleware during the execution of the defined middleware in order to wrap code execution (for example, to time execution, or recover in case of panic).
type NextMiddleware ¶
NextMiddleware represents the next middleware that can be executed during the previous middleware. If this is already the last middleware, it will execute the appropriate autogenerated code for reception/sending of messages.