Documentation
¶
Overview ¶
Package cc is a very flexible configuration management library for humans, which is easy to use and support YAML and JSON only.
Usage
c, _ := cc.NewConfigFromFile("./example/example.yaml") // file must has extension
_ := c.MergeFromFile("./example/example.json") // do not ignore the errors
c.Must("name") // panic if not found
c.String("name")
cc := c.Config("map")
cc.Bool("key_one")
list := c.Value("list").List()
list[1].Int()
// environment variables
os.Setenv("float_env", "11.11")
c.Float("float_env")
// flags
flag.Int("flag", 33, "usage")
c.Int("flag")
The priorities: flags > environment variables > normal configs NOTE: we take empty string, false boolean and zero number value as default value in flags, and those value has no priority.
Default Configs ¶
We may write the code like this:
name := "default"
if c.Has("name") {
name = c.String("name") // or panic
}
Now, we can write code like this:
name := c.StringOr("name", "cc") // or c.Must("name")
b := c.BoolOr("bool", true)
f := c.FloatOr("float", 3.14)
i := c.IntOr("int", 33)
Pattern and Validation ¶
If you want to check string value whether it is matched by regexp:
s, ok := c.StringAnd("name", "^c")
Or, the make the string value as a pattern:
p := c.Pattern("pattern_key_name")
ok := p.ValidateString("a string")
For int(time.Duration) and float, cc use if-like condition to do similar work. Assume we have `threhold: "N>=30&&N<=80"` in config file, we can use it like this:
p := c.Pattern("threhold")
ok := p.ValidateInt(40) // or ValidateFloat
Or, using a pattern to validate the number:
ni, ok := c.IntAnd("int_key", "N>50")
nf, ok := c.FloatAnd("float_key", "N/100>=0.3")
// or, given a default value
ni = c.IntAndOr("int_key", "N>50", 51)
nf = c.FloatAndOr("int_key", "N/100>=0.3", 40)
d := c.DurationAndOr("duration", "N>20&&N<=100", 50)
NOTE: bit operation is not supported.
Index ¶
- type Config
- func (c *Config) Bool(name string) bool
- func (c *Config) BoolOr(name string, deflt bool) bool
- func (c *Config) Config(name string) Configer
- func (c *Config) Duration(name string) time.Duration
- func (c *Config) DurationAnd(name string, pattern string) (time.Duration, bool)
- func (c *Config) DurationAndOr(name string, pattern string, deflt int64) time.Duration
- func (c *Config) DurationOr(name string, deflt int64) time.Duration
- func (c *Config) Float(name string) float64
- func (c *Config) FloatAnd(name string, pattern string) (float64, bool)
- func (c *Config) FloatAndOr(name string, pattern string, deflt float64) float64
- func (c *Config) FloatOr(name string, deflt float64) float64
- func (c *Config) Has(name string) bool
- func (c *Config) Int(name string) int
- func (c *Config) Int64(name string) int64
- func (c *Config) Int64And(name string, pattern string) (int64, bool)
- func (c *Config) Int64AndOr(name string, pattern string, deflt int64) int64
- func (c *Config) Int64Or(name string, deflt int64) int64
- func (c *Config) IntAnd(name string, pattern string) (int, bool)
- func (c *Config) IntAndOr(name string, pattern string, deflt int) int
- func (c *Config) IntOr(name string, deflt int) int
- func (c *Config) KV() map[string]interface{}
- func (c *Config) Merge(config *Config) error
- func (c *Config) MergeFromFile(fpath string) error
- func (c *Config) MergeFromJSON(b []byte) error
- func (c *Config) MergeFromYAML(b []byte) error
- func (c *Config) Must(name string)
- func (c *Config) ParseFlags()
- func (c *Config) Pattern(name string) Patterner
- func (c *Config) Raw(name string) interface{}
- func (c *Config) Set(name string, value interface{})
- func (c *Config) SetDefault(name string, value interface{})
- func (c *Config) String(name string) string
- func (c *Config) StringAnd(name string, pattern string) (string, bool)
- func (c *Config) StringAndOr(name string, pattern string, deflt string) string
- func (c *Config) StringOr(name string, deflt string) string
- func (c *Config) Value(name string) Valuer
- type Configer
- type Pattern
- type Patterner
- type Value
- func (v *Value) Bool() bool
- func (v *Value) BoolOr(deflt bool) bool
- func (v *Value) Config() Configer
- func (v *Value) Duration() time.Duration
- func (v *Value) DurationAnd(pattern string) (time.Duration, bool)
- func (v *Value) DurationAndOr(pattern string, deflt int64) time.Duration
- func (v *Value) DurationOr(deflt int64) time.Duration
- func (v *Value) Exist() bool
- func (v *Value) Float() float64
- func (v *Value) FloatAnd(pattern string) (float64, bool)
- func (v *Value) FloatAndOr(pattern string, deflt float64) float64
- func (v *Value) FloatOr(deflt float64) float64
- func (v *Value) GoString() string
- func (v *Value) Int() int
- func (v *Value) Int64() int64
- func (v *Value) Int64And(pattern string) (int64, bool)
- func (v *Value) Int64AndOr(pattern string, deflt int64) int64
- func (v *Value) Int64Or(deflt int64) int64
- func (v *Value) IntAnd(pattern string) (int, bool)
- func (v *Value) IntAndOr(pattern string, deflt int) int
- func (v *Value) IntOr(deflt int) int
- func (v *Value) List() []Valuer
- func (v *Value) Map() map[string]Valuer
- func (v *Value) Pattern() Patterner
- func (v *Value) Raw() interface{}
- func (v *Value) String() string
- func (v *Value) StringAnd(pattern string) (string, bool)
- func (v *Value) StringAndOr(pattern string, deflt string) string
- func (v *Value) StringOr(deflt string) string
- type Valuer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config implements the Configer interface. The priorities: flag > environment variables > normal configs. Only the String/Bool/Int/Float/Duration family use environment variables and flags, if any environment variable with same name is set and it isn't empty, the Bool/BoolOr will return true. NOTE: we take empty string, false boolean and zero number value as default value in flags, and those value has no priority.
func NewConfigFrom ¶
NewConfigFrom creates a new Config from map.
func NewConfigFromFile ¶
NewConfigFromFile creates a Config from a config file, extension must be one of ".yaml", ".yml" or ".json".
func NewConfigFromJSON ¶
NewConfigFromJSON creates a new Config from JSON bytes.
func NewConfigFromYAML ¶
NewConfigFromYAML creates a new Config from YAML bytes.
func (*Config) Config ¶
Config returns a key-value sub Configer by name, the returned Configer can consider as a reference. Excludes the flags and environment variable.
func (*Config) Duration ¶
Duration returns the time.Duration value by name, return time.Duration(0) if not found.
func (*Config) DurationAnd ¶
DurationAnd returns the (time.Duration(value), true) by name if pattern matched, otherwise (time.Duration(0), false) returned. NOTE: we convert all numbers into float64 then validate.
func (*Config) DurationAndOr ¶
DurationAndOr returns the time.Duration value by name if pattern matched, otherwise returns the deflt. NOTE: we convert all numbers into float64 then validate.
func (*Config) DurationOr ¶
DurationOr returns the time.Duration value by name, return time.Duration(deflt) if not found.
func (*Config) FloatAnd ¶
FloatAnd returns the (float64 value, true) if pattern matched, otherwise (0.0, false) returned.
func (*Config) FloatAndOr ¶
FloatAndOr returns the float64 value by name if pattern matched, otherwise returns the deflt.
func (*Config) Int64And ¶
Int64And returns the (int64 value, true) by name if pattern matched, otherwise returns (0, false). NOTE: we convert all numbers into float64 then validate.
func (*Config) Int64AndOr ¶
Int64AndOr returns the int64 value by name if pattern matched, otherwise returns the deflt. NOTE: we convert all numbers into float64 then validate.
func (*Config) IntAnd ¶
IntAnd returns the (int value, true) by name if pattern matched, otherwise returns (0, false)
func (*Config) IntAndOr ¶
IntAndOr returns the int value by name if pattern matched, otherwise returns the deflt.
func (*Config) KV ¶
KV returns the Config's internal data as a string map. Excludes the flags and environment variables.
func (*Config) Merge ¶
Merge merges data from another Config, the value from same name will be replaced.
func (*Config) MergeFromFile ¶
MergeFromFile merges config data from file, the new config will replace the old. File extension must be one of ".yaml", ".yaml" or ".json".
func (*Config) MergeFromJSON ¶
MergeFromJSON merges data from JSON bytes, the value from same name will be replaced.
func (*Config) MergeFromYAML ¶
MergeFromYAML merges data from YAML bytes, the value from same name will be replaced.
func (*Config) ParseFlags ¶
func (c *Config) ParseFlags()
ParseFlags parse the flags explicitly, in genral, you don't.
func (*Config) Raw ¶
Raw returns the raw value by name. Excludes the flags and environment variable.
func (*Config) SetDefault ¶
SetDefault set the default value by name if not found.
func (*Config) StringAnd ¶
StringAnd returns the (string value, true) if pattern matched, otherwise returns ("", false).
func (*Config) StringAndOr ¶
StringAndOr returns the string value by name if pattern matched, otherwise returns the deflt.
type Configer ¶
type Configer interface {
KV() map[string]interface{}
Has(name string) bool
Must(name string)
Raw(name string) interface{}
Value(name string) Valuer
Config(name string) Configer
Pattern(name string) Patterner
SetDefault(name string, value interface{})
Set(name string, value interface{})
String(name string) string
StringOr(name string, deflt string) string
StringAnd(name string, pattern string) (string, bool)
StringAndOr(name string, pattern string, deflt string) string
Bool(name string) bool
BoolOr(name string, deflt bool) bool
Int(name string) int
IntOr(name string, deflt int) int
IntAnd(name string, pattern string) (int, bool)
IntAndOr(name string, pattern string, deflt int) int
Int64(name string) int64
Int64Or(name string, deflt int64) int64
Int64And(name string, pattern string) (int64, bool)
Int64AndOr(name string, pattern string, deflt int64) int64
Float(name string) float64
FloatOr(name string, deflt float64) float64
FloatAnd(name string, pattern string) (float64, bool)
FloatAndOr(name string, pattern string, deflt float64) float64
Duration(name string) time.Duration
DurationOr(name string, deflt int64) time.Duration
DurationAnd(name string, pattern string) (time.Duration, bool)
DurationAndOr(name string, pattern string, deflt int64) time.Duration
}
Configer is a abstraction for config.
type Pattern ¶
type Pattern struct {
// contains filtered or unexported fields
}
Pattern implements the Patterner interface.
func NewPattern ¶
NewPattern creates a new Pattern, even if pattern is not valid, in such case, Validate-like methods always return false. The compiled pattern is cached.
func (*Pattern) ValidateFloat ¶
ValidateFloat validate the float64 value n, return true if it is valid.
func (*Pattern) ValidateInt ¶
ValidateInt validate the int value n, return true if it is valid.
func (*Pattern) ValidateString ¶
ValidateString validate the string value n, return true if it is valid.
type Patterner ¶
type Patterner interface {
Err() error
ValidateInt(n int) bool
ValidateFloat(n float64) bool
ValidateString(s string) bool
}
Patterner is abstraction which do validation work. if pattern is not valid, then the following methods will always return false.
string pattern use the native regular expression to validate the value.
int(time.Duration) and float64 pattern use the basic if-like conditions to calculate and validate the value, use 'N' as placeholder for number, bit operation is not supported, for example:
"N>2" "N>1&&N<=5" "N<1||N>3" "(N%2==0)&&(N<=4||N>=8)"
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value implements the Valuer interface.
func (*Value) Config ¶
Config returns the value as a Configer, the modification on returned Configer has no affect to the origin value.
func (*Value) Duration ¶
Duration returns the time.Duration value, returns time.Duration(0) if not exists.
func (*Value) DurationAnd ¶
DurationAnd returns the (time.Duration(value), true) if pattern matched, otherwise (time.Duration(0), false) returned. NOTE: we convert all numbers into float64 then validate.
func (*Value) DurationAndOr ¶
DurationAndOr returns the time.Duration value if pattern matched, otherwise returns the deflt. NOTE: we convert all numbers into float64 then validate.
func (*Value) DurationOr ¶
DurationOr returns the time.Duration value, returns time.Duration(deflt) if not exists.
func (*Value) FloatAnd ¶
FloatAnd returns the (float64 value, true) if pattern matched, otherwise returns (0.0, false).
func (*Value) FloatAndOr ¶
FloatAndOr returns the float64 value if pattern matched, otherwise returns the deflt.
func (*Value) Int64And ¶
Int64And returns the (int64 value, true) if pattern matched, otherwise returns (0, false). NOTE: we convert all numbers into float64 then validate.
func (*Value) Int64AndOr ¶
Int64AndOr returns the int value if pattern matched, otherwise returns the deflt. NOTE: we convert all numbers into float64 then validate.
func (*Value) IntAnd ¶
IntAnd returns the (int value, true) if pattern matched, otherwise returns (0, false).
func (*Value) IntAndOr ¶
IntAndOr returns the int value if pattern matched, otherwise returns the deflt.
func (*Value) List ¶
List returns the value as slice, the modification on returned slice has no affect to the origin value.
func (*Value) Map ¶
Map returns the value as a map, the modification on returned map has no affect to the origin value.
func (*Value) StringAnd ¶
StringAnd returns the (string value, true) if pattern matched, otherwise returns ("", false).
func (*Value) StringAndOr ¶
StringAndOr returns the string value if pattern matched, otherwise returns the deflt.
type Valuer ¶
type Valuer interface {
Exist() bool
Raw() interface{}
Config() Configer
Pattern() Patterner
Map() map[string]Valuer
List() []Valuer
String() string
StringOr(deflt string) string
StringAnd(pattern string) (string, bool)
StringAndOr(pattern string, deflt string) string
Bool() bool
BoolOr(deflt bool) bool
Int() int
IntOr(deflt int) int
IntAnd(pattern string) (int, bool)
IntAndOr(pattern string, deflt int) int
Int64() int64
Int64Or(deflt int64) int64
Int64And(pattern string) (int64, bool)
Int64AndOr(pattern string, deflt int64) int64
Float() float64
FloatOr(deflt float64) float64
FloatAnd(pattern string) (float64, bool)
FloatAndOr(pattern string, deflt float64) float64
Duration() time.Duration
DurationOr(deflt int64) time.Duration
DurationAnd(pattern string) (time.Duration, bool)
DurationAndOr(pattern string, deflt int64) time.Duration
}
Valuer is a abstraction for config value, which can convert into multiple types.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package rpn defines a kind of condition pattern just like normal if condition in Golang, which trasfer string pattern to normal if condition.
|
Package rpn defines a kind of condition pattern just like normal if condition in Golang, which trasfer string pattern to normal if condition. |