Documentation ¶
Index ¶
- Variables
- func Bool(v *Value, def bool) bool
- func Close() error
- func Drivers() []string
- func Duration(v *Value, def time.Duration) time.Duration
- func Float32(v *Value, def float32) float32
- func Float64(v *Value, def float64) float64
- func Init(args ...interface{}) (err error)
- func Int(v *Value, def int) int
- func Int32(v *Value, def int32) int32
- func Int64(v *Value, def int64) int64
- func KeyNamed(key string) string
- func Keys() []string
- func Register(name string, driver Driver)
- func String(v *Value, def string) string
- func Watch(key string, s Setter) error
- func WatchEvent(ctx context.Context, keys ...string) <-chan Event
- type Client
- type Driver
- type Event
- type EventType
- type Getter
- type Map
- type Mock
- type Setter
- type TOML
- type Value
- func (v *Value) Bool() (bool, error)
- func (v *Value) Duration() (time.Duration, error)
- func (v *Value) Float32() (float32, error)
- func (v *Value) Float64() (float64, error)
- func (v *Value) Int() (int, error)
- func (v *Value) Int32() (int32, error)
- func (v *Value) Int64() (int64, error)
- func (v *Value) Raw() (string, error)
- func (v *Value) Slice(dst interface{}) error
- func (v *Value) String() (string, error)
- func (v *Value) Unmarshal(un encoding.TextUnmarshaler) error
- func (v *Value) UnmarshalJSON(dst interface{}) error
- func (v *Value) UnmarshalTOML(dst interface{}) error
- func (v *Value) UnmarshalYAML(dst interface{}) error
- type Watcher
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotExist = errors.New("paladin: value key not exist") ErrTypeAssertion = errors.New("paladin: value type assertion no match") ErrDifferentTypes = errors.New("paladin: value different types") )
ErrNotExist value key not exist.
Functions ¶
func Drivers ¶
func Drivers() []string
Drivers returns a sorted list of the names of the registered paladin driver.
func Duration ¶
Duration parses a duration string. A duration string is a possibly signed sequence of decimal numbers each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
func Init ¶
func Init(args ...interface{}) (err error)
Init init config client. If confPath is set, it inits file client by default Otherwise we could pass args to init remote client args[0]: driver name, string type
func Register ¶
Register makes a paladin driver available by the provided name. If Register is called twice with the same name or if driver is nil, it panics.
Types ¶
type Client ¶
Client is config client.
Example ¶
ExampleClient is an example client usage. exmaple.toml:
bool = true int = 100 float = 100.1 string = "text" strings = ["a", "b", "c"]
package main import ( "context" "fmt" "github.com/huizluo/kratos/pkg/conf/paladin" "github.com/BurntSushi/toml" ) type exampleConf struct { Bool bool Int int64 Float float64 String string Strings []string } func (e *exampleConf) Set(text string) error { var ec exampleConf if err := toml.Unmarshal([]byte(text), &ec); err != nil { return err } *e = ec return nil } func main() { if err := paladin.Init(); err != nil { panic(err) } var ec exampleConf // var setter if err := paladin.Watch("example.toml", &ec); err != nil { panic(err) } if err := paladin.Get("example.toml").UnmarshalTOML(&ec); err != nil { panic(err) } // use exampleConf // watch event key go func() { for event := range paladin.WatchEvent(context.TODO(), "key") { fmt.Println(event) } }() }
Output:
var ( // DefaultClient default client. DefaultClient Client )
type Driver ¶
Driver defined paladin remote client impl each remote config center driver must do 1. implements `New` method 2. call `Register` to register itself
type Getter ¶
type Getter interface { // Get a config value by a config key(may be a sven filename). Get(string) *Value // GetAll return all config key->value map. GetAll() *Map }
Getter is value getter.
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
Map is config map, key(filename) -> value(file).
Example ¶
ExampleMap is an example map usage. exmaple.toml:
bool = true int = 100 float = 100.1 string = "text" strings = ["a", "b", "c"] [object] string = "text" bool = true int = 100 float = 100.1 strings = ["a", "b", "c"]
package main import ( "fmt" "github.com/huizluo/kratos/pkg/conf/paladin" ) func main() { var ( m paladin.TOML strs []string ) // paladin toml if err := paladin.Watch("example.toml", &m); err != nil { panic(err) } // value string s, err := m.Get("string").String() if err != nil { s = "default" } fmt.Println(s) // value bool b, err := m.Get("bool").Bool() if err != nil { b = false } fmt.Println(b) // value int i, err := m.Get("int").Int64() if err != nil { i = 100 } fmt.Println(i) // value float f, err := m.Get("float").Float64() if err != nil { f = 100.1 } fmt.Println(f) // value slice if err = m.Get("strings").Slice(&strs); err == nil { fmt.Println(strs) } }
Output:
type TOML ¶
type TOML = Map
TOML is toml map.
func (*TOML) UnmarshalText ¶
UnmarshalText implemented toml.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value is config value, maybe a json/toml/ini/string file.
func (*Value) Duration ¶
Duration parses a duration string. A duration string is a possibly signed sequence of decimal numbers each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
func (*Value) Unmarshal ¶
func (v *Value) Unmarshal(un encoding.TextUnmarshaler) error
Unmarshal is the interface implemented by an object that can unmarshal a textual representation of itself.
func (*Value) UnmarshalJSON ¶
UnmarshalJSON unmarhsal json to struct.
func (*Value) UnmarshalTOML ¶
UnmarshalTOML unmarhsal toml to struct.
func (*Value) UnmarshalYAML ¶
UnmarshalYAML unmarshal yaml to struct.