state

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CtxKeyType

type CtxKeyType string

CtxKeyType is a type that can be used as a key for a context.Context

type State

type State[T any] struct {
	CtxKey CtxKeyType
}

State is a type that can be used to store data in a context.Context It is useful for storing data that needs to be shared between tests

func NewState

func NewState[T any](opts ...StateOption[T]) *State[T]

NewState creates a new State

Example:

type test struct {
	Name string
}

state := state.NewState[test]()

func (*State[T]) Enrich

func (state *State[T]) Enrich(ctx context.Context, data *T) context.Context

Enrich enriches the context with the data

Example:

type test struct {
	Name string
}

state := state.NewState[test]()
ctx := state.Enrich(ctx, &test{
	Name: "John",
})

func (*State[T]) Retrieve

func (state *State[T]) Retrieve(ctx context.Context) *T

Retrieve retrieves the data from the context

Example:

type test struct {
	Name string
}

state := state.NewState[test]()
ctx := state.Enrich(ctx, &test{
	Name: "John",
})
data := state.Retrieve(ctx)

fmt.Println(data.Name) // John

type StateOption

type StateOption[T any] func(*State[T])

StateOption is a type that can be used to configure a State

func WithCtxKey

func WithCtxKey[T any](ctxKey CtxKeyType) StateOption[T]

WithCtxKey is a StateOption that sets the key for the context.Context

Default: default

Example:

type test struct {
	Name string
}

state := state.NewState[test](
	state.WithCtxKey("test"),
)

Jump to

Keyboard shortcuts

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