bean

package
v0.1.0-preview.4 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package bean provides typed, reflection-free dependency handles used by generated Spice applications.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RequestScopeMiddleware

func RequestScopeMiddleware(
	next http.Handler,
	onError ScopeErrorHandler,
) (http.Handler, error)

RequestScopeMiddleware creates and closes one scope per HTTP request.

func WithScope

func WithScope(ctx context.Context, scope *Scope) (context.Context, error)

WithScope attaches a matching explicit scope to ctx.

Types

type AcquireFunc

type AcquireFunc[T any] func(
	context.Context,
) (T, lifecycle.Cleanup, error)

AcquireFunc constructs or leases one typed bean.

type Lazy

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

Lazy resolves one typed value at most once. The first result, including an error, is stable for the lifetime of the owning generated scope.

func NewLazy

func NewLazy[T any](
	resolve func(context.Context) (T, error),
) Lazy[T]

NewLazy constructs an isolated lazy dependency.

func (*Lazy[T]) Get

func (lazy *Lazy[T]) Get(ctx context.Context) (T, error)

Get resolves and returns the value. A nil context or resolver fails closed.

type Optional

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

Optional contains either one unambiguous typed bean or no bean. Ambiguity is rejected by the compiler and can never be represented at runtime.

func None

func None[T any]() Optional[T]

None constructs an absent optional value.

func Some

func Some[T any](value T) Optional[T]

Some constructs a present optional value.

func (Optional[T]) Get

func (optional Optional[T]) Get() (T, bool)

Get returns the value and whether it is present.

type Override

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

Override is an explicit typed replacement for one generated singleton bean. Its zero value is disabled, so production ApplicationOptions remain concise.

func Replace

func Replace[T any](value T) Override[T]

Replace returns an enabled override for one caller-owned value.

func ReplaceFactory

func ReplaceFactory[T any](factory OverrideFactory[T]) Override[T]

ReplaceFactory returns an enabled override with caller-owned construction and cleanup. A nil factory fails when the generated application is constructed.

func (Override[T]) Acquire

func (override Override[T]) Acquire(
	ctx context.Context,
) (T, lifecycle.Cleanup, error)

Acquire constructs the replacement. Generated code calls it only when Enabled returns true.

func (Override[T]) Enabled

func (override Override[T]) Enabled() bool

Enabled reports whether generated construction should use this replacement.

type OverrideFactory

type OverrideFactory[T any] func(
	context.Context,
) (T, lifecycle.Cleanup, error)

OverrideFactory constructs one test- or embedding-owned replacement bean. Generated applications register returned cleanup in the original bean's module lifecycle and never place the value in a runtime container.

type Provider

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

Provider is an explicit typed acquisition handle. It contains no global registry and performs no reflection or string lookup.

func NewProvider

func NewProvider[T any](acquire AcquireFunc[T]) Provider[T]

NewProvider constructs a typed provider handle.

func (Provider[T]) Acquire

func (provider Provider[T]) Acquire(
	ctx context.Context,
) (T, lifecycle.Cleanup, error)

Acquire obtains one value and an idempotent caller-owned cleanup lease.

type Scope

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

Scope owns cleanup leases in reverse construction order.

func NewScope

func NewScope(kind ScopeKind) (*Scope, error)

NewScope constructs an isolated request or session scope.

func (*Scope) Close

func (scope *Scope) Close(ctx context.Context) error

Close is idempotent and releases owned values in reverse order.

func (*Scope) Kind

func (scope *Scope) Kind() ScopeKind

Kind returns the immutable scope kind.

func (*Scope) Register

func (scope *Scope) Register(cleanup lifecycle.Cleanup) error

Register assigns cleanup ownership to the scope.

type ScopeErrorHandler

type ScopeErrorHandler func(*http.Request, error)

ScopeErrorHandler observes request-scope cleanup failures.

type ScopeKind

type ScopeKind string

ScopeKind identifies an explicit runtime ownership boundary.

const (
	// ScopeRequest owns values for one HTTP or message request.
	ScopeRequest ScopeKind = "request"
	// ScopeSession owns values for one caller-managed session.
	ScopeSession ScopeKind = "session"
)

type Scoped

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

Scoped is a typed generated-scope factory. Each instance represents exactly one bean declaration, so lookup never uses a runtime bean name.

func NewScoped

func NewScoped[T any](
	kind ScopeKind,
	factory AcquireFunc[T],
) *Scoped[T]

NewScoped constructs a typed request- or session-scoped factory.

func (*Scoped[T]) Provider

func (scoped *Scoped[T]) Provider() Provider[T]

Provider exposes the scoped factory through the standard typed handle.

Jump to

Keyboard shortcuts

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