injectable

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2023 License: Apache-2.0 Imports: 4 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfigFromContext

func ConfigFromContext(ctx context.Context, defaulters ...func() *viper.Viper) *viper.Viper

ConfigFromContext retrieves a configuration registry from the context.

An optional defaulter can be added to deal with a non-existing config.

func SLoggerFromContext added in v0.1.3

func SLoggerFromContext(ctx context.Context, defaulters ...func() *slog.Logger) *slog.Logger

SLoggerFromContext retrieves a standard structured logger from the context.

Optional defaulters can be added to deal with a non-existing logger.

func ZapLoggerFromContext added in v0.2.0

func ZapLoggerFromContext(ctx context.Context, defaulters ...func() *zap.Logger) *zap.Logger

ZapLoggerFromContext retrieves a zap logger from the context.

Optional defaulters can be added to deal with a non-existing logger.

Types

type Config

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

Config can wrap a viper.Viper configuration in the context

func NewConfig

func NewConfig(v *viper.Viper) *Config

func (*Config) Context

func (c *Config) Context(ctx context.Context) context.Context

func (Config) FromContext

func (c Config) FromContext(ctx context.Context) interface{}

type ContextInjectable

type ContextInjectable interface {
	// Context builds a context with the injected value
	Context(context.Context) context.Context

	// FromContext retrieves the injected value from the context.
	//
	// It should work even if the receiver is zero value.
	FromContext(context.Context) interface{}
}

ContextInjectable knows how to retrieve a value from a Context.

Users of the github.com/fredbi/go-cli/cli package can define their own injections via the context.

NOTE: every such type should declare their own key type to avoid conflicts inside the context.

For example:

type commandCtxKey uint8
const ctxConfig commandCtxKey = iota + 1

type SLogger added in v0.1.3

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

func NewSLogger added in v0.1.3

func NewSLogger(l *slog.Logger) *SLogger

func (*SLogger) Context added in v0.1.3

func (c *SLogger) Context(ctx context.Context) context.Context

func (SLogger) FromContext added in v0.1.3

func (c SLogger) FromContext(ctx context.Context) interface{}

type ZapLogger

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

func NewZapLogger added in v0.2.0

func NewZapLogger(l *zap.Logger) *ZapLogger

func (*ZapLogger) Context added in v0.2.0

func (c *ZapLogger) Context(ctx context.Context) context.Context

func (ZapLogger) FromContext added in v0.2.0

func (c ZapLogger) FromContext(ctx context.Context) interface{}

Jump to

Keyboard shortcuts

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