config

package
v3.10.38 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2024 License: Apache-2.0 Imports: 15 Imported by: 14

Documentation

Overview

Package config is an interface for dynamic configuration.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCodecMissing is returned when codec needed and not specified
	ErrCodecMissing = errors.New("codec missing")
	// ErrInvalidStruct is returned when the target struct is invalid
	ErrInvalidStruct = errors.New("invalid struct specified")
	// ErrWatcherStopped is returned when source watcher has been stopped
	ErrWatcherStopped = errors.New("watcher stopped")
	// ErrWatcherNotImplemented returned when config does not implement watch
	ErrWatcherNotImplemented = errors.New("watcher not implemented")
)
View Source
var (
	// DefaultBeforeLoad default func that runs before config Load
	DefaultBeforeLoad = func(ctx context.Context, c Config) error {
		for _, fn := range c.Options().BeforeLoad {
			if fn == nil {
				return nil
			}
			if err := fn(ctx, c); err != nil {
				c.Options().Logger.Errorf(ctx, "%s BeforeLoad err: %v", c.String(), err)
				if !c.Options().AllowFail {
					return err
				}
			}
		}
		return nil
	}
	// DefaultAfterLoad default func that runs after config Load
	DefaultAfterLoad = func(ctx context.Context, c Config) error {
		for _, fn := range c.Options().AfterLoad {
			if fn == nil {
				return nil
			}
			if err := fn(ctx, c); err != nil {
				c.Options().Logger.Errorf(ctx, "%s AfterLoad err: %v", c.String(), err)
				if !c.Options().AllowFail {
					return err
				}
			}
		}
		return nil
	}
	// DefaultBeforeSave default func that runs befora config Save
	DefaultBeforeSave = func(ctx context.Context, c Config) error {
		for _, fn := range c.Options().BeforeSave {
			if fn == nil {
				return nil
			}
			if err := fn(ctx, c); err != nil {
				c.Options().Logger.Errorf(ctx, "%s BeforeSave err: %v", c.String(), err)
				if !c.Options().AllowFail {
					return err
				}
			}
		}
		return nil
	}
	// DefaultAfterSave default func that runs after config Save
	DefaultAfterSave = func(ctx context.Context, c Config) error {
		for _, fn := range c.Options().AfterSave {
			if fn == nil {
				return nil
			}
			if err := fn(ctx, c); err != nil {
				c.Options().Logger.Errorf(ctx, "%s AfterSave err: %v", c.String(), err)
				if !c.Options().AllowFail {
					return err
				}
			}
		}
		return nil
	}
	// DefaultBeforeInit default func that runs befora config Init
	DefaultBeforeInit = func(ctx context.Context, c Config) error {
		for _, fn := range c.Options().BeforeInit {
			if fn == nil {
				return nil
			}
			if err := fn(ctx, c); err != nil {
				c.Options().Logger.Errorf(ctx, "%s BeforeInit err: %v", c.String(), err)
				if !c.Options().AllowFail {
					return err
				}
			}
		}
		return nil
	}
	// DefaultAfterInit default func that runs after config Init
	DefaultAfterInit = func(ctx context.Context, c Config) error {
		for _, fn := range c.Options().AfterSave {
			if fn == nil {
				return nil
			}
			if err := fn(ctx, c); err != nil {
				c.Options().Logger.Errorf(ctx, "%s AfterInit err: %v", c.String(), err)
				if !c.Options().AllowFail {
					return err
				}
			}
		}
		return nil
	}
)
View Source
var DefaultConfig = NewConfig()

DefaultConfig default config

View Source
var DefaultWatcherMaxInterval = 9 * time.Second

DefaultWatcherMaxInterval default max interval for poll changes

View Source
var DefaultWatcherMinInterval = 5 * time.Second

DefaultWatcherMinInterval default min interval for poll changes

Functions

func Load

func Load(ctx context.Context, cs []Config, opts ...LoadOption) error

Load loads config from config sources

func NewContext

func NewContext(ctx context.Context, c Config) context.Context

NewContext put store in context

func Validate added in v3.9.1

func Validate(ctx context.Context, cfg interface{}) error

Validate runs Validate() error func for each struct field

Types

type Config

type Config interface {
	// Name returns name of config
	Name() string
	// Init the config
	Init(opts ...Option) error
	// Options in the config
	Options() Options
	// Load config from sources
	Load(context.Context, ...LoadOption) error
	// Save config to sources
	Save(context.Context, ...SaveOption) error
	// Watch a config for changes
	Watch(context.Context, ...WatchOption) (Watcher, error)
	// String returns config type name
	String() string
}

Config is an interface abstraction for dynamic configuration

func FromContext

func FromContext(ctx context.Context) (Config, bool)

FromContext returns store from context

func NewConfig

func NewConfig(opts ...Option) Config

NewConfig returns new default config source

type LoadOption

type LoadOption func(o *LoadOptions)

LoadOption function signature

func LoadAppend

func LoadAppend(b bool) LoadOption

LoadAppend override values when load

func LoadOverride

func LoadOverride(b bool) LoadOption

LoadOverride override values when load

func LoadStruct

func LoadStruct(src interface{}) LoadOption

LoadStruct override struct for loading

func SetLoadOption added in v3.8.8

func SetLoadOption(k, v interface{}) LoadOption

SetLoadOption returns a function to setup a context with given value

type LoadOptions

type LoadOptions struct {
	Struct   interface{}
	Context  context.Context
	Override bool
	Append   bool
}

LoadOptions struct

func NewLoadOptions

func NewLoadOptions(opts ...LoadOption) LoadOptions

NewLoadOptions create LoadOptions struct with provided opts

type Option

type Option func(o *Options)

Option function signature

func AfterInit added in v3.10.15

func AfterInit(fn ...func(context.Context, Config) error) Option

AfterInit run funcs after config Init

func AfterLoad

func AfterLoad(fn ...func(context.Context, Config) error) Option

AfterLoad run funcs after config load

func AfterSave

func AfterSave(fn ...func(context.Context, Config) error) Option

AfterSave run fncs after save

func AllowFail

func AllowFail(b bool) Option

AllowFail allows config source to fail

func BeforeInit added in v3.10.15

func BeforeInit(fn ...func(context.Context, Config) error) Option

BeforeInit run funcs before config Init

func BeforeLoad

func BeforeLoad(fn ...func(context.Context, Config) error) Option

BeforeLoad run funcs before config load

func BeforeSave

func BeforeSave(fn ...func(context.Context, Config) error) Option

BeforeSave run funcs before save

func Codec

func Codec(c codec.Codec) Option

Codec sets the source codec

func Context

func Context(ctx context.Context) Option

Context pass context

func Logger

func Logger(l logger.Logger) Option

Logger sets the logger

func Name

func Name(n string) Option

Name sets the name

func SetOption

func SetOption(k, v interface{}) Option

SetOption returns a function to setup a context with given value

func Struct

func Struct(v interface{}) Option

Struct used as config

func StructTag

func StructTag(name string) Option

StructTag sets the struct tag that used for filling

func Tracer

func Tracer(t tracer.Tracer) Option

Tracer to be used for tracing

type Options

type Options struct {
	// Struct holds the destination config struct
	Struct interface{}
	// Codec that used for load/save
	Codec codec.Codec
	// Tracer that will be used
	Tracer tracer.Tracer
	// Meter that will be used
	Meter meter.Meter
	// Logger that will be used
	Logger logger.Logger
	// Context used for external options
	Context context.Context
	// Name of the config
	Name string
	// StructTag name
	StructTag string
	// BeforeSave contains slice of funcs that runs before Save
	BeforeSave []func(context.Context, Config) error
	// AfterSave contains slice of funcs that runs after Save
	AfterSave []func(context.Context, Config) error
	// BeforeLoad contains slice of funcs that runs before Load
	BeforeLoad []func(context.Context, Config) error
	// AfterLoad contains slice of funcs that runs after Load
	AfterLoad []func(context.Context, Config) error
	// BeforeInit contains slice of funcs that runs before Init
	BeforeInit []func(context.Context, Config) error
	// AfterInit contains slice of funcs that runs after Init
	AfterInit []func(context.Context, Config) error
	// AllowFail flag to allow fail in config source
	AllowFail bool
	// SkipLoad runs only if condition returns true
	SkipLoad func(context.Context, Config) bool
	// SkipSave runs only if condition returns true
	SkipSave func(context.Context, Config) bool
}

Options hold the config options

func NewOptions

func NewOptions(opts ...Option) Options

NewOptions new options struct with filed values

type SaveOption

type SaveOption func(o *SaveOptions)

SaveOption function signature

func SaveStruct

func SaveStruct(src interface{}) SaveOption

SaveStruct override struct for save to config

func SetSaveOption added in v3.8.8

func SetSaveOption(k, v interface{}) SaveOption

SetSaveOption returns a function to setup a context with given value

type SaveOptions

type SaveOptions struct {
	Struct  interface{}
	Context context.Context
}

SaveOptions struct

func NewSaveOptions

func NewSaveOptions(opts ...SaveOption) SaveOptions

NewSaveOptions fill SaveOptions struct

type Validator added in v3.9.1

type Validator interface {
	Validate() error
}

type WatchOption

type WatchOption func(*WatchOptions)

WatchOption func signature

func SetWatchOption added in v3.8.9

func SetWatchOption(k, v interface{}) WatchOption

SetWatchOption returns a function to setup a context with given value

func WatchCoalesce

func WatchCoalesce(b bool) WatchOption

WatchCoalesce controls watch event combining

func WatchContext

func WatchContext(ctx context.Context) WatchOption

WatchContext pass context

func WatchInterval

func WatchInterval(min, max time.Duration) WatchOption

WatchInterval specifies min and max time.Duration for pulling changes

func WatchStruct

func WatchStruct(src interface{}) WatchOption

WatchStruct overrides struct for fill

type WatchOptions

type WatchOptions struct {
	// Context used by non default options
	Context context.Context
	// Struct for filling
	Struct interface{}
	// MinInterval specifies the min time.Duration interval for poll changes
	MinInterval time.Duration
	// MaxInterval specifies the max time.Duration interval for poll changes
	MaxInterval time.Duration
	// Coalesce multiple events to one
	Coalesce bool
}

WatchOptions struuct

func NewWatchOptions

func NewWatchOptions(opts ...WatchOption) WatchOptions

NewWatchOptions create WatchOptions struct with provided opts

type Watcher

type Watcher interface {
	// Next blocks until update happens or error returned
	Next() (map[string]interface{}, error)
	// Stop stops watcher
	Stop() error
}

Watcher is the config watcher

Jump to

Keyboard shortcuts

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