gconf

package module
v6.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2023 License: Apache-2.0 Imports: 25 Imported by: 7

README

Go Config Build Status GoDoc License

An extensible and powerful go configuration manager supporting Go1.18+, which is inspired by oslo.config, viper and github.com/micro/go-micro/config.

Install

$ go get -u github.com/xgfone/gconf/v6

Features

  • A atomic key-value configuration center.
  • Support kinds of decoders to decode the data from the source.
  • Support to get the configuration data from many data sources.
  • Support to change of the configuration option thread-safely during running.
  • Support to observe the change of the configration options.

Basic

package main

import (
	"fmt"

	"github.com/xgfone/gconf/v6"
)

// Pre-define a set of options.
var opts = []gconf.Opt{
	gconf.BoolOpt("opt1", "opt1 help doc"),
	gconf.StrOpt("opt2", "opt2 help doc").D("default"),
	gconf.IntOpt("opt3", "opt3 help doc").D(123).S("o"), // For short name
	gconf.Int32Opt("opt4", "opt4 help doc").As("opt5"),  // For alias name
	gconf.UintOpt("opt6", "opt6 help doc").D(1).V(gconf.NewIntegerRangeValidator(1, 100)),
	gconf.Float64Opt("opt7", "opt7 help doc").Cli(false),
}

func main() {
	// Register the options.
	gconf.RegisterOpts(opts...)

	// Print the registered options.
	for _, opt := range gconf.GetAllOpts() {
		fmt.Printf("Option: name=%s, value=%v\n", opt.Name, opt.Default)
	}

	// Add the observer to observe the change of the options.
	gconf.Observe(func(optName string, oldValue, newValue interface{}) {
		fmt.Printf("option=%s: %v -> %v\n", optName, oldValue, newValue)
	})

	// Update the value of the option thread-safely during app is running.
	gconf.Set("opt1", true)
	gconf.Set("opt2", "abc")
	gconf.Set("opt3", "456")
	gconf.Set("opt4", 789)
	gconf.Set("opt6", 100)
	gconf.Set("opt7", 1.2)

	// Get the values of the options thread-safely.
	fmt.Println(gconf.Get("opt1"))
	fmt.Println(gconf.Get("opt2"))
	fmt.Println(gconf.Get("opt3"))
	fmt.Println(gconf.Get("opt4"))
	fmt.Println(gconf.Get("opt6"))
	fmt.Println(gconf.Get("opt7"))

	// Output:
	// Option: name=opt1, value=false
	// Option: name=opt2, value=default
	// Option: name=opt3, value=123
	// Option: name=opt4, value=0
	// Option: name=opt6, value=1
	// Option: name=opt7, value=0
	// option=opt1: false -> true
	// option=opt2: default -> abc
	// option=opt3: 123 -> 456
	// option=opt4: 0 -> 789
	// option=opt6: 1 -> 100
	// option=opt7: 0 -> 1.2
	// true
	// abc
	// 456
	// 789
	// 100
	// 1.2

	// TODO ...
}

Option Proxy

package main

import (
	"fmt"
	"time"

	"github.com/xgfone/gconf/v6"
)

func main() {
	// New the proxy of the option, which will new an option and register them,
	// then return the proxy of the option. So you can use the option proxy
	// to update and get the value of the option.
	opt1 := gconf.NewInt("opt1", 111, "opt1 help doc")
	opt2 := gconf.NewDuration("opt2", time.Second, "opt2 help doc")

	// Update the value of the option by the proxy.
	opt1.Set("222")
	opt2.Set("1m")

	// Get the value of the option by the proxy.
	fmt.Println(opt1.Get())
	fmt.Println(opt2.Get())

	// Output:
	// 222
	// 1m0s
}

Option Group

package main

import (
	"fmt"
	"time"

	"github.com/xgfone/gconf/v6"
)

// Pre-define a set of options.
var opts = []gconf.Opt{
	gconf.StrOpt("opt1", "opt1 help doc").D("abc"),
	gconf.IntOpt("opt2", "opt2 help doc").D(123),
}

func main() {
	group1 := gconf.Group("group1")  // New the group "group1"
	group2 := group1.Group("group2") // New the sub-group "group1.group2"

	gconf.RegisterOpts(opts...)  // Register opts
	group1.RegisterOpts(opts...) // Register opts with group1
	group2.RegisterOpts(opts...) // Register opts with group2

	opt3 := group1.NewFloat64("opt3", 1.2, "opt3 help doc")          // For "group1.opt3"
	opt4 := group1.NewDuration("opt4", time.Second, "opt4 help doc") // For "group1.opt4"
	opt5 := group2.NewUint("opt5", 456, "opt5 help doc")             // For "group1.group2.opt5"
	opt6 := group2.NewBool("opt6", false, "opt6 help doc")           // For "group1.group2.opt6"

	/// Update the value of the option thread-safely during app is running.
	//
	// Method 1: Update the value of the option by the full name.
	gconf.Set("opt1", "aaa")
	gconf.Set("opt2", "111")
	gconf.Set("group1.opt1", "bbb")
	gconf.Set("group1.opt2", 222)
	gconf.Set("group1.opt3", 2.4)
	gconf.Set("group1.opt4", "1m")
	gconf.Set("group1.group2.opt1", "ccc")
	gconf.Set("group1.group2.opt2", 333)
	gconf.Set("group1.group2.opt5", 444)
	gconf.Set("group1.group2.opt6", "true")
	//
	// Method 2: Update the value of the option by the group proxy.
	group1.Set("opt1", "bbb")
	group1.Set("opt2", 222)
	group1.Set("opt3", 2.4)
	group1.Set("opt4", "1m")
	group2.Set("opt1", "ccc")
	group2.Set("opt2", 333)
	group2.Set("opt5", 444)
	group2.Set("opt6", "true")
	//
	// Method 3: Update the value of the option by the option proxy.
	opt3.Set(2.4)
	opt4.Set("1m")
	opt5.Set(444)
	opt6.Set("true")

	/// Get the values of the options thread-safely.
	//
	// Method 1: Get the value of the option by the full name.
	gconf.Get("opt1")
	gconf.Get("opt2")
	gconf.Get("group1.opt1")
	gconf.Get("group1.opt2")
	gconf.Get("group1.opt3")
	gconf.Get("group1.opt4")
	gconf.Get("group1.group2.opt1")
	gconf.Get("group1.group2.opt2")
	gconf.Get("group1.group2.opt5")
	gconf.Get("group1.group2.opt6")
	//
	// Method 2: Get the value of the option by the group proxy.
	group1.Get("opt1")
	group1.Get("opt2")
	group1.Get("opt3")
	group1.Get("opt4")
	group2.Get("opt1")
	group2.Get("opt2")
	group2.Get("opt5")
	group2.Get("opt6")
	//
	// Method 3: Get the value of the option by the option proxy.
	opt3.Get()
	opt4.Get()
	opt5.Get()
	opt6.Get()
}

Data Decoder

The data decoder is a function like func(src []byte, dst map[string]interface{}) error, which is used to decode the configration data from the data source.

Config supports three kinds of decoders by default, such as ini, json, yaml, and yml is the alias of yaml. You can customize yourself decoder, then add it into Config, such as Config.AddDecoder("type", NewCustomizedDecoder()).

Data Source

A source is used to read the configuration from somewhere the data is. And it can also watch the change the data.

type Source interface {
	// String is the description of the source, such as "env", "file:/path/to".
	String() string

	// Read reads the source data once, which should not block.
	Read() (DataSet, error)

	// Watch watches the change of the source, then call the callback load.
	//
	// close is used to notice the underlying watcher to close and clean.
	Watch(close <-chan struct{}, load func(DataSet, error) (success bool))
}

You can load lots of sources to update the options. It has implemented the sources based on flag, env, file and url. But you can implement other sources, such as ZooKeeper, ETCD, etc.

package main

import (
	"fmt"
	"time"

	"github.com/xgfone/gconf/v6"
)

// Pre-define a set of options.
var opts = []gconf.Opt{
	gconf.StrOpt("opt1", "opt1 help doc").D("abc"),
	gconf.IntOpt("opt2", "opt2 help doc").D(123),
}

func main() {
	gconf.RegisterOpts(opts...)

	group := gconf.Group("group")
	group.RegisterOpts(opts...)

	// Convert the options to flag.Flag, and parse the CLI arguments with "flag".
	gconf.AddAndParseOptFlag(gconf.Conf)

	// Load the sources "flag" and "env".
	gconf.LoadSource(gconf.NewFlagSource())
	gconf.LoadSource(gconf.NewEnvSource(""))

	// Load and watch the file source.
	configFile := gconf.GetString(gconf.ConfigFileOpt.Name)
	gconf.LoadAndWatchSource(gconf.NewFileSource(configFile))

	for _, opt := range gconf.GetAllOpts() {
		fmt.Printf("%s: %v\n", opt.Name, gconf.Get(opt.Name))
	}

	gconf.Observe(func(optName string, oldValue, newValue interface{}) {
		fmt.Printf("%s: %v -> %v\n", optName, oldValue, newValue)
	})

	time.Sleep(time.Minute)

	// ## Run:
	// $ GROUP_OPT2=456 go run main.go --config-file conf.json
	// config-file: conf.json
	// group.opt1: abc
	// group.opt2: 456
	// opt1: abc
	// opt2: 123
	//
	// ## echo '{"opt1":"aaa","opt2":111,"group":{"opt1":"bbb","opt2": 222}}' > conf.json
	// opt1: abc -> aaa
	// opt2: 123 -> 111
	// group.opt1: abc -> bbb
	// group.opt2: 456 -> 222
	//
	// ## echo '{"opt1":"ccc","opt2":333,"group":{"opt1":"ddd","opt2":444}}' > conf.json
	// opt1: aaa -> ccc
	// opt2: 111 -> 333
	// group.opt1: bbb -> ddd
	// group.opt2: 222 -> 444
	//
	// ## echo '{"opt1":"eee","opt2":555,"group":{"opt1":"fff","opt2":666}}' > conf.json
	// opt1: ccc -> eee
	// opt2: 333 -> 555
	// group.opt1: ddd -> fff
	// group.opt2: 444 -> 666
}

Snapshot & Backup

package main

import (
	"fmt"

	"github.com/xgfone/gconf/v6"
)

// Pre-define a set of options.
var opts = []gconf.Opt{
	gconf.StrOpt("opt1", "opt1 help doc").D("abc"),
	gconf.IntOpt("opt2", "opt2 help doc").D(123),
}

func main() {
	gconf.RegisterOpts(opts...)

	group := gconf.Group("group")
	group.RegisterOpts(opts...)

	// Convert the options to flag.Flag, and parse the CLI arguments with "flag".
	gconf.AddAndParseOptFlag(gconf.Conf)

	// Load the sources "flag" and "env".
	gconf.LoadSource(gconf.NewFlagSource())
	gconf.LoadSource(gconf.NewEnvSource(""))

	// Load and update the configuration from the backup file which will watch
	// the change of all configuration options and write them into the backup
	// file to wait to be loaded when the program starts up next time.
	gconf.LoadBackupFile("config-file.backup")

	fmt.Println(gconf.Get("opt1"))
	fmt.Println(gconf.Get("opt2"))
	fmt.Println(group.Get("opt1"))
	fmt.Println(group.Get("opt2"))

	/// Get the snapshot of all configuration options at any time.
	// generation, snapshots := gconf.Snapshot()
	// fmt.Println(generation, snapshots)

	// $ go run main.go
	// abc
	// 123
	// abc
	// 123
	//
	// $ echo '{"opt1":"aaa","opt2":111,"group":{"opt1":"bbb","opt2":222}}' > config-file.backup
	// $ go run main.go
	// aaa
	// 111
	// bbb
	// 222
}

Documentation

Overview

Package gconf is an extensible and powerful go configuration manager.

Features

  • A atomic key-value configuration center.
  • Support kinds of decoders to decode the data from the source.
  • Support to get the configuration data from many data sources.
  • Support to change of the configuration option thread-safely during running.
  • Support to observe the change of the configration options.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ToBool     = cast.ToBool     // func(interface{}) (bool, error)
	ToInt64    = cast.ToInt64    // func(interface{}) (int64, error)
	ToUint64   = cast.ToUint64   // func(interface{}) (uint64, error)
	ToFloat64  = cast.ToFloat64  // func(interface{}) (float64, error)
	ToString   = cast.ToString   // func(interface{}) (string, error)
	ToDuration = cast.ToDuration // func(interface{}) (time.Duration, error)
	ToTime     = cast.ToTime     // func(interface{}) (time.Time, error)
	ToInt      = toInt           // func(interface{}) (int, error)
	ToInt16    = toInt16         // func(interface{}) (int16, error)
	ToInt32    = toInt32         // func(interface{}) (int32, error)
	ToUint     = toUint          // func(interface{}) (uint, error)
	ToUint16   = toUint16        // func(interface{}) (uint16, error)
	ToUint32   = toUint32        // func(interface{}) (uint32, error)

	// For string type, it will be split by using cast.ToStringSlice.
	ToIntSlice      = toIntSlice      // func(interface{}) ([]int, error)
	ToUintSlice     = toUintSlice     // func(interface{}) ([]uint, error)
	ToFloat64Slice  = toFloat64Slice  // func(interface{}) ([]float64, error)
	ToStringSlice   = toStringSlice   // func(interface{}) ([]string, error)
	ToDurationSlice = toDurationSlice // func(interface{}) ([]time.Duration, error)
)

Some type converters, all of which have a default implementation, but you can reset them to yourself implementations.

View Source
var (
	AddressOrIPSliceValidator = NewAddressOrIPSliceValidator()
	AddressOrIPValidator      = NewAddressOrIPValidator()
	AddressSliceValidator     = NewAddressSliceValidator()
	AddressValidator          = NewAddressValidator()
	EmailSliceValidator       = NewEmailSliceValidator()
	EmailValidator            = NewEmailValidator()
	EmptyStrValidator         = NewEmptyStrValidator()
	IPSliceValidator          = NewIPSliceValidator()
	IPValidator               = NewIPValidator()
	MaybeAddressOrIPValidator = NewMaybeAddressOrIPValidator()
	MaybeAddressValidator     = NewMaybeAddressValidator()
	MaybeEmailValidator       = NewMaybeEmailValidator()
	MaybeIPValidator          = NewMaybeIPValidator()
	MaybeURLValidator         = NewMaybeURLValidator()
	PortValidator             = NewPortValidator()
	StrNotEmptyValidator      = NewStrNotEmptyValidator()
	URLSliceValidator         = NewURLSliceValidator()
	URLValidator              = NewURLValidator()
)

Predefine some constant validators.

View Source
var Conf = New()

Conf is the default global Config.

View Source
var ConfigFileOpt = StrOpt("config-file", "the config file path.")

ConfigFileOpt is the default option for the configuration file.

View Source
var ErrNoDecoder = errors.New("no decoder")

ErrNoDecoder represents the error that there is no decoder.

View Source
var ErrNoOpt = errors.New("no option")

ErrNoOpt represents an error that the option does not exist.

View Source
var VersionOpt = StrOpt("version", "Print the version and exit.").S("v").D("1.0.0")

VersionOpt reprensents a version option.

Functions

func AddAndParseOptFlag

func AddAndParseOptFlag(c *Config, flagSet ...*flag.FlagSet) error

AddAndParseOptFlag is the same as AddOptFlag, but parses the CLI arguments.

Notice: if there is the version flag and it is true, it will print the version and exit.

func AddDecoder

func AddDecoder(_type string, decoder Decoder)

AddDecoder is equal to Conf.AddDecoder(_type, decoder).

func AddOptFlag

func AddOptFlag(c *Config, flagSet ...*flag.FlagSet)

AddOptFlag adds the option to the flagSet, which is flag.CommandLine by default.

Notice: for the slice option, it maybe occur many times, and they are combined with the comma as the string representation of slice. For example,

$APP --slice-opt v1  --slice-opt v2  --slice-opt v3

They are equivalent.

func Get

func Get(name string) interface{}

Get is equal to Conf.Get(name).

func GetBool

func GetBool(name string) bool

GetBool is equal to Conf.GetBool(name).

func GetDuration

func GetDuration(name string) time.Duration

GetDuration is equal to Conf.GetDuration(name).

func GetDurationSlice

func GetDurationSlice(name string) []time.Duration

GetDurationSlice is equal to Conf.GetDurationSlice(name).

func GetFloat64

func GetFloat64(name string) float64

GetFloat64 is equal to Conf.GetFloat64(name).

func GetFloat64Slice

func GetFloat64Slice(name string) []float64

GetFloat64Slice is equal to Conf.GetFloat64Slice(name).

func GetGroupSep

func GetGroupSep() (sep string)

GetGroupSep is equal to Conf.GetGroupSep().

func GetInt

func GetInt(name string) int

GetInt is equal to Conf.GetInt(name).

func GetInt16 added in v6.1.0

func GetInt16(name string) int16

GetInt16 is equal to Conf.GetInt16(name).

func GetInt32

func GetInt32(name string) int32

GetInt32 is equal to Conf.GetInt32(name).

func GetInt64

func GetInt64(name string) int64

GetInt64 is equal to Conf.GetInt64(name).

func GetIntSlice

func GetIntSlice(name string) []int

GetIntSlice is equal to Conf.GetIntSlice(name).

func GetString

func GetString(name string) string

GetString is equal to Conf.GetString(name).

func GetStringSlice

func GetStringSlice(name string) []string

GetStringSlice is equal to Conf.GetStringSlice(name).

func GetTime

func GetTime(name string) time.Time

GetTime is equal to Conf.GetTime(name).

func GetUint

func GetUint(name string) uint

GetUint is equal to Conf.GetUint(name).

func GetUint16 added in v6.1.0

func GetUint16(name string) uint16

GetUint16 is equal to Conf.GetUint16(name).

func GetUint32

func GetUint32(name string) uint32

GetUint32 is equal to Conf.GetUint32(name).

func GetUint64

func GetUint64(name string) uint64

GetUint64 is equal to Conf.GetUint64(name).

func GetUintSlice

func GetUintSlice(name string) []uint

GetUintSlice is equal to Conf.GetUintSlice(name).

func LoadAndWatchSource

func LoadAndWatchSource(source Source, force ...bool) error

LoadAndWatchSource is equal to Conf.LoadAndWatchSource(source, force...).

func LoadBackupFile

func LoadBackupFile(filename string) error

LoadBackupFile is equal to Conf.LoadBackupFile().

func LoadDataSet

func LoadDataSet(ds DataSet, force ...bool) error

LoadDataSet is equal to Conf.LoadDataSet(source, force...).

func LoadMap

func LoadMap(options map[string]interface{}, force ...bool) error

LoadMap is equal to Conf.LoadMap(options, force...).

func LoadSource

func LoadSource(source Source, force ...bool) error

LoadSource is equal to Conf.LoadSource(source, force...).

func Must

func Must(name string) interface{}

Must is equal to Conf.Must(name).

func Observe

func Observe(observers ...Observer)

Observe is equal to Conf.Observe(observers...).

func PrintFlagUsage

func PrintFlagUsage(flagSet *flag.FlagSet)

PrintFlagUsage prints the flag usage instead of the default.

func RegisterOpts

func RegisterOpts(opts ...Opt)

RegisterOpts is equal to Conf.RegisterOpts(opts...).

func Set

func Set(name string, value interface{}) error

Set is equal to Conf.Set(name, value).

func SetVersion

func SetVersion(version string)

SetVersion is equal to Conf.SetVersion(version).

func Snapshot

func Snapshot() (generation uint64, snap map[string]interface{})

Snapshot is equal to Conf.Snapshot().

func UnregisterOpts

func UnregisterOpts(optNames ...string)

UnregisterOpts is equal to Conf.UnregisterOpts(optNames...).

Types

type Config

type Config struct {
	// Args is the CLI rest arguments.
	//
	// Default: nil
	Args []string

	// Version is the version of the application, which is used by CLI.
	Version Opt

	// Errorf is used to log the error.
	//
	// Default: log.Printf
	Errorf func(format string, args ...interface{})
	// contains filtered or unexported fields
}

Config is used to manage the configuration options.

func New

func New() *Config

New returns a new Config with the "json", "yaml/yml" and "ini" decoder.

func (*Config) AddDecoder

func (c *Config) AddDecoder(_type string, decoder Decoder)

AddDecoder adds a decoder, which will override it if it has been added.s

func (*Config) AddDecoderTypeAliases

func (c *Config) AddDecoderTypeAliases(_type string, aliases ...string)

AddDecoderTypeAliases adds the aliases of the decoder typed _type.

For example,

c.AddDecoderTypeAliases("yaml", "yml")

When acquiring the "yml" decoder and it does not exist, it will try to return the "yaml" decoder.

func (*Config) Get

func (c *Config) Get(name string) (value interface{})

Get returns the value of the option named name.

Return nil if this option does not exist.

func (*Config) GetAllOpts

func (c *Config) GetAllOpts() []Opt

GetAllOpts returns all the registered options.

func (*Config) GetBool

func (c *Config) GetBool(name string) bool

GetBool returns the value of the option named name as bool.

func (*Config) GetDecoder

func (c *Config) GetDecoder(_type string) (decoder Decoder)

GetDecoder returns the decoder by the type.

Return nil if the decoder does not exist.

func (*Config) GetDuration

func (c *Config) GetDuration(name string) time.Duration

GetDuration returns the value of the option named name as time.Duration.

func (*Config) GetDurationSlice

func (c *Config) GetDurationSlice(name string) []time.Duration

GetDurationSlice returns the value of the option named name as []time.Duration.

func (*Config) GetFloat64

func (c *Config) GetFloat64(name string) float64

GetFloat64 returns the value of the option named name as float64.

func (*Config) GetFloat64Slice

func (c *Config) GetFloat64Slice(name string) []float64

GetFloat64Slice returns the value of the option named name as []float64.

func (*Config) GetGroupSep

func (c *Config) GetGroupSep() (sep string)

GetGroupSep returns the separator between the option groups.

func (*Config) GetInt

func (c *Config) GetInt(name string) int

GetInt returns the value of the option named name as int.

func (*Config) GetInt16 added in v6.1.0

func (c *Config) GetInt16(name string) int16

GetInt16 returns the value of the option named name as int16.

func (*Config) GetInt32

func (c *Config) GetInt32(name string) int32

GetInt32 returns the value of the option named name as int32.

func (*Config) GetInt64

func (c *Config) GetInt64(name string) int64

GetInt64 returns the value of the option named name as int64.

func (*Config) GetIntSlice

func (c *Config) GetIntSlice(name string) []int

GetIntSlice returns the value of the option named name as []int.

func (*Config) GetOpt

func (c *Config) GetOpt(name string) (opt Opt, ok bool)

GetOpt returns the registered option by the name.

func (*Config) GetString

func (c *Config) GetString(name string) string

GetString returns the value of the option named name as string.

func (*Config) GetStringSlice

func (c *Config) GetStringSlice(name string) []string

GetStringSlice returns the value of the option named name as []string.

func (*Config) GetTime

func (c *Config) GetTime(name string) time.Time

GetTime returns the value of the option named name as time.Time.

func (*Config) GetUint

func (c *Config) GetUint(name string) uint

GetUint returns the value of the option named name as uint.

func (*Config) GetUint16 added in v6.1.0

func (c *Config) GetUint16(name string) uint16

GetUint16 returns the value of the option named name as uint16.

func (*Config) GetUint32

func (c *Config) GetUint32(name string) uint32

GetUint32 returns the value of the option named name as uint32.

func (*Config) GetUint64

func (c *Config) GetUint64(name string) uint64

GetUint64 returns the value of the option named name as uint64.

func (*Config) GetUintSlice

func (c *Config) GetUintSlice(name string) []uint

GetUintSlice returns the value of the option named name as []uint.

func (*Config) Group

func (c *Config) Group(name string) *OptGroup

Group returns the option group with the group name.

func (*Config) HasOpt

func (c *Config) HasOpt(name string) (yes bool)

HasOpt reports whether the option named name has been registered.

func (*Config) IgnoreNoOptError

func (c *Config) IgnoreNoOptError(ignore bool)

IgnoreNoOptError sets whether to ignore the error NoOptError when updating the value of the option.

func (*Config) LoadAndWatchSource

func (c *Config) LoadAndWatchSource(source Source, force ...bool) (err error)

LoadAndWatchSource is the same as LoadSource, but also watches the source after loading the source successfully.

func (*Config) LoadBackupFile

func (c *Config) LoadBackupFile(filename string) (err error)

LoadBackupFile loads configuration data from the backup file if exists, then watches the change of the options and write them into the file. So you can use it as the local cache.

func (*Config) LoadDataSet

func (c *Config) LoadDataSet(ds DataSet, force ...bool) (err error)

LoadDataSet loads the DataSet ds, which will parse the data by calling the corresponding decoder and load it.

If failing to parse the value of any option, it terminates to parse and load it.

If force is missing or false, ignore the assigned options.

func (*Config) LoadMap

func (c *Config) LoadMap(options map[string]interface{}, force ...bool) error

LoadMap updates a set of the options together, but terminates to parse and load all if failing to parse the value of any option.

If force is missing or false, ignore the assigned options.

func (*Config) LoadSource

func (c *Config) LoadSource(source Source, force ...bool) (err error)

LoadSource loads the options from the given source.

If force is missing or false, ignore the assigned options.

func (*Config) Must

func (c *Config) Must(name string) (value interface{})

Must is the same as Get, but panic if the returned value is nil.

func (*Config) NewBool

func (c *Config) NewBool(name string, _default bool, help string) *OptProxyBool

NewBool creates and registers a bool option, then returns its proxy.

func (*Config) NewDuration

func (c *Config) NewDuration(name string, _default time.Duration, help string) *OptProxyDuration

NewDuration creates and registers a time.Duration option, then returns its proxy.

func (*Config) NewDurationSlice

func (c *Config) NewDurationSlice(name string, _default []time.Duration, help string) *OptProxyDurationSlice

NewDurationSlice creates and registers a []time.Duration option, then returns its proxy.

func (*Config) NewFloat64

func (c *Config) NewFloat64(name string, _default float64, help string) *OptProxyFloat64

NewFloat64 creates and registers a float64 option, then returns its proxy.

func (*Config) NewFloat64Slice

func (c *Config) NewFloat64Slice(name string, _default []float64, help string) *OptProxyFloat64Slice

NewFloat64Slice creates and registers a []float64 option, then returns its proxy.

func (*Config) NewInt

func (c *Config) NewInt(name string, _default int, help string) *OptProxyInt

NewInt creates and registers an int option, then returns its proxy.

func (*Config) NewInt16 added in v6.1.0

func (c *Config) NewInt16(name string, _default int16, help string) *OptProxyInt16

NewInt16 creates and registers a int16 option, then returns its proxy.

func (*Config) NewInt32

func (c *Config) NewInt32(name string, _default int32, help string) *OptProxyInt32

NewInt32 creates and registers a int32 option, then returns its proxy.

func (*Config) NewInt64

func (c *Config) NewInt64(name string, _default int64, help string) *OptProxyInt64

NewInt64 creates and registers a int64 option, then returns its proxy.

func (*Config) NewIntSlice

func (c *Config) NewIntSlice(name string, _default []int, help string) *OptProxyIntSlice

NewIntSlice creates and registers a []int option, then returns its proxy.

func (*Config) NewOptProxy

func (c *Config) NewOptProxy(opt Opt) OptProxy

NewOptProxy registers the option and returns a new proxy of the option.

func (*Config) NewString

func (c *Config) NewString(name, _default, help string) *OptProxyString

NewString creates and registers a string option, then returns its proxy.

func (*Config) NewStringSlice

func (c *Config) NewStringSlice(name string, _default []string, help string) *OptProxyStringSlice

NewStringSlice creates and registers a []string option, then returns its proxy.

func (*Config) NewTime

func (c *Config) NewTime(name string, _default time.Time, help string) *OptProxyTime

NewTime creates and registers a time.Time option, then returns its proxy.

func (*Config) NewUint

func (c *Config) NewUint(name string, _default uint, help string) *OptProxyUint

NewUint creates and registers a uint option, then returns its proxy.

func (*Config) NewUint16 added in v6.1.0

func (c *Config) NewUint16(name string, _default uint16, help string) *OptProxyUint16

NewUint16 creates and registers a uint16 option, then returns its proxy.

func (*Config) NewUint32

func (c *Config) NewUint32(name string, _default uint32, help string) *OptProxyUint32

NewUint32 creates and registers a uint32 option, then returns its proxy.

func (*Config) NewUint64

func (c *Config) NewUint64(name string, _default uint64, help string) *OptProxyUint64

NewUint64 creates and registers a uint64 option, then returns its proxy.

func (*Config) NewUintSlice

func (c *Config) NewUintSlice(name string, _default []uint, help string) *OptProxyUintSlice

NewUintSlice creates and registers a []uint option, then returns its proxy.

func (*Config) Observe

func (c *Config) Observe(observers ...Observer)

Observe appends the observers to watch the change of all the option values.

func (*Config) OptIsSet

func (c *Config) OptIsSet(name string) (yes bool)

OptIsSet reports whether the option named name is set.

Return false if the option does not exist.

func (*Config) Parse

func (c *Config) Parse(name string, value interface{}) (interface{}, error)

Parse parses the option value named name, and returns it.

func (*Config) RegisterOpts

func (c *Config) RegisterOpts(opts ...Opt)

RegisterOpts registers a set of options.

Notice: if a certain option has existed, it will panic.

func (*Config) Set

func (c *Config) Set(name string, value interface{}) (err error)

Set is used to reset the option named name to value.

func (*Config) SetVersion

func (c *Config) SetVersion(version string)

SetVersion sets the version information.

func (*Config) Snapshot

func (c *Config) Snapshot() (generation uint64, snap map[string]interface{})

Snapshot returns the snapshot of all the options and its generation which will increase with 1 each time any option value is changed.

For example,

map[string]interface{} {
    "opt1": "value1",
    "opt2": "value2",
    "group1.opt3": "value3",
    "group1.group2.opt4": "value4",
    // ...
}

func (*Config) Stop

func (c *Config) Stop()

Stop stops the watchers of all the sources.

func (*Config) UnregisterOpts

func (c *Config) UnregisterOpts(optNames ...string)

UnregisterOpts unregisters the registered options.

type DataSet

type DataSet struct {
	Args      []string  // The CLI arguments filled by the CLI source such as flag.
	Data      []byte    // The original data.
	Format    string    // Such as "json", "xml", etc.
	Source    string    // Such as "file:/path/to/file", "zk:127.0.0.1:2181", etc.
	Checksum  string    // Such as "md5:7d2f31e6fff478337478413ee1b70d2a", etc.
	Timestamp time.Time // The timestamp when the data is modified.
}

DataSet represents the information of the configuration data.

func (DataSet) Md5

func (ds DataSet) Md5() string

Md5 returns the md5 checksum of the DataSet data

func (DataSet) Sha256

func (ds DataSet) Sha256() string

Sha256 returns the sha256 checksum of the DataSet data

type Decoder

type Decoder func(src []byte, dst map[string]interface{}) error

Decoder is used to decode the configuration data.

func NewIniDecoder

func NewIniDecoder(defaultGroupName ...string) Decoder

NewIniDecoder returns a INI decoder to decode the INI data.

Notice:

  1. The empty line will be ignored.
  2. The spacewhite on the beginning and end of line or value will be trimmed.
  3. The comment line starts with the character '#' or ';', which is ignored.
  4. The name of the default group is "DEFAULT", but it is optional.
  5. The group can nest other groups by ".", such as "group1.group2.group3".
  6. The key must only contain the printable non-spacewhite characters.
  7. The line can continue to the next line with the last character "\", and the spacewhite on the beginning and end of the each line will be trimmed, then combines them with a space.

func NewJSONDecoder

func NewJSONDecoder() Decoder

NewJSONDecoder returns a json decoder to decode the json data.

If the json data contains the comment line starting with "//", it will remove the comment line and parse the json data.

Example
data := []byte(`{
		// user name
		"name": "Aaron",
		"age": 123,

		// the other information
		"other": {
			// address
			"home": "http://www.example.com"
		}
	}`)

ms := make(map[string]interface{})
err := NewJSONDecoder()(data, ms)

fmt.Println(err)
fmt.Println(len(ms))
fmt.Println(ms["name"])
fmt.Println(ms["age"])
fmt.Println(ms["other"])
Output:

<nil>
3
Aaron
123
map[home:http://www.example.com]

type Observer

type Observer func(optName string, oldValue, newValue interface{})

Observer is used to observe the change of the option value.

type Opt

type Opt struct {
	// Name is the long name of the option, which should be lower case.
	//
	// Required!
	Name string

	// Default is the default value of the option, which will be used to
	// indicate the type of the option.
	//
	// Required!
	Default interface{}

	// Parser is used to parse the input option value to a specific type.
	//
	// Required!
	Parser Parser

	// Short is the short name of the option, which should be a single-
	// character string, such as "v" for "version".
	//
	// Optional?
	Short string

	// Help is the help or usage information.
	//
	// Optional?
	Help string

	// IsCli indicates whether the option can be used for the CLI flag.
	//
	// Optional?
	IsCli bool

	// The list of the aliases of the option.
	//
	// Optional?
	Aliases []string

	// Validators is used to validate whether the option value is valid
	// after parsing it and before updating it.
	//
	// Optional?
	Validators []Validator

	// OnUpdate is called when the option value is updated.
	OnUpdate func(oldValue, newValue interface{})
}

Opt is used to represent a option vlaue.

func BoolOpt

func BoolOpt(name string, help string) Opt

BoolOpt is the same NewOpt, but uses ToBool to parse the value as bool.

func DurationOpt

func DurationOpt(name string, help string) Opt

DurationOpt is the same NewOpt, but uses ToDuration to parse the value as time.Duration.

func DurationSliceOpt

func DurationSliceOpt(name string, help string) Opt

DurationSliceOpt is the same NewOpt, but uses ToDurationSlice to parse the value as []time.Duration.

func Float64Opt

func Float64Opt(name string, help string) Opt

Float64Opt is the same NewOpt, but uses ToFloat64 to parse the value as float64.

func Float64SliceOpt

func Float64SliceOpt(name string, help string) Opt

Float64SliceOpt is the same NewOpt, but uses ToFloat64Slice to parse the value as []float64.

func GetAllOpts

func GetAllOpts() []Opt

GetAllOpts is equal to Conf.GetAllOpts().

func Int16Opt added in v6.1.0

func Int16Opt(name string, help string) Opt

Int16Opt is the same NewOpt, but uses ToInt16 to parse the value as int16.

func Int32Opt

func Int32Opt(name string, help string) Opt

Int32Opt is the same NewOpt, but uses ToInt32 to parse the value as int32.

func Int64Opt

func Int64Opt(name string, help string) Opt

Int64Opt is the same NewOpt, but uses ToInt64 to parse the value as int64.

func IntOpt

func IntOpt(name string, help string) Opt

IntOpt is the same NewOpt, but uses ToInt to parse the value as int.

func IntSliceOpt

func IntSliceOpt(name string, help string) Opt

IntSliceOpt is the same NewOpt, but uses ToIntSlice to parse the value as []int.

func NewOpt

func NewOpt(name, help string, _default interface{}, parser Parser) Opt

NewOpt returns a new Opt that IsCli is true.

func StrOpt

func StrOpt(name string, help string) Opt

StrOpt is the same NewOpt, but uses ToString to parse the value as string.

func StrSliceOpt

func StrSliceOpt(name string, help string) Opt

StrSliceOpt is the same NewOpt, but uses ToStringSlice to parse the value as []string.

func TimeOpt

func TimeOpt(name string, help string) Opt

TimeOpt is the same NewOpt, but uses ToTime to parse the value as time.Time.

func Uint16Opt added in v6.1.0

func Uint16Opt(name string, help string) Opt

Uint16Opt is the same NewOpt, but uses ToUint16 to parse the value as uint16.

func Uint32Opt

func Uint32Opt(name string, help string) Opt

Uint32Opt is the same NewOpt, but uses ToUint32 to parse the value as uint32.

func Uint64Opt

func Uint64Opt(name string, help string) Opt

Uint64Opt is the same NewOpt, but uses ToUint64 to parse the value as uint64.

func UintOpt

func UintOpt(name string, help string) Opt

UintOpt is the same NewOpt, but uses ToUint to parse the value as uint.

func UintSliceOpt

func UintSliceOpt(name string, help string) Opt

UintSliceOpt is the same NewOpt, but uses ToUintSlice to parse the value as []uint.

func (Opt) As

func (o Opt) As(aliases ...string) Opt

As returns a new Opt with the new aliases based on the current option, which will append them.

func (Opt) Cli

func (o Opt) Cli(cli bool) Opt

Cli returns a new Opt with the cli flag based on the current option.

func (Opt) D

func (o Opt) D(_default interface{}) Opt

D returns a new Opt with the given default value based on the current option.

func (Opt) H

func (o Opt) H(help string) Opt

H returns a new Opt with the given help based on the current option.

func (Opt) N

func (o Opt) N(name string) Opt

N returns a new Opt with the given name based on the current option.

func (Opt) P

func (o Opt) P(parser Parser) Opt

P returns a new Opt with the given parser based on the current option.

func (Opt) S

func (o Opt) S(shortName string) Opt

S returns a new Opt with the given short name based on the current option.

func (Opt) U

func (o Opt) U(callback func(oldValue, newValue interface{})) Opt

U returns a new Opt with the update callback function on the current option.

func (Opt) V

func (o Opt) V(validators ...Validator) Opt

V returns a new Opt with the given validators based on the current option, which will append them.

type OptGroup

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

OptGroup is the proxy for a group of options.

func Group

func Group(name string) *OptGroup

Group is equal to Conf.Group(name).

func (*OptGroup) Get

func (g *OptGroup) Get(name string) interface{}

Get returns the value of the option named name.

Return nil if this option does not exist.

func (*OptGroup) GetAllOpts added in v6.2.0

func (g *OptGroup) GetAllOpts() []Opt

GetAllOpts returns all the registered options in the group.

func (*OptGroup) GetBool

func (g *OptGroup) GetBool(name string) bool

GetBool returns the value of the option named name as bool.

func (*OptGroup) GetDuration

func (g *OptGroup) GetDuration(name string) time.Duration

GetDuration returns the value of the option named name as time.Duration.

func (*OptGroup) GetDurationSlice

func (g *OptGroup) GetDurationSlice(name string) []time.Duration

GetDurationSlice returns the value of the option named name as []time.Duration.

func (*OptGroup) GetFloat64

func (g *OptGroup) GetFloat64(name string) float64

GetFloat64 returns the value of the option named name as float64.

func (*OptGroup) GetFloat64Slice

func (g *OptGroup) GetFloat64Slice(name string) []float64

GetFloat64Slice returns the value of the option named name as []float64.

func (*OptGroup) GetInt

func (g *OptGroup) GetInt(name string) int

GetInt returns the value of the option named name as int.

func (*OptGroup) GetInt16 added in v6.1.0

func (g *OptGroup) GetInt16(name string) int16

GetInt16 returns the value of the option named name as int16.

func (*OptGroup) GetInt32

func (g *OptGroup) GetInt32(name string) int32

GetInt32 returns the value of the option named name as int32.

func (*OptGroup) GetInt64

func (g *OptGroup) GetInt64(name string) int64

GetInt64 returns the value of the option named name as int64.

func (*OptGroup) GetIntSlice

func (g *OptGroup) GetIntSlice(name string) []int

GetIntSlice returns the value of the option named name as []int.

func (*OptGroup) GetOpt added in v6.2.0

func (g *OptGroup) GetOpt(name string) (opt Opt, ok bool)

GetOpt returns the registered option by the name from the current group.

func (*OptGroup) GetString

func (g *OptGroup) GetString(name string) string

GetString returns the value of the option named name as string.

func (*OptGroup) GetStringSlice

func (g *OptGroup) GetStringSlice(name string) []string

GetStringSlice returns the value of the option named name as []string.

func (*OptGroup) GetTime

func (g *OptGroup) GetTime(name string) time.Time

GetTime returns the value of the option named name as time.Time.

func (*OptGroup) GetUint

func (g *OptGroup) GetUint(name string) uint

GetUint returns the value of the option named name as uint.

func (*OptGroup) GetUint16 added in v6.1.0

func (g *OptGroup) GetUint16(name string) uint16

GetUint16 returns the value of the option named name as uint16.

func (*OptGroup) GetUint32

func (g *OptGroup) GetUint32(name string) uint32

GetUint32 returns the value of the option named name as uint32.

func (*OptGroup) GetUint64

func (g *OptGroup) GetUint64(name string) uint64

GetUint64 returns the value of the option named name as uint64.

func (*OptGroup) GetUintSlice

func (g *OptGroup) GetUintSlice(name string) []uint

GetUintSlice returns the value of the option named name as []uint.

func (*OptGroup) Group

func (g *OptGroup) Group(name string) *OptGroup

Group returns a sub-group with the group name.

func (*OptGroup) HasOpt added in v6.2.0

func (g *OptGroup) HasOpt(name string) (yes bool)

HasOpt reports whether the option named name has been registered into the group.

func (*OptGroup) Must

func (g *OptGroup) Must(name string) interface{}

Must is the same as Get, but panic if the returned value is nil.

func (*OptGroup) NewBool

func (g *OptGroup) NewBool(name string, _default bool, help string) *OptProxyBool

NewBool creates and registers a bool option, then returns its proxy.

func (*OptGroup) NewDuration

func (g *OptGroup) NewDuration(name string, _default time.Duration, help string) *OptProxyDuration

NewDuration creates and registers a time.Duration option, then returns its proxy.

func (*OptGroup) NewDurationSlice

func (g *OptGroup) NewDurationSlice(name string, _default []time.Duration, help string) *OptProxyDurationSlice

NewDurationSlice creates and registers a []time.Duration option, then returns its proxy.

func (*OptGroup) NewFloat64

func (g *OptGroup) NewFloat64(name string, _default float64, help string) *OptProxyFloat64

NewFloat64 creates and registers a float64 option, then returns its proxy.

func (*OptGroup) NewFloat64Slice

func (g *OptGroup) NewFloat64Slice(name string, _default []float64, help string) *OptProxyFloat64Slice

NewFloat64Slice creates and registers a []float64 option, then returns its proxy.

func (*OptGroup) NewInt

func (g *OptGroup) NewInt(name string, _default int, help string) *OptProxyInt

NewInt creates and registers an int option, then returns its proxy.

func (*OptGroup) NewInt16 added in v6.1.0

func (g *OptGroup) NewInt16(name string, _default int16, help string) *OptProxyInt16

NewInt16 creates and registers a int16 option, then returns its proxy.

func (*OptGroup) NewInt32

func (g *OptGroup) NewInt32(name string, _default int32, help string) *OptProxyInt32

NewInt32 creates and registers a int32 option, then returns its proxy.

func (*OptGroup) NewInt64

func (g *OptGroup) NewInt64(name string, _default int64, help string) *OptProxyInt64

NewInt64 creates and registers a int64 option, then returns its proxy.

func (*OptGroup) NewIntSlice

func (g *OptGroup) NewIntSlice(name string, _default []int, help string) *OptProxyIntSlice

NewIntSlice creates and registers a []int option, then returns its proxy.

func (*OptGroup) NewOptProxy

func (g *OptGroup) NewOptProxy(opt Opt) OptProxy

NewOptProxy registers the option and returns a new proxy of the option.

func (*OptGroup) NewString

func (g *OptGroup) NewString(name, _default, help string) *OptProxyString

NewString creates and registers a string option, then returns its proxy.

func (*OptGroup) NewStringSlice

func (g *OptGroup) NewStringSlice(name string, _default []string, help string) *OptProxyStringSlice

NewStringSlice creates and registers a []string option, then returns its proxy.

func (*OptGroup) NewTime

func (g *OptGroup) NewTime(name string, _default time.Time, help string) *OptProxyTime

NewTime creates and registers a time.Time option, then returns its proxy.

func (*OptGroup) NewUint

func (g *OptGroup) NewUint(name string, _default uint, help string) *OptProxyUint

NewUint is equal to Conf.NewUint(name, _default, help).

func (*OptGroup) NewUint16 added in v6.1.0

func (g *OptGroup) NewUint16(name string, _default uint16, help string) *OptProxyUint16

NewUint16 creates and registers a uint16 option, then returns its proxy.

func (*OptGroup) NewUint32

func (g *OptGroup) NewUint32(name string, _default uint32, help string) *OptProxyUint32

NewUint32 creates and registers a uint32 option, then returns its proxy.

func (*OptGroup) NewUint64

func (g *OptGroup) NewUint64(name string, _default uint64, help string) *OptProxyUint64

NewUint64 creates and registers a uint64 option, then returns its proxy.

func (*OptGroup) NewUintSlice

func (g *OptGroup) NewUintSlice(name string, _default []uint, help string) *OptProxyUintSlice

NewUintSlice creates and registers a []uint option, then returns its proxy.

func (*OptGroup) OptIsSet added in v6.2.0

func (g *OptGroup) OptIsSet(name string) (yes bool)

OptIsSet reports whether the option named name in the group is set.

Return false if the option does not exist.

func (*OptGroup) Prefix

func (g *OptGroup) Prefix() string

Prefix returns the prefix of the group.

func (*OptGroup) RegisterOpts

func (g *OptGroup) RegisterOpts(opts ...Opt)

RegisterOpts registers a set of options.

Notice: if a certain option has existed, it will panic.

func (*OptGroup) SelfBool

func (g *OptGroup) SelfBool(help string) *OptProxyBool

SelfBool makes itself into a bool option proxy with the default value "false".

func (*OptGroup) Set

func (g *OptGroup) Set(name string, value interface{}) error

Set resets the option named name to value.

func (*OptGroup) UnregisterOpts

func (g *OptGroup) UnregisterOpts(optNames ...string)

UnregisterOpts unregisters the registered options.

type OptProxy

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

OptProxy is a proxy for the option registered into Config, which can be used to modify the attributions of the option and update its value directly.

func NewOptProxy

func NewOptProxy(c *Config, opt Opt) OptProxy

NewOptProxy registers the option into c and returns a proxy of opt.

func (*OptProxy) Aliases

func (o *OptProxy) Aliases(aliases ...string) *OptProxy

Aliases appends the aliases of the option and returns itself.

func (*OptProxy) Default

func (o *OptProxy) Default(_default interface{}) *OptProxy

Default resets the default value of the option and returns itself.

func (*OptProxy) Get

func (o *OptProxy) Get() interface{}

Get returns the value of the option.

func (*OptProxy) IsCli

func (o *OptProxy) IsCli(cli bool) *OptProxy

IsCli resets the cli flag of the option and returns itself.

func (*OptProxy) Name

func (o *OptProxy) Name() string

Name returns the name of the option.

func (*OptProxy) OnUpdate

func (o *OptProxy) OnUpdate(callback func(old, new interface{})) *OptProxy

OnUpdate resets the update callback function of the option and returns itself.

func (*OptProxy) Opt

func (o *OptProxy) Opt() Opt

Opt returns the registered and proxied option.

func (*OptProxy) Parser

func (o *OptProxy) Parser(parser Parser) *OptProxy

Parser resets the parser of the option and returns itself.

func (*OptProxy) Set

func (o *OptProxy) Set(value interface{}) (err error)

Set sets the value of the option to value.

func (*OptProxy) Short

func (o *OptProxy) Short(short string) *OptProxy

Short resets the short name of the option and returns itself.

func (OptProxy) ToBool added in v6.1.0

func (o OptProxy) ToBool() *OptProxyBool

ToBool converts option from OptProxy to OptProxyBool.

func (OptProxy) ToDuration added in v6.1.0

func (o OptProxy) ToDuration() *OptProxyDuration

ToDuration converts option from OptProxy to OptProxyDuration.

func (OptProxy) ToDurationSlice added in v6.1.0

func (o OptProxy) ToDurationSlice() *OptProxyDurationSlice

ToDurationSlice converts option from OptProxy to OptProxyDurationSlice.

func (OptProxy) ToFloat64 added in v6.1.0

func (o OptProxy) ToFloat64() *OptProxyFloat64

ToFloat64 converts option from OptProxy to OptProxyFloat64.

func (OptProxy) ToFloat64Slice added in v6.1.0

func (o OptProxy) ToFloat64Slice() *OptProxyFloat64Slice

ToFloat64Slice converts option from OptProxy to OptProxyFloat64Slice.

func (OptProxy) ToInt added in v6.1.0

func (o OptProxy) ToInt() *OptProxyInt

ToInt converts option from OptProxy to OptProxyInt.

func (OptProxy) ToInt16 added in v6.1.0

func (o OptProxy) ToInt16() *OptProxyInt16

ToInt16 converts option from OptProxy to OptProxyInt16.

func (OptProxy) ToInt32 added in v6.1.0

func (o OptProxy) ToInt32() *OptProxyInt32

ToInt32 converts option from OptProxy to OptProxyInt32.

func (OptProxy) ToInt64 added in v6.1.0

func (o OptProxy) ToInt64() *OptProxyInt64

ToInt64 converts option from OptProxy to OptProxyInt64.

func (OptProxy) ToIntSlice added in v6.1.0

func (o OptProxy) ToIntSlice() *OptProxyIntSlice

ToIntSlice converts option from OptProxy to OptProxyIntSlice.

func (OptProxy) ToString added in v6.1.0

func (o OptProxy) ToString() *OptProxyString

ToString converts option from OptProxy to OptProxyString.

func (OptProxy) ToStringSlice added in v6.1.0

func (o OptProxy) ToStringSlice() *OptProxyStringSlice

ToStringSlice converts option from OptProxy to OptProxyStringSlice.

func (OptProxy) ToTime added in v6.1.0

func (o OptProxy) ToTime() *OptProxyTime

ToTime converts option from OptProxy to OptProxyTime.

func (OptProxy) ToUint added in v6.1.0

func (o OptProxy) ToUint() *OptProxyUint

ToUint converts option from OptProxy to OptProxyUint.

func (OptProxy) ToUint16 added in v6.1.0

func (o OptProxy) ToUint16() *OptProxyUint16

ToUint16 converts option from OptProxy to OptProxyUint16.

func (OptProxy) ToUint32 added in v6.1.0

func (o OptProxy) ToUint32() *OptProxyUint32

ToUint32 converts option from OptProxy to OptProxyUint32.

func (OptProxy) ToUint64 added in v6.1.0

func (o OptProxy) ToUint64() *OptProxyUint64

ToUint64 converts option from OptProxy to OptProxyUint64.

func (OptProxy) ToUintSlice added in v6.1.0

func (o OptProxy) ToUintSlice() *OptProxyUintSlice

ToUintSlice converts option from OptProxy to OptProxyUintSlice.

func (*OptProxy) Validators

func (o *OptProxy) Validators(validators ...Validator) *OptProxy

Validators appends the validators of the option and returns itself.

type OptProxyBool

type OptProxyBool struct{ OptProxy }

OptProxyBool is a proxy for the bool option registered into Config, which can be used to modify the attributions of the option and update its value directly.

func NewBool

func NewBool(name string, _default bool, help string) *OptProxyBool

NewBool is equal to Conf.NewBool(name, _default, help).

func (*OptProxyBool) Aliases

func (o *OptProxyBool) Aliases(aliases ...string) *OptProxyBool

Aliases appends the aliases of the option and returns itself.

func (*OptProxyBool) Default

func (o *OptProxyBool) Default(_default interface{}) *OptProxyBool

Default resets the default value of the option and returns itself.

func (*OptProxyBool) Get

func (o *OptProxyBool) Get() bool

Get returns the value of the option.

func (*OptProxyBool) IsCli

func (o *OptProxyBool) IsCli(cli bool) *OptProxyBool

IsCli resets the cli flag of the option and returns itself.

func (*OptProxyBool) Name

func (o *OptProxyBool) Name() string

Name returns the name of the option.

func (*OptProxyBool) OnUpdate

func (o *OptProxyBool) OnUpdate(f func(old, new interface{})) *OptProxyBool

OnUpdate resets the update callback of the option and returns itself.

func (*OptProxyBool) Opt

func (o *OptProxyBool) Opt() Opt

Opt returns the registered and proxied option.

func (*OptProxyBool) Parser

func (o *OptProxyBool) Parser(parser Parser) *OptProxyBool

Parser resets the parser of the option and returns itself.

func (*OptProxyBool) Set

func (o *OptProxyBool) Set(value interface{}) (err error)

Set sets the value of the option to value.

func (*OptProxyBool) Short

func (o *OptProxyBool) Short(short string) *OptProxyBool

Short resets the short name of the option and returns itself.

func (*OptProxyBool) Validators

func (o *OptProxyBool) Validators(validators ...Validator) *OptProxyBool

Validators appends the validators of the option and returns itself.

type OptProxyDuration

type OptProxyDuration struct{ OptProxy }

OptProxyDuration is a proxy for the time.Duration option registered into Config, which can be used to modify the attributions of the option and update its value directly.

func NewDuration

func NewDuration(name string, _default time.Duration, help string) *OptProxyDuration

NewDuration is equal to Conf.NewDuration(name, _default, help).

func (*OptProxyDuration) Aliases

func (o *OptProxyDuration) Aliases(aliases ...string) *OptProxyDuration

Aliases appends the aliases of the option and returns itself.

func (*OptProxyDuration) Default

func (o *OptProxyDuration) Default(_default interface{}) *OptProxyDuration

Default resets the default value of the option and returns itself.

func (*OptProxyDuration) Get

func (o *OptProxyDuration) Get() time.Duration

Get returns the value of the option.

func (*OptProxyDuration) IsCli

func (o *OptProxyDuration) IsCli(cli bool) *OptProxyDuration

IsCli resets the cli flag of the option and returns itself.

func (*OptProxyDuration) Name

func (o *OptProxyDuration) Name() string

Name returns the name of the option.

func (*OptProxyDuration) OnUpdate

func (o *OptProxyDuration) OnUpdate(f func(old, new interface{})) *OptProxyDuration

OnUpdate resets the update callback of the option and returns itself.

func (*OptProxyDuration) Opt

func (o *OptProxyDuration) Opt() Opt

Opt returns the registered and proxied option.

func (*OptProxyDuration) Parser

func (o *OptProxyDuration) Parser(parser Parser) *OptProxyDuration

Parser resets the parser of the option and returns itself.

func (*OptProxyDuration) Set

func (o *OptProxyDuration) Set(value interface{}) (err error)

Set sets the value of the option to value.

func (*OptProxyDuration) Short

func (o *OptProxyDuration) Short(short string) *OptProxyDuration

Short resets the short name of the option and returns itself.

func (*OptProxyDuration) Validators

func (o *OptProxyDuration) Validators(validators ...Validator) *OptProxyDuration

Validators appends the validators of the option and returns itself.

type OptProxyDurationSlice

type OptProxyDurationSlice struct{ OptProxy }

OptProxyDurationSlice is a proxy for the []time.Duration option registered into Config, which can be used to modify the attributions of the option and update its value directly.

func NewDurationSlice

func NewDurationSlice(name string, _default []time.Duration, help string) *OptProxyDurationSlice

NewDurationSlice is equal to Conf.NewDurationSlice(name, _default, help).

func (*OptProxyDurationSlice) Aliases

func (o *OptProxyDurationSlice) Aliases(aliases ...string) *OptProxyDurationSlice

Aliases appends the aliases of the option and returns itself.

func (*OptProxyDurationSlice) Default

func (o *OptProxyDurationSlice) Default(_default interface{}) *OptProxyDurationSlice

Default resets the default value of the option and returns itself.

func (*OptProxyDurationSlice) Get

func (o *OptProxyDurationSlice) Get() []time.Duration

Get returns the value of the option.

func (*OptProxyDurationSlice) IsCli

IsCli resets the cli flag of the option and returns itself.

func (*OptProxyDurationSlice) Name

func (o *OptProxyDurationSlice) Name() string

Name returns the name of the option.

func (*OptProxyDurationSlice) OnUpdate

func (o *OptProxyDurationSlice) OnUpdate(f func(old, new interface{})) *OptProxyDurationSlice

OnUpdate resets the update callback of the option and returns itself.

func (*OptProxyDurationSlice) Opt

func (o *OptProxyDurationSlice) Opt() Opt

Opt returns the registered and proxied option.

func (*OptProxyDurationSlice) Parser

Parser resets the parser of the option and returns itself.

func (*OptProxyDurationSlice) Set

func (o *OptProxyDurationSlice) Set(value interface{}) (err error)

Set sets the value of the option to value.

func (*OptProxyDurationSlice) Short

Short resets the short name of the option and returns itself.

func (*OptProxyDurationSlice) Validators

func (o *OptProxyDurationSlice) Validators(validators ...Validator) *OptProxyDurationSlice

Validators appends the validators of the option and returns itself.

type OptProxyFloat64

type OptProxyFloat64 struct{ OptProxy }

OptProxyFloat64 is a proxy for the float64 option registered into Config, which can be used to modify the attributions of the option and update its value directly.

func NewFloat64

func NewFloat64(name string, _default float64, help string) *OptProxyFloat64

NewFloat64 is equal to Conf.NewFloat64(name, _default, help).

func (*OptProxyFloat64) Aliases

func (o *OptProxyFloat64) Aliases(aliases ...string) *OptProxyFloat64

Aliases appends the aliases of the option and returns itself.

func (*OptProxyFloat64) Default

func (o *OptProxyFloat64) Default(_default interface{}) *OptProxyFloat64

Default resets the default value of the option and returns itself.

func (*OptProxyFloat64) Get

func (o *OptProxyFloat64) Get() float64

Get returns the value of the option.

func (*OptProxyFloat64) IsCli

func (o *OptProxyFloat64) IsCli(cli bool) *OptProxyFloat64

IsCli resets the cli flag of the option and returns itself.

func (*OptProxyFloat64) Name

func (o *OptProxyFloat64) Name() string

Name returns the name of the option.

func (*OptProxyFloat64) OnUpdate

func (o *OptProxyFloat64) OnUpdate(f func(old, new interface{})) *OptProxyFloat64

OnUpdate resets the update callback of the option and returns itself.

func (*OptProxyFloat64) Opt

func (o *OptProxyFloat64) Opt() Opt

Opt returns the registered and proxied option.

func (*OptProxyFloat64) Parser

func (o *OptProxyFloat64) Parser(parser Parser) *OptProxyFloat64

Parser resets the parser of the option and returns itself.

func (*OptProxyFloat64) Set

func (o *OptProxyFloat64) Set(value interface{}) (err error)

Set sets the value of the option to value.

func (*OptProxyFloat64) Short

func (o *OptProxyFloat64) Short(short string) *OptProxyFloat64

Short resets the short name of the option and returns itself.

func (*OptProxyFloat64) Validators

func (o *OptProxyFloat64) Validators(validators ...Validator) *OptProxyFloat64

Validators appends the validators of the option and returns itself.

type OptProxyFloat64Slice

type OptProxyFloat64Slice struct{ OptProxy }

OptProxyFloat64Slice is a proxy for the []float64 option registered into Config, which can be used to modify the attributions of the option and update its value directly.

func NewFloat64Slice

func NewFloat64Slice(name string, _default []float64, help string) *OptProxyFloat64Slice

NewFloat64Slice is equal to Conf.NewFloat64Slice(name, _default, help).

func (*OptProxyFloat64Slice) Aliases

func (o *OptProxyFloat64Slice) Aliases(aliases ...string) *OptProxyFloat64Slice

Aliases appends the aliases of the option and returns itself.

func (*OptProxyFloat64Slice) Default

func (o *OptProxyFloat64Slice) Default(_default interface{}) *OptProxyFloat64Slice

Default resets the default value of the option and returns itself.

func (*OptProxyFloat64Slice) Get

func (o *OptProxyFloat64Slice) Get() []float64

Get returns the value of the option.

func (*OptProxyFloat64Slice) IsCli

IsCli resets the cli flag of the option and returns itself.

func (*OptProxyFloat64Slice) Name

func (o *OptProxyFloat64Slice) Name() string

Name returns the name of the option.

func (*OptProxyFloat64Slice) OnUpdate

func (o *OptProxyFloat64Slice) OnUpdate(f func(old, new interface{})) *OptProxyFloat64Slice

OnUpdate resets the update callback of the option and returns itself.

func (*OptProxyFloat64Slice) Opt

func (o *OptProxyFloat64Slice) Opt() Opt

Opt returns the registered and proxied option.

func (*OptProxyFloat64Slice) Parser

Parser resets the parser of the option and returns itself.

func (*OptProxyFloat64Slice) Set

func (o *OptProxyFloat64Slice) Set(value interface{}) (err error)

Set sets the value of the option to value.

func (*OptProxyFloat64Slice) Short

Short resets the short name of the option and returns itself.

func (*OptProxyFloat64Slice) Validators

func (o *OptProxyFloat64Slice) Validators(validators ...Validator) *OptProxyFloat64Slice

Validators appends the validators of the option and returns itself.

type OptProxyInt

type OptProxyInt struct{ OptProxy }

OptProxyInt is a proxy for the int option registered into Config, which can be used to modify the attributions of the option and update its value directly.

func NewInt

func NewInt(name string, _default int, help string) *OptProxyInt

NewInt is equal to Conf.NewInt(name, _default, help).

func (*OptProxyInt) Aliases

func (o *OptProxyInt) Aliases(aliases ...string) *OptProxyInt

Aliases appends the aliases of the option and returns itself.

func (*OptProxyInt) Default

func (o *OptProxyInt) Default(_default interface{}) *OptProxyInt

Default resets the default value of the option and returns itself.

func (*OptProxyInt) Get

func (o *OptProxyInt) Get() int

Get returns the value of the option.

func (*OptProxyInt) IsCli

func (o *OptProxyInt) IsCli(cli bool) *OptProxyInt

IsCli resets the cli flag of the option and returns itself.

func (*OptProxyInt) Name

func (o *OptProxyInt) Name() string

Name returns the name of the option.

func (*OptProxyInt) OnUpdate

func (o *OptProxyInt) OnUpdate(f func(old, new interface{})) *OptProxyInt

OnUpdate resets the update callback of the option and returns itself.

func (*OptProxyInt) Opt

func (o *OptProxyInt) Opt() Opt

Opt returns the registered and proxied option.

func (*OptProxyInt) Parser

func (o *OptProxyInt) Parser(parser Parser) *OptProxyInt

Parser resets the parser of the option and returns itself.

func (*OptProxyInt) Set

func (o *OptProxyInt) Set(value interface{}) (err error)

Set sets the value of the option to value.

func (*OptProxyInt) Short

func (o *OptProxyInt) Short(short string) *OptProxyInt

Short resets the short name of the option and returns itself.

func (*OptProxyInt) Validators

func (o *OptProxyInt) Validators(validators ...Validator) *OptProxyInt

Validators appends the validators of the option and returns itself.

type OptProxyInt16 added in v6.1.0

type OptProxyInt16 struct{ OptProxy }

OptProxyInt16 is a proxy for the int16 option registered into Config, which can be used to modify the attributions of the option and update its value directly.

func NewInt16 added in v6.1.0

func NewInt16(name string, _default int16, help string) *OptProxyInt16

NewInt16 is equal to Conf.NewInt16(name, _default, help).

func (*OptProxyInt16) Aliases added in v6.1.0

func (o *OptProxyInt16) Aliases(aliases ...string) *OptProxyInt16

Aliases appends the aliases of the option and returns itself.

func (*OptProxyInt16) Default added in v6.1.0

func (o *OptProxyInt16) Default(_default interface{}) *OptProxyInt16

Default resets the default value of the option and returns itself.

func (*OptProxyInt16) Get added in v6.1.0

func (o *OptProxyInt16) Get() int16

Get returns the value of the option.

func (*OptProxyInt16) IsCli added in v6.1.0

func (o *OptProxyInt16) IsCli(cli bool) *OptProxyInt16

IsCli resets the cli flag of the option and returns itself.

func (*OptProxyInt16) Name added in v6.1.0

func (o *OptProxyInt16) Name() string

Name returns the name of the option.

func (*OptProxyInt16) OnUpdate added in v6.1.0

func (o *OptProxyInt16) OnUpdate(f func(old, new interface{})) *OptProxyInt16

OnUpdate resets the update callback of the option and returns itself.

func (*OptProxyInt16) Opt added in v6.1.0

func (o *OptProxyInt16) Opt() Opt

Opt returns the registered and proxied option.

func (*OptProxyInt16) Parser added in v6.1.0

func (o *OptProxyInt16) Parser(parser Parser) *OptProxyInt16

Parser resets the parser of the option and returns itself.

func (*OptProxyInt16) Set added in v6.1.0

func (o *OptProxyInt16) Set(value interface{}) (err error)

Set sets the value of the option to value.

func (*OptProxyInt16) Short added in v6.1.0

func (o *OptProxyInt16) Short(short string) *OptProxyInt16

Short resets the short name of the option and returns itself.

func (*OptProxyInt16) Validators added in v6.1.0

func (o *OptProxyInt16) Validators(validators ...Validator) *OptProxyInt16

Validators appends the validators of the option and returns itself.

type OptProxyInt32

type OptProxyInt32 struct{ OptProxy }

OptProxyInt32 is a proxy for the int32 option registered into Config, which can be used to modify the attributions of the option and update its value directly.

func NewInt32

func NewInt32(name string, _default int32, help string) *OptProxyInt32

NewInt32 is equal to Conf.NewInt32(name, _default, help).

func (*OptProxyInt32) Aliases

func (o *OptProxyInt32) Aliases(aliases ...string) *OptProxyInt32

Aliases appends the aliases of the option and returns itself.

func (*OptProxyInt32) Default

func (o *OptProxyInt32) Default(_default interface{}) *OptProxyInt32

Default resets the default value of the option and returns itself.

func (*OptProxyInt32) Get

func (o *OptProxyInt32) Get() int32

Get returns the value of the option.

func (*OptProxyInt32) IsCli

func (o *OptProxyInt32) IsCli(cli bool) *OptProxyInt32

IsCli resets the cli flag of the option and returns itself.

func (*OptProxyInt32) Name

func (o *OptProxyInt32) Name() string

Name returns the name of the option.

func (*OptProxyInt32) OnUpdate

func (o *OptProxyInt32) OnUpdate(f func(old, new interface{})) *OptProxyInt32

OnUpdate resets the update callback of the option and returns itself.

func (*OptProxyInt32) Opt

func (o *OptProxyInt32) Opt() Opt

Opt returns the registered and proxied option.

func (*OptProxyInt32) Parser

func (o *OptProxyInt32) Parser(parser Parser) *OptProxyInt32

Parser resets the parser of the option and returns itself.

func (*OptProxyInt32) Set

func (o *OptProxyInt32) Set(value interface{}) (err error)

Set sets the value of the option to value.

func (*OptProxyInt32) Short

func (o *OptProxyInt32) Short(short string) *OptProxyInt32

Short resets the short name of the option and returns itself.

func (*OptProxyInt32) Validators

func (o *OptProxyInt32) Validators(validators ...Validator) *OptProxyInt32

Validators appends the validators of the option and returns itself.

type OptProxyInt64

type OptProxyInt64 struct{ OptProxy }

OptProxyInt64 is a proxy for the int64 option registered into Config, which can be used to modify the attributions of the option and update its value directly.

func NewInt64

func NewInt64(name string, _default int64, help string) *OptProxyInt64

NewInt64 is equal to Conf.NewInt64(name, _default, help).

func (*OptProxyInt64) Aliases

func (o *OptProxyInt64) Aliases(aliases ...string) *OptProxyInt64

Aliases appends the aliases of the option and returns itself.

func (*OptProxyInt64) Default

func (o *OptProxyInt64) Default(_default interface{}) *OptProxyInt64

Default resets the default value of the option and returns itself.

func (*OptProxyInt64) Get

func (o *OptProxyInt64) Get() int64

Get returns the value of the option.

func (*OptProxyInt64) IsCli

func (o *OptProxyInt64) IsCli(cli bool) *OptProxyInt64

IsCli resets the cli flag of the option and returns itself.

func (*OptProxyInt64) Name

func (o *OptProxyInt64) Name() string

Name returns the name of the option.

func (*OptProxyInt64) OnUpdate

func (o *OptProxyInt64) OnUpdate(f func(old, new interface{})) *OptProxyInt64

OnUpdate resets the update callback of the option and returns itself.

func (*OptProxyInt64) Opt

func (o *OptProxyInt64) Opt() Opt

Opt returns the registered and proxied option.

func (*OptProxyInt64) Parser

func (o *OptProxyInt64) Parser(parser Parser) *OptProxyInt64

Parser resets the parser of the option and returns itself.

func (*OptProxyInt64) Set

func (o *OptProxyInt64) Set(value interface{}) (err error)

Set sets the value of the option to value.

func (*OptProxyInt64) Short

func (o *OptProxyInt64) Short(short string) *OptProxyInt64

Short resets the short name of the option and returns itself.

func (*OptProxyInt64) Validators

func (o *OptProxyInt64) Validators(validators ...Validator) *OptProxyInt64

Validators appends the validators of the option and returns itself.

type OptProxyIntSlice

type OptProxyIntSlice struct{ OptProxy }

OptProxyIntSlice is a proxy for the []int option registered into Config, which can be used to modify the attributions of the option and update its value directly.

func NewIntSlice

func NewIntSlice(name string, _default []int, help string) *OptProxyIntSlice

NewIntSlice is equal to Conf.NewIntSlice(name, _default, help).

func (*OptProxyIntSlice) Aliases

func (o *OptProxyIntSlice) Aliases(aliases ...string) *OptProxyIntSlice

Aliases appends the aliases of the option and returns itself.

func (*OptProxyIntSlice) Default

func (o *OptProxyIntSlice) Default(_default interface{}) *OptProxyIntSlice

Default resets the default value of the option and returns itself.

func (*OptProxyIntSlice) Get

func (o *OptProxyIntSlice) Get() []int

Get returns the value of the option.

func (*OptProxyIntSlice) IsCli

func (o *OptProxyIntSlice) IsCli(cli bool) *OptProxyIntSlice

IsCli resets the cli flag of the option and returns itself.

func (*OptProxyIntSlice) Name

func (o *OptProxyIntSlice) Name() string

Name returns the name of the option.

func (*OptProxyIntSlice) OnUpdate

func (o *OptProxyIntSlice) OnUpdate(f func(old, new interface{})) *OptProxyIntSlice

OnUpdate resets the update callback of the option and returns itself.

func (*OptProxyIntSlice) Opt

func (o *OptProxyIntSlice) Opt() Opt

Opt returns the registered and proxied option.

func (*OptProxyIntSlice) Parser

func (o *OptProxyIntSlice) Parser(parser Parser) *OptProxyIntSlice

Parser resets the parser of the option and returns itself.

func (*OptProxyIntSlice) Set

func (o *OptProxyIntSlice) Set(value interface{}) (err error)

Set sets the value of the option to value.

func (*OptProxyIntSlice) Short

func (o *OptProxyIntSlice) Short(short string) *OptProxyIntSlice

Short resets the short name of the option and returns itself.

func (*OptProxyIntSlice) Validators

func (o *OptProxyIntSlice) Validators(validators ...Validator) *OptProxyIntSlice

Validators appends the validators of the option and returns itself.

type OptProxyString

type OptProxyString struct{ OptProxy }

OptProxyString is a proxy for the string option registered into Config, which can be used to modify the attributions of the option and update its value directly.

func NewString

func NewString(name, _default, help string) *OptProxyString

NewString is equal to Conf.NewString(name, _default, help).

func (*OptProxyString) Aliases

func (o *OptProxyString) Aliases(aliases ...string) *OptProxyString

Aliases appends the aliases of the option and returns itself.

func (*OptProxyString) Default

func (o *OptProxyString) Default(_default interface{}) *OptProxyString

Default resets the default value of the option and returns itself.

func (*OptProxyString) Get

func (o *OptProxyString) Get() string

Get returns the value of the option.

func (*OptProxyString) IsCli

func (o *OptProxyString) IsCli(cli bool) *OptProxyString

IsCli resets the cli flag of the option and returns itself.

func (*OptProxyString) Name

func (o *OptProxyString) Name() string

Name returns the name of the option.

func (*OptProxyString) OnUpdate

func (o *OptProxyString) OnUpdate(f func(old, new interface{})) *OptProxyString

OnUpdate resets the update callback of the option and returns itself.

func (*OptProxyString) Opt

func (o *OptProxyString) Opt() Opt

Opt returns the registered and proxied option.

func (*OptProxyString) Parser

func (o *OptProxyString) Parser(parser Parser) *OptProxyString

Parser resets the parser of the option and returns itself.

func (*OptProxyString) Set

func (o *OptProxyString) Set(value interface{}) (err error)

Set sets the value of the option to value.

func (*OptProxyString) Short

func (o *OptProxyString) Short(short string) *OptProxyString

Short resets the short name of the option and returns itself.

func (*OptProxyString) Validators

func (o *OptProxyString) Validators(validators ...Validator) *OptProxyString

Validators appends the validators of the option and returns itself.

type OptProxyStringSlice

type OptProxyStringSlice struct{ OptProxy }

OptProxyStringSlice is a proxy for the []string option registered into Config, which can be used to modify the attributions of the option and update its value directly.

func NewStringSlice

func NewStringSlice(name string, _default []string, help string) *OptProxyStringSlice

NewStringSlice is equal to Conf.NewStringSlice(name, _default, help).

func (*OptProxyStringSlice) Aliases

func (o *OptProxyStringSlice) Aliases(aliases ...string) *OptProxyStringSlice

Aliases appends the aliases of the option and returns itself.

func (*OptProxyStringSlice) Default

func (o *OptProxyStringSlice) Default(_default interface{}) *OptProxyStringSlice

Default resets the default value of the option and returns itself.

func (*OptProxyStringSlice) Get

func (o *OptProxyStringSlice) Get() []string

Get returns the value of the option.

func (*OptProxyStringSlice) IsCli

IsCli resets the cli flag of the option and returns itself.

func (*OptProxyStringSlice) Name

func (o *OptProxyStringSlice) Name() string

Name returns the name of the option.

func (*OptProxyStringSlice) OnUpdate

func (o *OptProxyStringSlice) OnUpdate(f func(old, new interface{})) *OptProxyStringSlice

OnUpdate resets the update callback of the option and returns itself.

func (*OptProxyStringSlice) Opt

func (o *OptProxyStringSlice) Opt() Opt

Opt returns the registered and proxied option.

func (*OptProxyStringSlice) Parser

func (o *OptProxyStringSlice) Parser(parser Parser) *OptProxyStringSlice

Parser resets the parser of the option and returns itself.

func (*OptProxyStringSlice) Set

func (o *OptProxyStringSlice) Set(value interface{}) (err error)

Set sets the value of the option to value.

func (*OptProxyStringSlice) Short

Short resets the short name of the option and returns itself.

func (*OptProxyStringSlice) Validators

func (o *OptProxyStringSlice) Validators(validators ...Validator) *OptProxyStringSlice

Validators appends the validators of the option and returns itself.

type OptProxyTime

type OptProxyTime struct{ OptProxy }

OptProxyTime is a proxy for the time.Time option registered into Config, which can be used to modify the attributions of the option and update its value directly.

func NewTime

func NewTime(name string, _default time.Time, help string) *OptProxyTime

NewTime is equal to Conf.NewTime(name, _default, help).

func (*OptProxyTime) Aliases

func (o *OptProxyTime) Aliases(aliases ...string) *OptProxyTime

Aliases appends the aliases of the option and returns itself.

func (*OptProxyTime) Default

func (o *OptProxyTime) Default(_default interface{}) *OptProxyTime

Default resets the default value of the option and returns itself.

func (*OptProxyTime) Get

func (o *OptProxyTime) Get() time.Time

Get returns the value of the option.

func (*OptProxyTime) IsCli

func (o *OptProxyTime) IsCli(cli bool) *OptProxyTime

IsCli resets the cli flag of the option and returns itself.

func (*OptProxyTime) Name

func (o *OptProxyTime) Name() string

Name returns the name of the option.

func (*OptProxyTime) OnUpdate

func (o *OptProxyTime) OnUpdate(f func(old, new interface{})) *OptProxyTime

OnUpdate resets the update callback of the option and returns itself.

func (*OptProxyTime) Opt

func (o *OptProxyTime) Opt() Opt

Opt returns the registered and proxied option.

func (*OptProxyTime) Parser

func (o *OptProxyTime) Parser(parser Parser) *OptProxyTime

Parser resets the parser of the option and returns itself.

func (*OptProxyTime) Set

func (o *OptProxyTime) Set(value interface{}) (err error)

Set sets the value of the option to value.

func (*OptProxyTime) Short

func (o *OptProxyTime) Short(short string) *OptProxyTime

Short resets the short name of the option and returns itself.

func (*OptProxyTime) Validators

func (o *OptProxyTime) Validators(validators ...Validator) *OptProxyTime

Validators appends the validators of the option and returns itself.

type OptProxyUint

type OptProxyUint struct{ OptProxy }

OptProxyUint is a proxy for the uint option registered into Config, which can be used to modify the attributions of the option and update its value directly.

func NewUint

func NewUint(name string, _default uint, help string) *OptProxyUint

NewUint is equal to Conf.NewUint(name, _default, help).

func (*OptProxyUint) Aliases

func (o *OptProxyUint) Aliases(aliases ...string) *OptProxyUint

Aliases appends the aliases of the option and returns itself.

func (*OptProxyUint) Default

func (o *OptProxyUint) Default(_default interface{}) *OptProxyUint

Default resets the default value of the option and returns itself.

func (*OptProxyUint) Get

func (o *OptProxyUint) Get() uint

Get returns the value of the option.

func (*OptProxyUint) IsCli

func (o *OptProxyUint) IsCli(cli bool) *OptProxyUint

IsCli resets the cli flag of the option and returns itself.

func (*OptProxyUint) Name

func (o *OptProxyUint) Name() string

Name returns the name of the option.

func (*OptProxyUint) OnUpdate

func (o *OptProxyUint) OnUpdate(f func(old, new interface{})) *OptProxyUint

OnUpdate resets the update callback of the option and returns itself.

func (*OptProxyUint) Opt

func (o *OptProxyUint) Opt() Opt

Opt returns the registered and proxied option.

func (*OptProxyUint) Parser

func (o *OptProxyUint) Parser(parser Parser) *OptProxyUint

Parser resets the parser of the option and returns itself.

func (*OptProxyUint) Set

func (o *OptProxyUint) Set(value interface{}) (err error)

Set sets the value of the option to value.

func (*OptProxyUint) Short

func (o *OptProxyUint) Short(short string) *OptProxyUint

Short resets the short name of the option and returns itself.

func (*OptProxyUint) Validators

func (o *OptProxyUint) Validators(validators ...Validator) *OptProxyUint

Validators appends the validators of the option and returns itself.

type OptProxyUint16 added in v6.1.0

type OptProxyUint16 struct{ OptProxy }

OptProxyUint16 is a proxy for the uint16 option registered into Config, which can be used to modify the attributions of the option and update its value directly.

func NewUint16 added in v6.1.0

func NewUint16(name string, _default uint16, help string) *OptProxyUint16

NewUint16 is equal to Conf.NewUint16(name, _default, help).

func (*OptProxyUint16) Aliases added in v6.1.0

func (o *OptProxyUint16) Aliases(aliases ...string) *OptProxyUint16

Aliases appends the aliases of the option and returns itself.

func (*OptProxyUint16) Default added in v6.1.0

func (o *OptProxyUint16) Default(_default interface{}) *OptProxyUint16

Default resets the default value of the option and returns itself.

func (*OptProxyUint16) Get added in v6.1.0

func (o *OptProxyUint16) Get() uint16

Get returns the value of the option.

func (*OptProxyUint16) IsCli added in v6.1.0

func (o *OptProxyUint16) IsCli(cli bool) *OptProxyUint16

IsCli resets the cli flag of the option and returns itself.

func (*OptProxyUint16) Name added in v6.1.0

func (o *OptProxyUint16) Name() string

Name returns the name of the option.

func (*OptProxyUint16) OnUpdate added in v6.1.0

func (o *OptProxyUint16) OnUpdate(f func(old, new interface{})) *OptProxyUint16

OnUpdate resets the update callback of the option and returns itself.

func (*OptProxyUint16) Opt added in v6.1.0

func (o *OptProxyUint16) Opt() Opt

Opt returns the registered and proxied option.

func (*OptProxyUint16) Parser added in v6.1.0

func (o *OptProxyUint16) Parser(parser Parser) *OptProxyUint16

Parser resets the parser of the option and returns itself.

func (*OptProxyUint16) Set added in v6.1.0

func (o *OptProxyUint16) Set(value interface{}) (err error)

Set sets the value of the option to value.

func (*OptProxyUint16) Short added in v6.1.0

func (o *OptProxyUint16) Short(short string) *OptProxyUint16

Short resets the short name of the option and returns itself.

func (*OptProxyUint16) Validators added in v6.1.0

func (o *OptProxyUint16) Validators(validators ...Validator) *OptProxyUint16

Validators appends the validators of the option and returns itself.

type OptProxyUint32

type OptProxyUint32 struct{ OptProxy }

OptProxyUint32 is a proxy for the uint32 option registered into Config, which can be used to modify the attributions of the option and update its value directly.

func NewUint32

func NewUint32(name string, _default uint32, help string) *OptProxyUint32

NewUint32 is equal to Conf.NewUint32(name, _default, help).

func (*OptProxyUint32) Aliases

func (o *OptProxyUint32) Aliases(aliases ...string) *OptProxyUint32

Aliases appends the aliases of the option and returns itself.

func (*OptProxyUint32) Default

func (o *OptProxyUint32) Default(_default interface{}) *OptProxyUint32

Default resets the default value of the option and returns itself.

func (*OptProxyUint32) Get

func (o *OptProxyUint32) Get() uint32

Get returns the value of the option.

func (*OptProxyUint32) IsCli

func (o *OptProxyUint32) IsCli(cli bool) *OptProxyUint32

IsCli resets the cli flag of the option and returns itself.

func (*OptProxyUint32) Name

func (o *OptProxyUint32) Name() string

Name returns the name of the option.

func (*OptProxyUint32) OnUpdate

func (o *OptProxyUint32) OnUpdate(f func(old, new interface{})) *OptProxyUint32

OnUpdate resets the update callback of the option and returns itself.

func (*OptProxyUint32) Opt

func (o *OptProxyUint32) Opt() Opt

Opt returns the registered and proxied option.

func (*OptProxyUint32) Parser

func (o *OptProxyUint32) Parser(parser Parser) *OptProxyUint32

Parser resets the parser of the option and returns itself.

func (*OptProxyUint32) Set

func (o *OptProxyUint32) Set(value interface{}) (err error)

Set sets the value of the option to value.

func (*OptProxyUint32) Short

func (o *OptProxyUint32) Short(short string) *OptProxyUint32

Short resets the short name of the option and returns itself.

func (*OptProxyUint32) Validators

func (o *OptProxyUint32) Validators(validators ...Validator) *OptProxyUint32

Validators appends the validators of the option and returns itself.

type OptProxyUint64

type OptProxyUint64 struct{ OptProxy }

OptProxyUint64 is a proxy for the uint64 option registered into Config, which can be used to modify the attributions of the option and update its value directly.

func NewUint64

func NewUint64(name string, _default uint64, help string) *OptProxyUint64

NewUint64 is equal to Conf.NewUint64(name, _default, help).

func (*OptProxyUint64) Aliases

func (o *OptProxyUint64) Aliases(aliases ...string) *OptProxyUint64

Aliases appends the aliases of the option and returns itself.

func (*OptProxyUint64) Default

func (o *OptProxyUint64) Default(_default interface{}) *OptProxyUint64

Default resets the default value of the option and returns itself.

func (*OptProxyUint64) Get

func (o *OptProxyUint64) Get() uint64

Get returns the value of the option.

func (*OptProxyUint64) IsCli

func (o *OptProxyUint64) IsCli(cli bool) *OptProxyUint64

IsCli resets the cli flag of the option and returns itself.

func (*OptProxyUint64) Name

func (o *OptProxyUint64) Name() string

Name returns the name of the option.

func (*OptProxyUint64) OnUpdate

func (o *OptProxyUint64) OnUpdate(f func(old, new interface{})) *OptProxyUint64

OnUpdate resets the update callback of the option and returns itself.

func (*OptProxyUint64) Opt

func (o *OptProxyUint64) Opt() Opt

Opt returns the registered and proxied option.

func (*OptProxyUint64) Parser

func (o *OptProxyUint64) Parser(parser Parser) *OptProxyUint64

Parser resets the parser of the option and returns itself.

func (*OptProxyUint64) Set

func (o *OptProxyUint64) Set(value interface{}) (err error)

Set sets the value of the option to value.

func (*OptProxyUint64) Short

func (o *OptProxyUint64) Short(short string) *OptProxyUint64

Short resets the short name of the option and returns itself.

func (*OptProxyUint64) Validators

func (o *OptProxyUint64) Validators(validators ...Validator) *OptProxyUint64

Validators appends the validators of the option and returns itself.

type OptProxyUintSlice

type OptProxyUintSlice struct{ OptProxy }

OptProxyUintSlice is a proxy for the []uint option registered into Config, which can be used to modify the attributions of the option and update its value directly.

func NewUintSlice

func NewUintSlice(name string, _default []uint, help string) *OptProxyUintSlice

NewUintSlice is equal to Conf.NewUintSlice(name, _default, help).

func (*OptProxyUintSlice) Aliases

func (o *OptProxyUintSlice) Aliases(aliases ...string) *OptProxyUintSlice

Aliases appends the aliases of the option and returns itself.

func (*OptProxyUintSlice) Default

func (o *OptProxyUintSlice) Default(_default interface{}) *OptProxyUintSlice

Default resets the default value of the option and returns itself.

func (*OptProxyUintSlice) Get

func (o *OptProxyUintSlice) Get() []uint

Get returns the value of the option.

func (*OptProxyUintSlice) IsCli

func (o *OptProxyUintSlice) IsCli(cli bool) *OptProxyUintSlice

IsCli resets the cli flag of the option and returns itself.

func (*OptProxyUintSlice) Name

func (o *OptProxyUintSlice) Name() string

Name returns the name of the option.

func (*OptProxyUintSlice) OnUpdate

func (o *OptProxyUintSlice) OnUpdate(f func(old, new interface{})) *OptProxyUintSlice

OnUpdate resets the update callback of the option and returns itself.

func (*OptProxyUintSlice) Opt

func (o *OptProxyUintSlice) Opt() Opt

Opt returns the registered and proxied option.

func (*OptProxyUintSlice) Parser

func (o *OptProxyUintSlice) Parser(parser Parser) *OptProxyUintSlice

Parser resets the parser of the option and returns itself.

func (*OptProxyUintSlice) Set

func (o *OptProxyUintSlice) Set(value interface{}) (err error)

Set sets the value of the option to value.

func (*OptProxyUintSlice) Short

func (o *OptProxyUintSlice) Short(short string) *OptProxyUintSlice

Short resets the short name of the option and returns itself.

func (*OptProxyUintSlice) Validators

func (o *OptProxyUintSlice) Validators(validators ...Validator) *OptProxyUintSlice

Validators appends the validators of the option and returns itself.

type Parser

type Parser func(input interface{}) (output interface{}, err error)

Parser is used to parse the option value intput.

type Source

type Source interface {
	// String is the description of the source, such as "env", "file:/path/to".
	String() string

	// Read reads the source data once, which should not block.
	Read() (DataSet, error)

	// Watch watches the change of the source, then call the callback load.
	//
	// close is used to notice the underlying watcher to close and clean.
	Watch(close <-chan struct{}, load func(DataSet, error) (success bool))
}

Source represents a data source where the data is.

func NewEnvSource

func NewEnvSource(prefix string) Source

NewEnvSource returns a new Source based on the environment variables, which reads the configuration from the environment variables.

If giving the prefix, it only uses the environment variable name matching the given prefix, then removes the prefix and the rest is used as the option name.

Notice: It will convert all the underlines("_") to the dots(".").

func NewFileSource

func NewFileSource(filename string, defaultFormat ...string) Source

NewFileSource returns a new source that the data is read from the file named filename.

The file source can watch the change of the given file. And it will identify the format by the filename extension automatically. If no filename extension, it will use defaulFormat, which is "ini" by default.

func NewFlagSource

func NewFlagSource(flagSet ...*flag.FlagSet) Source

NewFlagSource returns a new source based on flag.FlagSet, which is flag.CommandLine by default.

func NewURLSource

func NewURLSource(url string, interval time.Duration, format ...string) Source

NewURLSource returns a url source to read the configuration data from the url by the stdlib http.Get(url).

The header "Content-Type" indicates the data format, that's, it will split the value by "/" and use the last part, such as "application/json" represents the format "json". But you can set format to override it.

The url source can watch the configuration data from the url each interval period. If interval is equal to 0, it is defaulted to time.Minute.

type Validator

type Validator func(value interface{}) error

Validator is used to validate whether the option value is valid.

func NewAddressOrIPSliceValidator

func NewAddressOrIPSliceValidator() Validator

NewAddressOrIPSliceValidator returns a validator to validate whether the string element of the []string value is an address or ip.

func NewAddressOrIPValidator

func NewAddressOrIPValidator() Validator

NewAddressOrIPValidator is equal to NewAddressValidator, but it maybe miss the port.

func NewAddressSliceValidator

func NewAddressSliceValidator() Validator

NewAddressSliceValidator returns a validator to validate whether the string element of the []string value is a valid address.

func NewAddressValidator

func NewAddressValidator() Validator

NewAddressValidator returns a validator to validate whether an address is like "host:port", "host%zone:port", "[host]:port" or "[host%zone]:port".

This validator uses net.SplitHostPort() to validate it.

func NewEmailSliceValidator

func NewEmailSliceValidator() Validator

NewEmailSliceValidator returns a validator to validate whether the string element of the []string value is a valid email.

func NewEmailValidator

func NewEmailValidator() Validator

NewEmailValidator returns a validator to validate whether an email is valid.

func NewEmptyStrValidator

func NewEmptyStrValidator() Validator

NewEmptyStrValidator returns a validator to validate that the value must be an empty string.

func NewFloatRangeValidator

func NewFloatRangeValidator(min, max float64) Validator

NewFloatRangeValidator returns a validator to validate whether the float value is between the min and the max.

This validator can be used to validate the value of the type float32 and float64.

func NewIPSliceValidator

func NewIPSliceValidator() Validator

NewIPSliceValidator returns a validator to validate whether the string element of the []string value is a valid IP.

func NewIPValidator

func NewIPValidator() Validator

NewIPValidator returns a validator to validate whether an ip is valid.

func NewIntegerRangeValidator

func NewIntegerRangeValidator(min, max int64) Validator

NewIntegerRangeValidator returns a validator to validate whether the integer value is between the min and the max.

This validator can be used to validate the value of the type int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64.

func NewMaybeAddressOrIPValidator

func NewMaybeAddressOrIPValidator() Validator

NewMaybeAddressOrIPValidator returns a validator to validate the value may be empty or an address or an ip.

func NewMaybeAddressValidator

func NewMaybeAddressValidator() Validator

NewMaybeAddressValidator returns a validator to validate the value may be empty or an address.

func NewMaybeEmailValidator

func NewMaybeEmailValidator() Validator

NewMaybeEmailValidator returns a validator to validate the value may be empty or an email.

func NewMaybeIPValidator

func NewMaybeIPValidator() Validator

NewMaybeIPValidator returns a validator to validate the value may be empty or a ip.

func NewMaybeURLValidator

func NewMaybeURLValidator() Validator

NewMaybeURLValidator returns a validator to validate the value may be empty or a URL.

func NewPortValidator

func NewPortValidator() Validator

NewPortValidator returns a validator to validate whether a port is between 0 and 65535.

func NewRegexpValidator

func NewRegexpValidator(pattern string) Validator

NewRegexpValidator returns a validator to validate whether the value match the regular expression.

This validator uses regexp.MatchString(pattern, s) to validate it.

func NewStrArrayValidator

func NewStrArrayValidator(array []string) Validator

NewStrArrayValidator returns a validator to validate that the value is in the array.

func NewStrLenValidator

func NewStrLenValidator(min, max int) Validator

NewStrLenValidator returns a validator to validate that the length of the string must be between min and max.

func NewStrNotEmptyValidator

func NewStrNotEmptyValidator() Validator

NewStrNotEmptyValidator returns a validator to validate that the value must not be an empty string.

func NewStrSliceValidator

func NewStrSliceValidator(strValidators ...Validator) Validator

NewStrSliceValidator returns a validator to validate whether the string element of the []string value satisfies all the given validators.

func NewURLSliceValidator

func NewURLSliceValidator() Validator

NewURLSliceValidator returns a validator to validate whether the string element of the []string value is a valid URL.

func NewURLValidator

func NewURLValidator() Validator

NewURLValidator returns a validator to validate whether a url is valid.

func Or

func Or(validators ...Validator) Validator

Or returns a union validator, which returns nil only if a certain validator returns nil or the error that the last validator returns.

Jump to

Keyboard shortcuts

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