cc

package module
Version: v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2017 License: BSD-3-Clause Imports: 12 Imported by: 2

README

cc - Golang Configuration Management for Humans™

Build Status Go Report Card GoDoc

Only support JSON and YAML.

Installation

go get -u github.com/damnever/cc

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 (import "flag")
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 flag, 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 && 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.

LICENSE

The BSD 3-Clause License

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

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 NewConfig

func NewConfig() *Config

NewConfig creates a new empty Config.

func NewConfigFrom

func NewConfigFrom(kv map[string]interface{}) *Config

NewConfigFrom creates a new Config from map.

func NewConfigFromFile

func NewConfigFromFile(fpath string) (*Config, error)

NewConfigFromFile creates a Config from a config file, extension must be one of ".yaml", ".yml" or ".json".

func NewConfigFromJSON

func NewConfigFromJSON(data []byte) (*Config, error)

NewConfigFromJSON creates a new Config from JSON bytes.

func NewConfigFromYAML

func NewConfigFromYAML(data []byte) (*Config, error)

NewConfigFromYAML creates a new Config from YAML bytes.

func (*Config) Bool

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

Bool returns the bool value by name, returns false if not found.

func (*Config) BoolOr

func (c *Config) BoolOr(name string, deflt bool) bool

BoolOr returns the bool value by name, returns the deflt if not found.

func (*Config) Config

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

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

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

Duration returns the time.Duration value by name, return time.Duration(0) if not found.

func (*Config) DurationAnd

func (c *Config) DurationAnd(name string, pattern string) (time.Duration, bool)

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

func (c *Config) DurationAndOr(name string, pattern string, deflt int64) time.Duration

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

func (c *Config) DurationOr(name string, deflt int64) time.Duration

DurationOr returns the time.Duration value by name, return time.Duration(deflt) if not found.

func (*Config) Float

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

Float returns the float64 value by name, return 0.0 if not found.

func (*Config) FloatAnd

func (c *Config) FloatAnd(name string, pattern string) (float64, bool)

FloatAnd returns the (float64 value, true) if pattern matched, otherwise (0.0, false) returned.

func (*Config) FloatAndOr

func (c *Config) FloatAndOr(name string, pattern string, deflt float64) float64

FloatAndOr returns the float64 value by name if pattern matched, otherwise returns the deflt.

func (*Config) FloatOr

func (c *Config) FloatOr(name string, deflt float64) float64

FloatOr returns the float64 value by name, return deflt if not found.

func (*Config) Has

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

Has returns true if the name has a value, otherwise false.

func (*Config) Int

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

Int returns the int value by name, returns 0 if not found.

func (*Config) Int64

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

Int64 returns the int64 value by name, returns 0 if not found.

func (*Config) Int64And

func (c *Config) Int64And(name string, pattern string) (int64, bool)

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

func (c *Config) Int64AndOr(name string, pattern string, deflt int64) int64

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) Int64Or

func (c *Config) Int64Or(name string, deflt int64) int64

Int64Or returns the int64 value by name, returns the deflt if not found.

func (*Config) IntAnd

func (c *Config) IntAnd(name string, pattern string) (int, bool)

IntAnd returns the (int value, true) by name if pattern matched, otherwise returns (0, false)

func (*Config) IntAndOr

func (c *Config) IntAndOr(name string, pattern string, deflt int) int

IntAndOr returns the int value by name if pattern matched, otherwise returns the deflt.

func (*Config) IntOr

func (c *Config) IntOr(name string, deflt int) int

IntOr returns the int value by name, returns the deflt if not found.

func (*Config) KV

func (c *Config) KV() map[string]interface{}

KV returns the Config's internal data as a string map. Excludes the flags and environment variables.

func (*Config) Merge

func (c *Config) Merge(config *Config) error

Merge merges data from another Config, the value from same name will be replaced.

func (*Config) MergeFromFile

func (c *Config) MergeFromFile(fpath string) error

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

func (c *Config) MergeFromJSON(b []byte) error

MergeFromJSON merges data from JSON bytes, the value from same name will be replaced.

func (*Config) MergeFromYAML

func (c *Config) MergeFromYAML(b []byte) error

MergeFromYAML merges data from YAML bytes, the value from same name will be replaced.

func (*Config) Must

func (c *Config) Must(name string)

Must creates panic if name not found.

func (*Config) ParseFlags

func (c *Config) ParseFlags()

ParseFlags parse the flags explicitly, in genral, you don't.

func (*Config) Pattern

func (c *Config) Pattern(name string) Patterner

Pattern returns a Patterner by name.

func (*Config) Raw

func (c *Config) Raw(name string) interface{}

Raw returns the raw value by name. Excludes the flags and environment variable.

func (*Config) Set

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

Set set the value by name, it will replace the exist value.

func (*Config) SetDefault

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

SetDefault set the default value by name if not found.

func (*Config) String

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

String returns the string value by name, returns "" if not found.

func (*Config) StringAnd

func (c *Config) StringAnd(name string, pattern string) (string, bool)

StringAnd returns the (string value, true) if pattern matched, otherwise returns ("", false).

func (*Config) StringAndOr

func (c *Config) StringAndOr(name string, pattern string, deflt string) string

StringAndOr returns the string value by name if pattern matched, otherwise returns the deflt.

func (*Config) StringOr

func (c *Config) StringOr(name string, deflt string) string

StringOr returns the string value by name, returns the deflt if not found.

func (*Config) Value

func (c *Config) Value(name string) Valuer

Value returns a Valuer by name. Excludes the flags and environment variable.

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

func NewPattern(pattern string) *Pattern

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) Err

func (p *Pattern) Err() error

Err returns the error if pattern is wrong.

func (*Pattern) ValidateFloat

func (p *Pattern) ValidateFloat(n float64) bool

ValidateFloat validate the float64 value n, return true if it is valid.

func (*Pattern) ValidateInt

func (p *Pattern) ValidateInt(n int) bool

ValidateInt validate the int value n, return true if it is valid.

func (*Pattern) ValidateString

func (p *Pattern) ValidateString(s string) bool

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 NewValue

func NewValue(v interface{}) *Value

NewValue creates a new Value.

func (*Value) Bool

func (v *Value) Bool() bool

Bool returns the bool value, returns false if not exists.

func (*Value) BoolOr

func (v *Value) BoolOr(deflt bool) bool

BoolOr returns the bool value, returns the deflt if not exists.

func (*Value) Config

func (v *Value) Config() Configer

Config returns the value as a Configer, the modification on returned Configer has no affect to the origin value.

func (*Value) Duration

func (v *Value) Duration() time.Duration

Duration returns the time.Duration value, returns time.Duration(0) if not exists.

func (*Value) DurationAnd

func (v *Value) DurationAnd(pattern string) (time.Duration, bool)

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

func (v *Value) DurationAndOr(pattern string, deflt int64) time.Duration

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

func (v *Value) DurationOr(deflt int64) time.Duration

DurationOr returns the time.Duration value, returns time.Duration(deflt) if not exists.

func (*Value) Exist

func (v *Value) Exist() bool

Exist returns true is value is a valid value, otherwise false.

func (*Value) Float

func (v *Value) Float() float64

Float returns the float64 value, returns 0.0 if not exists.

func (*Value) FloatAnd

func (v *Value) FloatAnd(pattern string) (float64, bool)

FloatAnd returns the (float64 value, true) if pattern matched, otherwise returns (0.0, false).

func (*Value) FloatAndOr

func (v *Value) FloatAndOr(pattern string, deflt float64) float64

FloatAndOr returns the float64 value if pattern matched, otherwise returns the deflt.

func (*Value) FloatOr

func (v *Value) FloatOr(deflt float64) float64

FloatOr returns the float64 value, return the deflt if not exists.

func (*Value) GoString

func (v *Value) GoString() string

GoString implements the native format for Value

func (*Value) Int

func (v *Value) Int() int

Int returns the int value, returns 0 if not exists.

func (*Value) Int64

func (v *Value) Int64() int64

Int64 returns the int64 value, returns 0 if not exists.

func (*Value) Int64And

func (v *Value) Int64And(pattern string) (int64, bool)

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

func (v *Value) Int64AndOr(pattern string, deflt int64) int64

Int64AndOr returns the int value if pattern matched, otherwise returns the deflt. NOTE: we convert all numbers into float64 then validate.

func (*Value) Int64Or

func (v *Value) Int64Or(deflt int64) int64

Int64Or returns the int64 value, returns the deflt if not exists.

func (*Value) IntAnd

func (v *Value) IntAnd(pattern string) (int, bool)

IntAnd returns the (int value, true) if pattern matched, otherwise returns (0, false).

func (*Value) IntAndOr

func (v *Value) IntAndOr(pattern string, deflt int) int

IntAndOr returns the int value if pattern matched, otherwise returns the deflt.

func (*Value) IntOr

func (v *Value) IntOr(deflt int) int

IntOr returns the int value, returns the deflt if not exists.

func (*Value) List

func (v *Value) List() []Valuer

List returns the value as slice, the modification on returned slice has no affect to the origin value.

func (*Value) Map

func (v *Value) Map() map[string]Valuer

Map returns the value as a map, the modification on returned map has no affect to the origin value.

func (*Value) Pattern

func (v *Value) Pattern() Patterner

Pattern returns a Patterner.

func (*Value) Raw

func (v *Value) Raw() interface{}

Raw returns the raw value.

func (*Value) String

func (v *Value) String() string

String returns the string value, returns "" if not exists.

func (*Value) StringAnd

func (v *Value) StringAnd(pattern string) (string, bool)

StringAnd returns the (string value, true) if pattern matched, otherwise returns ("", false).

func (*Value) StringAndOr

func (v *Value) StringAndOr(pattern string, deflt string) string

StringAndOr returns the string value if pattern matched, otherwise returns the deflt.

func (*Value) StringOr

func (v *Value) StringOr(deflt string) string

StringOr returns the string value, returns the deflt if not exists.

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.

Jump to

Keyboard shortcuts

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