Documentation
¶
Overview ¶
Package contextx is a helper package for managing context values Most **request-scoped data** is a singleton per request That is, it doesn't make sense for a request to carry around multiple loggers, users, traces you want to carry the _same one_ with you from function call to function call the way we've handled this historically is a separate context key per type you want to carry in the struct but with generics, instead of having to make a new zero-sized type for every struct we can just make a single generic type and use it for everything which is what this helper package is intended to do
Index ¶
- func From[T any](ctx context.Context) (T, bool)
- func FromOr[T any](ctx context.Context, def T) T
- func FromOrFunc[T any](ctx context.Context, f func() T) T
- func Has[T any](ctx context.Context) bool
- func MustFrom[T any](ctx context.Context) T
- func MustStringFrom[T ~string](ctx context.Context) T
- func StringFrom[T ~string](ctx context.Context) (T, bool)
- func StringFromOr[T ~string](ctx context.Context, def T) T
- func StringFromOrFunc[T ~string](ctx context.Context, fn func() T) T
- func With[T any](ctx context.Context, v T) context.Context
- func WithString[T ~string](ctx context.Context, value T) context.Context
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func From ¶
From returns the value associated with the wanted type from the context It performs a type assertion to convert the value to the desired type T If the type assertion is successful, it returns the value and true If the type assertion fails, it returns the zero value of type T and false
func FromOr ¶
FromOr returns the value associated with the wanted type or the given default value if the type is not found This function is useful when you want to ensure that a value is always returned from the context, even if the context does not contain a value of the desired type. By providing a default value, you can avoid handling the case where the value is missing and ensure that your code has a fallback value to use
func FromOrFunc ¶
FromOrFunc returns the value associated with the wanted type or the result of the given function if the type is not found This function is useful when the default value is expensive to compute or when the default value depends on some runtime conditions
func Has ¶ added in v0.5.2
Has checks if a value of type T exists in the context Useful for boolean flags using empty struct types as markers e.g. "OrganizationCreationContextKey"
func MustFrom ¶
MustFrom is similar to from, except that it panics if the type assertion fails / the value is not in the context
func MustStringFrom ¶ added in v0.5.2
MustStringFrom retrieves a string-based value from the context or panics if not found Use this when the value must exist for the operation to proceed
func StringFrom ¶ added in v0.5.2
StringFrom retrieves a string-based value from the context Returns the value and true if found, zero value and false otherwise
func StringFromOr ¶ added in v0.5.2
StringFromOr retrieves a string-based value from the context or returns the default value if not found
func StringFromOrFunc ¶ added in v0.5.2
StringFromOrFunc retrieves a string-based value from the context or returns the result of calling fn if not found The function is only called if the value is not present
Types ¶
This section is empty.