contextx

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNoConfigInContext = errors.New("configuration provider not found in context")

ErrNoConfigInContext is returned when no config is found in the context.

Functions

func ConfigFromContext

func ConfigFromContext(ctx context.Context) (*configx.Provider, error)

ConfigFromContext returns the configuration provider from the context or an error if no configuration provider is found in the context.

Example
ctx := context.Background()

config, err := configx.New(ctx, []byte(`{"type":"object","properties":{"foo":{"type":"string"}}}`), configx.WithValue("foo", "bar"))
if err != nil {
	panic(err)
}

ctx = WithConfig(ctx, config)
fmt.Printf("foo = %s", MustConfigFromContext(ctx).String("foo"))
Output:

foo = bar

func IsRootContext

func IsRootContext(ctx context.Context) bool

func MustConfigFromContext

func MustConfigFromContext(ctx context.Context) *configx.Provider

MustConfigFromContext returns the configuration provider from the context or panics if no configuration provider is found in the context.

func SetNIDContext

func SetNIDContext(ctx context.Context, nid uuid.UUID) context.Context

SetNIDContext sets the nid for the given context.

func WithConfig

func WithConfig(ctx context.Context, p *configx.Provider) context.Context

WithConfig returns a new context with the given configuration provider.

Types

type ContextKey

type ContextKey int
const (
	ValidContextKey ContextKey = iota + 1
)

type Contextualizer

type Contextualizer interface {
	// Network returns the network id for the given context.
	Network(ctx context.Context, network uuid.UUID) uuid.UUID

	// Config returns the config for the given context.
	Config(ctx context.Context, config *configx.Provider) *configx.Provider
}

type Default

type Default struct{}

func (*Default) Config

func (d *Default) Config(ctx context.Context, config *configx.Provider) *configx.Provider

func (*Default) Network

func (d *Default) Network(ctx context.Context, network uuid.UUID) uuid.UUID

type Provider

type Provider interface {
	Contextualizer() Contextualizer
}

type Static

type Static struct {
	NID uuid.UUID
	C   *configx.Provider
}

func (*Static) Config

func (d *Static) Config(ctx context.Context, config *configx.Provider) *configx.Provider

func (*Static) Network

func (d *Static) Network(ctx context.Context, network uuid.UUID) uuid.UUID

type TestContextualizer

type TestContextualizer struct{}

TestContextualizer is a mock implementation of the Contextualizer interface.

func (*TestContextualizer) Config

Config returns the config for the given context.

func (*TestContextualizer) Network

func (d *TestContextualizer) Network(ctx context.Context, network uuid.UUID) uuid.UUID

Network returns the network id for the given context.

Jump to

Keyboard shortcuts

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