core

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2026 License: MPL-2.0 Imports: 4 Imported by: 0

Documentation

Overview

core/block.go

core/container.go

core/errors.go

core/get.go

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBlockNotFound is returned when Get cannot find a registered block by name.
	ErrBlockNotFound = errors.New("go-code-blocks: block not found")

	// ErrNotInitialized is returned when a block operation is called before Init.
	ErrNotInitialized = errors.New("go-code-blocks: block not initialized; call Init first")

	// ErrItemNotFound is returned when a requested item does not exist.
	ErrItemNotFound = errors.New("go-code-blocks: item not found")

	// ErrAlreadyRegistered is returned when registering a duplicate block name.
	ErrAlreadyRegistered = errors.New("go-code-blocks: block already registered")
)

Functions

func Get

func Get[B Block](c *Container, name string) (B, error)

Get retrieves a registered block by name and asserts it to type B. Returns ErrBlockNotFound when the name is unknown, or a type mismatch error when the block exists but its concrete type does not match B.

This is the preferred retrieval API when the concrete block type is known at the call site, as it avoids a manual type assertion in application code.

users, err := core.Get[*dynamodb.Block[User]](app, "users")
if err != nil { … }
users.PutItem(ctx, u)

Types

type Block

type Block interface {
	// Name returns the unique identifier used to register and retrieve this block.
	Name() string
	// Init establishes connections, validates configuration, and readies the block.
	// It is called once during application startup.
	Init(ctx context.Context) error
	// Shutdown releases all resources held by the block.
	// It is called in reverse-registration order during graceful shutdown.
	Shutdown(ctx context.Context) error
}

Block defines the lifecycle contract that every integration block must fulfill. Blocks are self-contained units that encapsulate a single external dependency (DynamoDB, S3, Redis, etc.) and expose a typed API over it.

type Container

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

Container manages the registration and lifecycle of all blocks. Blocks are initialized in registration order and shut down in reverse order, following standard dependency teardown conventions.

func NewContainer

func NewContainer() *Container

NewContainer creates a new, empty Container.

func (*Container) Get

func (c *Container) Get(name string) (Block, error)

Get retrieves a registered block by name. Returns ErrBlockNotFound if no block with that name exists.

func (*Container) InitAll

func (c *Container) InitAll(ctx context.Context) error

InitAll initializes all registered blocks in registration order. Stops and returns on the first error.

func (*Container) MustRegister

func (c *Container) MustRegister(b Block)

MustRegister is like Register but panics on error. Useful for wiring blocks at startup where an error is unrecoverable.

func (*Container) Register

func (c *Container) Register(b Block) error

Register adds a block to the container. Returns ErrAlreadyRegistered if a block with the same name exists.

func (*Container) ShutdownAll

func (c *Container) ShutdownAll(ctx context.Context) error

ShutdownAll shuts down all registered blocks in reverse order, collecting all errors and joining them so partial failures are visible.

Jump to

Keyboard shortcuts

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