config

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 27, 2022 License: MIT Imports: 19 Imported by: 0

README

config

CI

Go configuration library

Usage

package config_test

import (
	"path"

	"github.com/sraphs/config"
	"github.com/sraphs/config/env"
	"github.com/sraphs/config/file"
	"github.com/sraphs/config/flag"
	testData "github.com/sraphs/config/internal/testdata"
)

func Example() {
	p := path.Join("internal", "testdata")

	c := config.New(
		config.WithSource(
			env.NewSource("sraph_"),
			file.NewSource(p),
			flag.NewSource(),
		),
	)

	if err := c.Load(); err != nil {
		panic(err)
	}

	var conf testData.Conf

	if err := c.Scan(&conf); err != nil {
		panic(err)
	}

	c.Watch(func(c config.Config) {
		c.Scan(&conf)
	})

	// fmt.Println(conf)

	// Output:
}

Contributing

We alway welcome your contributions 👏

  1. Fork the repository
  2. Create Feat_xxx branch
  3. Commit your code
  4. Create Pull Request

CHANGELOG

See Releases

License

MIT © sraph.com

Documentation

Overview

Example
package main

import (
	"fmt"
	"os"
	"path"

	"github.com/sraphs/config"
	"github.com/sraphs/config/env"
	"github.com/sraphs/config/file"
	"github.com/sraphs/config/flag"

	testData "github.com/sraphs/config/internal/testdata"
)

func main() {
	os.Setenv("sraph_log_level", "warn")

	p := path.Join("internal", "testdata", "yaml")

	c := config.New(
		config.WithSource(
			env.NewSource("sraph_"),
			file.NewSource(p),
			flag.NewSource(),
		),
	)

	if err := c.Load(); err != nil {
		panic(err)
	}

	var conf testData.Conf

	if err := c.Scan(&conf); err != nil {
		panic(err)
	}

	c.Watch(func(c config.Config) {
		c.Scan(&conf)
	})

	fmt.Println(&conf)

	c.Get("log.level").String()

	// Outputs:
	// log:{level:"warn"}  server:{http:{addr:"0.0.0.0:8000"  timeout:{seconds:1}}  grpc:{addr:"0.0.0.0:9000"  timeout:{seconds:1}}}  data:{database:{driver:"mysql"}  redis:{addr:"mysql:6379"  read_timeout:{nanos:200000000}  write_timeout:{nanos:200000000}}}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedFormat = errors.New("unsupported format")
	ErrScanNeedPtr       = errors.New("scan need ptr")
	ErrNotFound          = errors.New("key not found")
	ErrTypeAssert        = errors.New("type assert error")
)
View Source
var (
	SupportedFormats = []string{"env", "json", "xml", "yaml", "yml"}
)

Functions

This section is empty.

Types

type Config

type Config interface {
	Load() error
	Scan(v interface{}) error
	Watch(o Observer) error
	Close() error
	Get(key string) Value
}

Config is a config interface.

func New

func New(opts ...Option) Config

New new a config with options.

type Decoder added in v1.0.1

type Decoder func(*Descriptor, map[string]interface{}) error

Decoder is config decoder.

type Descriptor

type Descriptor struct {
	Name   string
	Format string
	Data   []byte
}

Descriptor is file or env or flag descriptor.

func (*Descriptor) GetCodec

func (d *Descriptor) GetCodec() encoding.Codec

type Observer

type Observer func(Config)

Observer is config observer.

type Option

type Option func(*options)

Option is config option.

func WithDecoder added in v1.0.1

func WithDecoder(d Decoder) Option

WithDecoder with config decoder. DefaultDecoder behavior: If KeyValue.Format is non-empty, then KeyValue.Value will be deserialized into map[string]interface{} and stored in the config cache(map[string]interface{}) if KeyValue.Format is empty,{KeyValue.Key : KeyValue.Value} will be stored in config cache(map[string]interface{})

func WithLog added in v1.0.1

func WithLog() Option

WithLog with config log.

func WithResolver added in v1.0.1

func WithResolver(r Resolver) Option

WithResolver with config resolver.

func WithSource

func WithSource(s ...Source) Option

WithSource with config source.

type Reader added in v1.0.1

type Reader interface {
	Merge(...*Descriptor) error
	Value(string) (Value, bool)
	Source() ([]byte, error)
	Resolve() error
}

Reader is config reader.

type Resolver added in v1.0.1

type Resolver func(map[string]interface{}) error

Resolver resolve placeholder in config.

type Source

type Source interface {
	Load() ([]*Descriptor, error)
	Watch() (Watcher, error)
}

Source is config source.

type Value added in v1.0.1

type Value interface {
	Bool() (bool, error)
	Int() (int64, error)
	Float() (float64, error)
	String() (string, error)
	Duration() (time.Duration, error)
	Slice() ([]Value, error)
	Map() (map[string]Value, error)
	Scan(interface{}) error
	Load() interface{}
	Store(interface{})
}

Value is config value interface.

type Watcher

type Watcher interface {
	Next() ([]*Descriptor, error)
	Stop() error
}

Watcher watches a source for changes.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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