easyconfmgr

package module
v0.0.0-...-0e90f96 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2022 License: MIT Imports: 7 Imported by: 0

README

easyconfmgr

Easy to use、extensible configuration manager for golang

example

file

// read config
manager := easyconfmgr.NewManager(
    easyconfmgr.WithLoader(file.NewLoader(configFile)),
    easyconfmgr.WithParser(parser.NewYamlParser()),
    easyconfmgr.WithValuer(valuer.NewTrieTreeValuer()),
)
err := manager.ReadConfig()
if err != nil {
    t.Fatal(err)
}

stringVal, err := manager.GetString("string")
assert.Nil(t, err)
assert.Equal(t, config.String, stringVal)

// watch
watcher, err := file.NewWatcher(fp)
assert.Nil(t, err)
manager := easyconfmgr.NewManager(easyconfmgr.WithWatcher(watcher))

err = manager.StartWatch()
assert.Nil(t, err)
events := manager.Events()
tmpConfContent := confContent
for event := range events {
    tmpConfContent += "float_32: 0.3\n"
    assert.Equal(t, tmpConfContent, string(event.Data()))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultLogger = NewSampleLogger(os.Stderr)
View Source
var DiscardLogger = &NopLogger{}

Functions

This section is empty.

Types

type BoolValuer

type BoolValuer interface {
	// GetBool returns the value associated with the key as a bool.
	GetBool(key string) (bool, error)
}

type Event

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

func NewEvent

func NewEvent(description interface{ String() string }, data []byte) *Event

func (*Event) Data

func (e *Event) Data() []byte

func (*Event) String

func (e *Event) String() string

type EventKey

type EventKey interface {
	KeyName() string
}

type FloatValuer

type FloatValuer interface {
	// GetFloat32 returns the value associated with the key as a float32.
	GetFloat32(key string) (float32, error)
	// GetFloat64 returns the value associated with the key as a float64.
	GetFloat64(key string) (float64, error)
}

type IntValuer

type IntValuer interface {
	// GetInt returns the value associated with the key as an int.
	GetInt(key string) (int, error)
	// GetInt8 returns the value associated with the key as an int8.
	GetInt8(key string) (int8, error)
	// GetInt16 returns the value associated with the key as an int16.
	GetInt16(key string) (int16, error)
	// GetInt32 returns the value associated with the key as an int32.
	GetInt32(key string) (int32, error)
	// GetInt64 returns the value associated with the key as an int64.
	GetInt64(key string) (int64, error)

	// GetUint returns the value associated with the key as an uint.
	GetUint(key string) (uint, error)
	// GetUint8 returns the value associated with the key as an uint8.
	GetUint8(key string) (uint8, error)
	// GetUint16 returns the value associated with the key as an uint16.
	GetUint16(key string) (uint16, error)
	// GetUint32 returns the value associated with the key as an uint32.
	GetUint32(key string) (uint32, error)
	// GetUint64 returns the value associated with the key as an uint64.
	GetUint64(key string) (uint64, error)
}

type Loader

type Loader interface {
	// ContentType get config content type
	ContentType() string
	// Load load config raw data from some where, like local file or remote server
	Load() error
	// RawData get raw data
	RawData() []byte
}

Loader load data from some medium.

type Logger

type Logger interface {
	Debug(args ...interface{})
	Debugf(template string, args ...interface{})
	Info(args ...interface{})
	Infof(template string, args ...interface{})
	Warn(args ...interface{})
	Warnf(template string, args ...interface{})
	Error(args ...interface{})
	Errorf(template string, args ...interface{})
}

func NewSampleLogger

func NewSampleLogger(out io.Writer) Logger

type Manager

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

Manager config manager, support mutil load

func NewManager

func NewManager(opts ...Option) *Manager

func (*Manager) AllConfigs

func (m *Manager) AllConfigs() map[string]interface{}

func (*Manager) Events

func (m *Manager) Events() <-chan *Event

func (*Manager) Get

func (m *Manager) Get(key string) (interface{}, error)

func (*Manager) GetBool

func (m *Manager) GetBool(key string) (bool, error)

func (*Manager) GetBoolSlice

func (m *Manager) GetBoolSlice(key string) ([]bool, error)

func (*Manager) GetDuration

func (m *Manager) GetDuration(key string) (time.Duration, error)

func (*Manager) GetDurationSlice

func (m *Manager) GetDurationSlice(key string) ([]time.Duration, error)

func (*Manager) GetFloat32

func (m *Manager) GetFloat32(key string) (float32, error)

func (*Manager) GetFloat64

func (m *Manager) GetFloat64(key string) (float64, error)

func (*Manager) GetInt

func (m *Manager) GetInt(key string) (int, error)

func (*Manager) GetInt16

func (m *Manager) GetInt16(key string) (int16, error)

func (*Manager) GetInt32

func (m *Manager) GetInt32(key string) (int32, error)

func (*Manager) GetInt64

func (m *Manager) GetInt64(key string) (int64, error)

func (*Manager) GetInt8

func (m *Manager) GetInt8(key string) (int8, error)

func (*Manager) GetIntSlice

func (m *Manager) GetIntSlice(key string) ([]int, error)

func (*Manager) GetSlice

func (m *Manager) GetSlice(key string) ([]interface{}, error)

func (*Manager) GetString

func (m *Manager) GetString(key string) (string, error)

func (*Manager) GetStringMap

func (m *Manager) GetStringMap(key string) (map[string]interface{}, error)

func (*Manager) GetStringMapBool

func (m *Manager) GetStringMapBool(key string) (map[string]bool, error)

func (*Manager) GetStringMapInt

func (m *Manager) GetStringMapInt(key string) (map[string]int, error)

func (*Manager) GetStringMapInt64

func (m *Manager) GetStringMapInt64(key string) (map[string]int64, error)

func (*Manager) GetStringMapString

func (m *Manager) GetStringMapString(key string) (map[string]string, error)

func (*Manager) GetStringMapStringSlice

func (m *Manager) GetStringMapStringSlice(key string) (map[string][]string, error)

func (*Manager) GetStringSlice

func (m *Manager) GetStringSlice(key string) ([]string, error)

func (*Manager) GetTime

func (m *Manager) GetTime(key string) (time.Time, error)

func (*Manager) GetUint

func (m *Manager) GetUint(key string) (uint, error)

func (*Manager) GetUint16

func (m *Manager) GetUint16(key string) (uint16, error)

func (*Manager) GetUint32

func (m *Manager) GetUint32(key string) (uint32, error)

func (*Manager) GetUint64

func (m *Manager) GetUint64(key string) (uint64, error)

func (*Manager) GetUint8

func (m *Manager) GetUint8(key string) (uint8, error)

func (*Manager) ReadConfig

func (m *Manager) ReadConfig() error

func (*Manager) StartWatch

func (m *Manager) StartWatch() error

func (*Manager) StopWatch

func (m *Manager) StopWatch() error

func (*Manager) Unmarshal

func (m *Manager) Unmarshal(rawVal interface{}) error

func (*Manager) UnmarshalKey

func (m *Manager) UnmarshalKey(key string, rawVal interface{}) error

type MapValuer

type MapValuer interface {
	// GetStringMap returns the value associated with the key as a map of interfaces.
	GetStringMap(key string) (map[string]interface{}, error)
	// GetStringMapInt64 returns the value associated with the key as a map of int64.
	GetStringMapInt64(key string) (map[string]int64, error)
	// GetStringMapInt returns the value associated with the key as a map of int.
	GetStringMapInt(key string) (map[string]int, error)
	// GetStringMapBool returns the value associated with the key as a map of bool.
	GetStringMapBool(key string) (map[string]bool, error)
	// GetStringMapString returns the value associated with the key as a map of strings.
	GetStringMapString(key string) (map[string]string, error)
	// GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.
	GetStringMapStringSlice(key string) (map[string][]string, error)
}

type NopLogger

type NopLogger struct{}

func (*NopLogger) Debug

func (logger *NopLogger) Debug(args ...interface{})

func (*NopLogger) Debugf

func (logger *NopLogger) Debugf(template string, args ...interface{})

func (*NopLogger) Error

func (logger *NopLogger) Error(args ...interface{})

func (*NopLogger) Errorf

func (logger *NopLogger) Errorf(template string, args ...interface{})

func (*NopLogger) Info

func (logger *NopLogger) Info(args ...interface{})

func (*NopLogger) Infof

func (logger *NopLogger) Infof(template string, args ...interface{})

func (*NopLogger) Warn

func (logger *NopLogger) Warn(args ...interface{})

func (*NopLogger) Warnf

func (logger *NopLogger) Warnf(template string, args ...interface{})

type Option

type Option func(mgr *Manager)

func WithLoader

func WithLoader(loader ...Loader) Option

func WithParser

func WithParser(parser ...Parser) Option

func WithValuer

func WithValuer(valuer Valuer) Option

func WithWatcher

func WithWatcher(watcher ...Watcher) Option

type Parser

type Parser interface {
	// Parse unmarshal raw data into a config that type of `map[string]interface{}`
	// must ensure the sub value is type of `map[string]interface{}`
	Parse(rawData []byte) error
	// ConfigMap return config that type of `map[string]interface{}`
	ConfigMap() map[string]interface{}
	// Support returns true if the Parser supports this contentType, return false if the Parser not supports this contentType
	Support(contentType string) bool
}

Parser encode raw data to map `map[string]interface{}`

type SampleLogger

type SampleLogger struct {
	DebugLogger *log.Logger
	InfoLogger  *log.Logger
	WarnLogger  *log.Logger
	ErrorLogger *log.Logger
}

func (*SampleLogger) Debug

func (logger *SampleLogger) Debug(args ...interface{})

func (*SampleLogger) Debugf

func (logger *SampleLogger) Debugf(template string, args ...interface{})

func (*SampleLogger) Error

func (logger *SampleLogger) Error(args ...interface{})

func (*SampleLogger) Errorf

func (logger *SampleLogger) Errorf(template string, args ...interface{})

func (*SampleLogger) Info

func (logger *SampleLogger) Info(args ...interface{})

func (*SampleLogger) Infof

func (logger *SampleLogger) Infof(template string, args ...interface{})

func (*SampleLogger) Warn

func (logger *SampleLogger) Warn(args ...interface{})

func (*SampleLogger) Warnf

func (logger *SampleLogger) Warnf(template string, args ...interface{})

type SliceValuer

type SliceValuer interface {
	// GetSlice returns the value associated with the key as a slice of interface values.
	GetSlice(key string) ([]interface{}, error)
	// GetIntSlice returns the value associated with the key as a slice of int values.
	GetIntSlice(key string) ([]int, error)
	// GetBoolSlice returns the value associated with the key as a slice of bool values.
	GetBoolSlice(key string) ([]bool, error)
	// GetStringSlice returns the value associated with the key as a slice of strings.
	GetStringSlice(key string) ([]string, error)
	// GetDurationSlice returns the value associated with the key as a slice of duration.
	GetDurationSlice(key string) ([]time.Duration, error)
}

type StringValuer

type StringValuer interface {
	// GetString returns the value associated with the key as a string.
	GetString(key string) (string, error)
}

type TimeValuer

type TimeValuer interface {
	// GetTime returns the value associated with the key as time.
	GetTime(key string) (time.Time, error)
	// GetDuration returns the value associated with the key as a duration.
	GetDuration(key string) (time.Duration, error)
}

type Valuer

type Valuer interface {
	// AddConfig add map[string]interface{} config
	AddConfig(configs ...map[string]interface{})
	// AllConfigs merges all config and returns them as a map[string]interface{}.
	AllConfigs() map[string]interface{}
	// UnmarshalKey takes a single key and unmarshal it into a Struct.
	UnmarshalKey(key string, rawVal interface{}) error
	// Unmarshal unmarshal the config into a Struct.
	Unmarshal(rawVal interface{}) error
	// Get can retrieve any value given the key to use.
	Get(key string) (interface{}, error)
	FloatValuer
	IntValuer
	BoolValuer
	StringValuer
	TimeValuer
	SliceValuer
	MapValuer
}

Valuer is Gets the value of a key, and Unmarshal value to a struct

type Watcher

type Watcher interface {
	// Watch start watch
	Watch() error
	// Events event sent to channel
	Events() <-chan *Event
	// Stop stop watch
	Stop() error
}

Watcher monitors whether the data source has changed and, if so, notifies the changed event

Directories

Path Synopsis
medium

Jump to

Keyboard shortcuts

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