ctx

package
v0.38.1 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package ctx provides type-safe context key helpers for Echo handlers.

It wraps Echo's context Set/Get with generics to avoid type assertion boilerplate and provides pre-defined keys used across the framework.

Index

Constants

This section is empty.

Variables

View Source
var (
	SubjectIDKey  = NewKey[string]("subject_id")
	SubjectKey    = NewKey[any]("subject")
	SessionKey    = NewKey[any]("session")
	RequestIDKey  = NewKey[string]("request_id")
	FlashKey      = NewKey[any]("flash")
	TranslatorKey = NewKey[any]("i18n_translator")
	LocaleKey     = NewKey[string]("i18n_locale")
)

Pre-defined keys used across the framework.

Functions

func Get

func Get[T any](c echo.Context, key Key[T]) (T, bool)

Get retrieves a typed value from the Echo context.

A nil context returns (zero, false) rather than panicking. Components render outside a request in normal use — middleware.ErrorPages builds its page from a func(code int, message string) with no echo.Context to pass down, so the scaffold's error page calls @Layout(nil, ...) — and every accessor built on Get (GetFlash, GetSubject, GetSubjectID) would otherwise nil-dereference there, turning a styled 404 into a recovered panic and a 500.

func GetAs

func GetAs[T any](c echo.Context, key Key[any]) (T, bool)

GetAs retrieves a value from the Echo context using an untyped key and asserts it to type T. It returns (zero, false) on missing or mismatched type, and on a nil context (see Get).

func MustGet

func MustGet[T any](c echo.Context, key Key[T]) T

MustGet retrieves a typed value from the Echo context or panics. Panicking is the contract; a nil context says so explicitly rather than surfacing as a bare nil dereference from inside Echo.

func MustGetAs

func MustGetAs[T any](c echo.Context, key Key[any]) T

MustGetAs retrieves a value from the Echo context using an untyped key and asserts it to type T. It panics with a clear message on missing or mismatched type.

func Set

func Set[T any](c echo.Context, key Key[T], value T)

Set stores a typed value in the Echo context.

Types

type Key

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

Key is a type-safe context key for use with Echo's context.

func NewKey

func NewKey[T any](name string) Key[T]

NewKey creates a new typed context key.

func (Key[T]) String

func (k Key[T]) String() string

String returns the key name.

Jump to

Keyboard shortcuts

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