message

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package message exposes the generic Message type, used to represent a message in a system (e.g. Event, Command, etc.).

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyIterated = errors.New("message.Stream: already iterated")

ErrAlreadyIterated is returned by Stream.Err when Iter is called more than once.

The second and subsequent iterations yield nothing; Err transitions to this sentinel value.

Functions

This section is empty.

Types

type Envelope

type Envelope[T Message] struct {
	Message  T
	Metadata Metadata
}

Envelope bundles a Message to be exchanged with optional Metadata support.

func (Envelope[T]) ToGenericEnvelope

func (e Envelope[T]) ToGenericEnvelope() GenericEnvelope

ToGenericEnvelope maps the Envelope instance into a GenericEnvelope one.

type GenericEnvelope

type GenericEnvelope Envelope[Message]

GenericEnvelope is an Envelope type that can be used when the concrete Message type in the Envelope is not of interest.

type Message

type Message interface {
	Name() string
}

Message is a Message payload.

Each payload should have a unique name identifier, that can be used to uniquely route a message to its type.

type Metadata

type Metadata map[string]string

Metadata contains some data related to a Message that are not functional for the Message itself, but instead functioning as supporting information to provide additional context.

func (Metadata) Merge

func (m Metadata) Merge(other Metadata) Metadata

Merge merges the other Metadata provided in input with the current map. Returns a pointer to the extended metadata map.

func (Metadata) With

func (m Metadata) With(key, value string) Metadata

With returns a new Metadata reference holding the value addressed using the specified key.

type Stream added in v0.4.1

type Stream[T any] struct {
	// contains filtered or unexported fields
}

Stream is a single-use, iterator-backed sequence of values of type T.

Producers are written as callbacks that push values via yield and return a terminal error if the sequence cannot be completed. Consumers iterate via Stream.Iter and check Stream.Err at the end:

for v := range stream.Iter() {
	// handle v
}
if err := stream.Err(); err != nil {
	// handle failure
}

Consumer abandonment (break in the range loop) is NOT a failure; Err returns nil in that case.

Stream is single-use. Calling Stream.Iter more than once yields an empty sequence and sets Stream.Err to ErrAlreadyIterated.

Ctx cancellation is the producer's responsibility: producers should check ctx.Err() at loop boundaries and return it to signal cancellation.

func NewStream added in v0.4.1

func NewStream[T any](produce func(yield func(T) bool) error) *Stream[T]

NewStream returns a Stream backed by the given producer.

The producer is invoked lazily when Stream.Iter is called. It must push each value via yield and respect yield's bool return: if yield returns false, the producer should stop and return nil.

A non-nil error returned by the producer is captured and surfaced via Stream.Err.

func (*Stream[T]) Err added in v0.4.1

func (s *Stream[T]) Err() error

Err returns the terminal error of the stream, or nil if the stream completed without error (including the case of consumer abandonment).

Err is valid at any time; before Stream.Iter is called, Err returns nil.

func (*Stream[T]) Iter added in v0.4.1

func (s *Stream[T]) Iter() iter.Seq[T]

Iter returns an iter.Seq over the stream's values.

Iter is single-use: calling it more than once returns an empty sequence and sets Stream.Err to ErrAlreadyIterated.

Jump to

Keyboard shortcuts

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