config

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2022 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package config provider load configuration from file

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrUnsupportedEncoder is error
	ErrUnsupportedEncoder = errors.New("unsupported encoder")
)
View Source
var (
	// ErrWatcherStopped defined watcher stoped
	ErrWatcherStopped = errors.New("watcher stoped")
)

Functions

func RegisterEncoder

func RegisterEncoder(Encoder Encoder, names ...string) error

RegisterEncoder retister a type loader to package level

func RegisterProvider

func RegisterProvider(name string, provider Provider) error

RegisterProvider register provider

func RegisterProviderCreator

func RegisterProviderCreator(name string, creator ProviderCreator) error

RegisterProviderCreator register a provider creator

Types

type ChangeSet added in v0.2.1

type ChangeSet struct {
	Data      []byte
	Checksum  string
	Format    string
	Source    string
	Timestamp time.Time
}

ChangeSet represents a set of changes from a source

func (*ChangeSet) Sum added in v0.2.1

func (c *ChangeSet) Sum() string

Sum returns the md5 checksum of the ChangeSet data

type Config

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

Config present runtime

func New

func New(v interface{}, opts ...Option) (*Config, error)

New create a new config

func (*Config) CopyValue added in v0.2.0

func (c *Config) CopyValue(v interface{}) error

CopyValue copy the raw data of config to object

func (*Config) Get added in v0.2.0

func (c *Config) Get(key string) (interface{}, bool)

Get take a value out from

func (*Config) Options

func (c *Config) Options() *Options

Options return the options

type Encoder

type Encoder interface {
	Decode([]byte, interface{}) error
	Encode(interface{}) ([]byte, error)
	String() string
}

Encoder handles source encoding formats

Unmarshal []byte to object Marshal object to []byte

func NewHCLEncoder

func NewHCLEncoder() Encoder

NewHCLEncoder return a hcl encoder

func NewJSONEncoder

func NewJSONEncoder() Encoder

NewJSONEncoder return a json encoder

func NewTOMLEncoder

func NewTOMLEncoder() Encoder

NewTOMLEncoder return a toml encoder

func NewXMLEncoder

func NewXMLEncoder() Encoder

NewXMLEncoder return a xml encoder

func NewYAMLEncoder

func NewYAMLEncoder() Encoder

NewYAMLEncoder return a yaml encoder

type EncoderFactory

type EncoderFactory struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

EncoderFactory multi encoders

func NewEncoderFactory

func NewEncoderFactory() *EncoderFactory

NewEncoderFactory create a new Encoder factry

func (*EncoderFactory) Decode

func (t *EncoderFactory) Decode(name string, data []byte, out interface{}) error

Decode data to out with special type name

func (*EncoderFactory) Encode

func (t *EncoderFactory) Encode(name string, out interface{}) ([]byte, error)

Encode out to data with special type name

func (*EncoderFactory) Register

func (t *EncoderFactory) Register(Encoder Encoder, names ...string) error

Register register a loader

type ExampleConfig

type ExampleConfig struct {
	// .env file we need to offer
	Timeout time.Duration // should we need to add all tags?
	Name    string        //
}

ExampleConfig example configuration

type Option

type Option func(c *Options)

Option present config for C

func WithChild

func WithChild() Option

WithChild add sub config

func WithConfigChanged added in v0.2.1

func WithConfigChanged(f func(o, n interface{})) Option

WithConfigChanged set on config chaned

func WithEnv

func WithEnv() Option

WithEnv set env loader, distinguish different environment

func WithName

func WithName(name string) Option

WithName set name

func WithNames added in v0.2.1

func WithNames(names ...string) Option

WithNames set names

func WithProvider

func WithProvider(provides ...Provider) Option

WithProvider set provider

func WithType

func WithType(typ ...string) Option

WithType set file extension type for loader

type Options

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

Options present current params

func NewOptions

func NewOptions(opts ...Option) *Options

NewOptions return a new options

type Provider

type Provider interface {
	// load configuraton data from name
	Read(name string, typs ...string) ([]byte, error)

	// dumps configuration
	// will we need to update the conguration
	Write(name string, data []byte, typs ...string) error

	// watch the config whilte change
	Watch(name string, typs ...string) (Watcher, error)

	// String returns name of provider
	String() string
}

Provider defined how to take payload of configuration file, http, database or something else

source is the interface for sources or we need to use a container for []byte like struct{ Data []byte, Checksum string, Timestamp time.Time }

func NewFSProvider

func NewFSProvider(path string) (Provider, error)

NewFSProvider return a fs provider

func NewProvider

func NewProvider(name string, config interface{}) (Provider, error)

NewProvider create a provider with name and configuration

func NewProviderFromURI added in v0.2.0

func NewProviderFromURI(url string) (Provider, error)

NewProviderFromURI create a provider with uri string at most time, format schema://config

type ProviderCreator

type ProviderCreator func(c interface{}) (Provider, error)

ProviderCreator function to create provider

type ProviderFactory

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

ProviderFactory provider factory

func NewProviderFactory

func NewProviderFactory() *ProviderFactory

NewProviderFactory create a factory

func (*ProviderFactory) Register

func (f *ProviderFactory) Register(name string, provider Provider) error

Register register a provider

type Watcher

type Watcher interface {
	Next() (*ChangeSet, error)
	Stop() error
}

Watcher watches a source for changes

Jump to

Keyboard shortcuts

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