conf

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

README

go-conf

license go-version release Coverage Status

中文

A layered configuration manager. It supports multiple configuration sources (environment variables, command line arguments, and configuration files) and it allows for overriding configuration values at runtime.

  • Layer 1: Properties set by func SetProperty
  • Layer 2: Properties set by local static files
  • Layer 3: Properties set by environment variables
  • Layer 4: Properties set by command line arguments
  • Layer 5: Properties set by remote dynamic sources

The properties in the lower layers are overridden by the higher ones.

Usage
  1. Initialize a configuration manager.
import "github.com/lvan100/go-conf"

c := conf.NewConfiguration()

// Set a property
c.SetProperty("spring.active.profile", "online")

// Add local static files, the properties in the later added files will override
// the properties in the earlier added files.
c.File().Add("testdata/conf.toml", "testdata/conf-${spring.active.profile}.yaml")
c.File().Add("testdata/xxx.yaml", "testdata/conf.json")

// Add remote dynamic files, the properties in the later added files will override
// the properties in the earlier added files.
c.Dync().Add("testdata-${spring.active.profile}/dync.properties")

var p conf.ReadOnlyProperties
p, err = c.Refresh() // You get a merged read-only properties here.
  1. Bind the properties into values.
type SomeOne struct {
    // Age configged by `age` and it has a default value of 16,
    // and it must be between 14 and 18.
    Age int32 `value:"${age:=16}" expr:"$>=14&&$<=18"`
    
    // Name configged by `name`. Because it hasn't a default value,
    // so it must be configged by files or environment variable, and so on.
    Addr string `value:"${addr}"`
    
    Phones []string `value:"${phones}"`
    Grades map[string]int `value:"${grades}"`
}

type Point struct {
	X int32
	Y int32
}

type Tomor struct {

    // SomeOne its all fields will be configged by the prefix `someone`. 
    SomeOne `value:"${someone}"`
    
    // Another its all fields will be configged without a prefix, because
    // there isn't a `value` tag on the field. 
    Another SomeOne
    
    // Points configged by `points` and it has a splitter `PointSplitter`
    // waht splits the string `(1,2)(3,4)` to slice `[]Point{{1,2},{3,4}}`.
    Points []Point `value:"${points}>>PointSplitter"`
}

var obj Tomor
p.Bind(&obj) // all fields has no prefix.

var obj Tomor
p.Bind(&obj, conf.Key("conf")) // all fields has a prefix `conf`.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotExist      = conf.ErrNotExist
	ErrInvalidSyntax = conf.ErrInvalidSyntax
)

Functions

func Def

func Def(v string) conf.GetOption

Def used to set default value for conf.Get().

func Key

func Key(key string) conf.BindArg

Key binds properties using a key for conf.Bind().

func Param

func Param(param BindParam) conf.BindArg

Param binds properties using BindParam for conf.Bind().

func RegisterConverter

func RegisterConverter(fn Converter)

RegisterConverter registers its converter for non-primitive type such as time.Time, time.Duration, or other user-defined value type.

func RegisterReader

func RegisterReader(r Reader, ext ...string)

RegisterReader registers its Reader for some kind of file extension.

func RegisterSplitter

func RegisterSplitter(name string, fn Splitter)

RegisterSplitter registers a Splitter and named it.

func SetValidator

func SetValidator(i ValidatorInterface)

SetValidator sets the validator.

func Tag

func Tag(tag string) conf.BindArg

Tag binds properties using a tag for conf.Bind().

Types

type BindParam

type BindParam = conf.BindParam

type Configuration

type Configuration = cfgr.Configuration

Configuration is a layered configuration manager.

func NewConfiguration

func NewConfiguration() *Configuration

type Converter

type Converter = conf.Converter

type ParsedTag

type ParsedTag = conf.ParsedTag

func ParseTag

func ParseTag(tag string) (ret ParsedTag, err error)

type Properties added in v0.0.2

type Properties = conf.Properties

Properties is a simple properties implementation.

func New added in v0.0.2

func New() *Properties

type ReadOnlyProperties

type ReadOnlyProperties = cfgr.ReadOnlyProperties

ReadOnlyProperties is the interface for read-only properties.

type Reader

type Reader = conf.Reader

type Splitter

type Splitter = conf.Splitter

type ValidatorInterface

type ValidatorInterface = conf.ValidatorInterface

Directories

Path Synopsis
internal
reader

Jump to

Keyboard shortcuts

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