appctx

package
v0.21.2 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package appctx provides a context.Context based mechanism of sharing certain "global" resources across different parts of an application. When building applications with the github.com/justtrackio/gosoline/pkg/application package a Container is automatically injected into the applications context.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get added in v0.10.0

func Get[T any](ctx context.Context, key any) (T, error)

Get retrieves the value behind key from the container which was injected into ctx by WithContainer. If a new value should be created when none is found for key, use Provide. Returns ErrNoItemFound if key is not present in the container. Returns ErrNoApplicationContainerFound when no container is present in ctx.

func MetadataAppend

func MetadataAppend(ctx context.Context, key string, values ...interface{}) error

MetadataAppend appends the provided values to any existing values at key. The metadata carrier comes from ctx.

func MetadataSet

func MetadataSet(ctx context.Context, key string, value interface{}) error

MetadataSet sets the value for key to the provided value, overwriting any existing value. The metadata carrier comes from ctx.

func Provide

func Provide[T any](ctx context.Context, key any, factory func() (T, error)) (T, error)

Provide retrieves the value behind key from the container which was injected into ctx by WithContainer. If key is not present in the container the factory will create a new value and store it in the container. This value is then accessible from all other points in a program which have access to the context containing the container. If no new value should be created when none is found for key, use Get. Returns ErrNoApplicationContainerFound when no container is present in ctx.

func WithContainer

func WithContainer(ctx context.Context) context.Context

WithContainer injects a thread safe pointer backed container into the provided context. This container is then addressable by Provide and Get.

Types

type ErrNoApplicationContainerFound

type ErrNoApplicationContainerFound struct{}

func (ErrNoApplicationContainerFound) Error

type ErrNoItemFound

type ErrNoItemFound struct {
	Key interface{}
}

func (ErrNoItemFound) Error

func (e ErrNoItemFound) Error() string

type Metadata

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

Metadata provides a thread safe key value store intended to be used with the container injected by WithContainer.

func NewMetadata

func NewMetadata() *Metadata

NewMetadata creates a new Metadata key value store

func ProvideMetadata

func ProvideMetadata(ctx context.Context) (*Metadata, error)

ProvideMetadata retrieves the metadata carrier from ctx if one is present, else it creates a new one. This is done through Provide, thus having the metadata globally available.

func (*Metadata) Append

func (m *Metadata) Append(key string, values ...interface{}) error

Append appends the provided values to any values present at key. If no values are present, they are set to values. If there are already values present no duplicates will be added (the comparison is performed through reflect.DeepEqual).

func (*Metadata) Get

func (m *Metadata) Get(key string) *mapx.MapXNode

Get retrieves the value node for key. If the key does not exist the data in the node will be empty.

func (*Metadata) Msi

func (m *Metadata) Msi() map[string]interface{}

Msi returns a map of all keys to values. Implements the mapx.Msier interface.

func (*Metadata) Set

func (m *Metadata) Set(key string, value interface{})

Set sets the values at key to the provided values, overwriting any already present values.

Jump to

Keyboard shortcuts

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