ectx

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2022 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Go programmers often use contexts to store request-scoped information, especially metadata or log fields. In general, these are singletons per type: it doesn't make sense for a context to have multiple IDs, origins, or loggers. The functions in this package provide a simple and useful API for working with contexts in this way.

QUICK START

Save and retrieve values from a context:

ctx := ectx.WithValue(context.Background(), zap.L().With(zap.String("author", "efron")))

Get a value from the context:

log, ok := ectx.Val[*zap.Logger](ctx)

Get a value from a context, using a default if it's missing

log := ectx.Val(ctx, log.Default())

Get a value from a context, or call the provided function if it's missing

log := ectx.ValOr(ctx, log.Default) // note the missing ()

Word of warning

The context is often misused as a general-purpose key-value storage, which causes a ton of harm. This ectx's api _deliberately omits_ the more general context.WithValue to discourage this. Ask yourself two questions before putting a value in the context rather than passing it directly as a value in the function signature.

1: Is this PART of the function, or is it ABOUT the function?

2: Should the function proceed without the value (i.e, through a sensible default or no-op?)

Only if both are true should you use ectx. When in doubt, error on the side of injecting dependencies directly.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Background

func Background() context.Context
// alias for context.Background

Background returns a non-nil, empty Context. It is never canceled, has no values, and has no deadline. It is typically used by the main function, initialization, and tests, and as the top-level Context for incoming requests.

func MustVal

func MustVal[T any](ctx context.Context) T

MustVal gets the entry stored under TypeKey[T], panicking otherwise.

Warning

Be careful when and where you use this function! Anything you MUST have should probably be in the function signature, not the context. This can be useful for tests & in rare other occasions and is included for completion.

func StripCancel

func StripCancel(ctx context.Context) context.Context

StripCancel returns a context which inherits it's parents values, but NOT it's deadline or cancellation.

func TODO

func TODO() context.Context
// alias for context.TODO()

TODO returns a non-nil, empty Context. Code should use context.TODO when it's unclear which Context to use or it is not yet available (because the surrounding function has not yet been extended to accept a Context parameter).

func Val

func Val[T any](ctx context.Context) (T, bool)

Val looks up the value stored under TypeKey[T]{}.

func ValOr

func ValOr[T any](ctx context.Context, fallback T) T

ValOr gets the entry stored under TypeKey[T], if it exists, returning fallback otherwise.

func ValOrElse

func ValOrElse[T any](ctx context.Context, f func() T) T

ValOrElse is like ValOr, but lazily evaluated.

func ValOrZero

func ValOrZero[T any](ctx context.Context) T

ValOrZero is as FromCtx, but ignores the 'ok'.

func WithCancel

func WithCancel(parent context.Context) (ctx context.Context, cancel context.CancelFunc)

// alias for context.WithCancel WithCancel returns a copy of parent with a new Done channel. The returned context's Done channel is closed when the returned cancel function is called or when the parent context's Done channel is closed, whichever happens first. Canceling this context releases resources associated with it, so code should call cancel as soon as the operations running in this Context complete.

func WithDeadline

func WithDeadline(parent context.Context, deadline time.Time) (ctx context.Context, cancel context.CancelFunc)
// alias for context.WithDeadline

WithDeadline returns a copy of the parent context with the deadline adjusted to be no later than d. If the parent's deadline is already earlier than d, WithDeadline(parent, d) is semantically equivalent to parent. The returned context's Done channel is closed when the deadline expires, when the returned cancel function is called, or when the parent context's Done channel is closed, whichever happens first. Canceling this context releases resources associated with it, so code should call cancel as soon as the operations running in this Context complete.

func WithTimeout

func WithTimeout(parent context.Context, timeout time.Duration) (ctx context.Context, cancel context.CancelFunc)
// alias for context.WithTimeout

WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)). Canceling this context releases resources associated with it, so code should call cancel as soon as the operations running in this Context complete:

func WithVal

func WithVal[T any](ctx context.Context, t T) context.Context

WithVal returns a new context with the value under a TypeKey[T]

Types

type TypeKey

type TypeKey[T any] struct{}

TypeKey is a zero-sized type that acts as a context key for the type T. You can't store multiple values of the same type with a TypeKey! If you're worried about that, don't use the functions in this package!

Jump to

Keyboard shortcuts

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