kratos: github.com/bilibili/kratos/pkg/conf/paladin Index | Examples | Files

package paladin

import "github.com/bilibili/kratos/pkg/conf/paladin"

Index

Examples

Package Files

client.go default.go file.go helper.go map.go mock.go toml.go value.go

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.

func Bool Uses

func Bool(v *Value, def bool) bool

Bool return bool value.

func Close Uses

func Close() error

Close close watcher.

func Duration Uses

func Duration(v *Value, def time.Duration) time.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 Float32 Uses

func Float32(v *Value, def float32) float32

Float32 return float32 value.

func Float64 Uses

func Float64(v *Value, def float64) float64

Float64 return float32 value.

func Init Uses

func Init() (err error)

Init init config client.

func Int Uses

func Int(v *Value, def int) int

Int return int value.

func Int32 Uses

func Int32(v *Value, def int32) int32

Int32 return int32 value.

func Int64 Uses

func Int64(v *Value, def int64) int64

Int64 return int64 value.

func Keys Uses

func Keys() []string

Keys return values key.

func String Uses

func String(v *Value, def string) string

String return string value.

func Watch Uses

func Watch(key string, s Setter) error

Watch watch on a key. The configuration implements the setter interface, which is invoked when the configuration changes.

func WatchEvent Uses

func WatchEvent(ctx context.Context, keys ...string) <-chan Event

WatchEvent watch on multi keys. Events are returned when the configuration changes.

type Client Uses

type Client interface {
    Watcher
    Getter
}

Client is config client.

var (
    // DefaultClient default client.
    DefaultClient Client
)

ExampleClient is a example client usage. exmaple.toml:

bool = true
int = 100
float = 100.1
string = "text"
strings = ["a", "b", "c"]

Code:

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)
    }
}()

func NewFile Uses

func NewFile(base string) (Client, error)

NewFile new a config file client. conf = /data/conf/app/ conf = /data/conf/app/xxx.toml

type Event Uses

type Event struct {
    Event EventType
    Key   string
    Value string
}

Event is watch event.

type EventType Uses

type EventType int

EventType is config event.

const (
    // EventAdd config add event.
    EventAdd EventType = iota
    // EventUpdate config update event.
    EventUpdate
    // EventRemove config remove event.
    EventRemove
)

type Getter Uses

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 Uses

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

Map is config map, key(filename) -> value(file).

ExampleMap is a 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"]

Code:

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

func GetAll Uses

func GetAll() *Map

GetAll return all config map.

func (*Map) Exist Uses

func (m *Map) Exist(key string) bool

Exist check if values map exist a key.

func (*Map) Get Uses

func (m *Map) Get(key string) *Value

Get return get value by key.

func (*Map) Keys Uses

func (m *Map) Keys() []string

Keys return map keys.

func (*Map) Load Uses

func (m *Map) Load() map[string]*Value

Load returns the value set by the most recent Store.

func (*Map) Store Uses

func (m *Map) Store(values map[string]*Value)

Store sets the value of the Value to values map.

type Mock Uses

type Mock struct {
    C   chan Event
    *Map
}

Mock is Mock config client.

func NewMock Uses

func NewMock(vs map[string]string) *Mock

NewMock new a config mock client.

func (*Mock) Close Uses

func (m *Mock) Close() error

Close close watcher.

func (*Mock) GetAll Uses

func (m *Mock) GetAll() *Map

GetAll return value map.

func (*Mock) WatchEvent Uses

func (m *Mock) WatchEvent(ctx context.Context, key ...string) <-chan Event

WatchEvent watch multi key.

type Setter Uses

type Setter interface {
    Set(string) error
}

Setter is value setter.

type TOML Uses

type TOML = Map

TOML is toml map.

func (*TOML) Set Uses

func (m *TOML) Set(text string) error

Set set the map by value.

func (*TOML) UnmarshalText Uses

func (m *TOML) UnmarshalText(text []byte) error

UnmarshalText implemented toml.

type Value Uses

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

Value is config value, maybe a json/toml/ini/string file.

func Get Uses

func Get(key string) *Value

Get return value by key.

func (*Value) Bool Uses

func (v *Value) Bool() (bool, error)

Bool return bool value.

func (*Value) Duration Uses

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

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) Float32 Uses

func (v *Value) Float32() (float32, error)

Float32 return float32 value.

func (*Value) Float64 Uses

func (v *Value) Float64() (float64, error)

Float64 return float64 value.

func (*Value) Int Uses

func (v *Value) Int() (int, error)

Int return int value.

func (*Value) Int32 Uses

func (v *Value) Int32() (int32, error)

Int32 return int32 value.

func (*Value) Int64 Uses

func (v *Value) Int64() (int64, error)

Int64 return int64 value.

func (*Value) Raw Uses

func (v *Value) Raw() (string, error)

Raw return raw value.

func (*Value) Slice Uses

func (v *Value) Slice(dst interface{}) error

Slice scan a slcie interface, if slice has element it will be discard.

func (*Value) String Uses

func (v *Value) String() (string, error)

String return string value.

func (*Value) Unmarshal Uses

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 Uses

func (v *Value) UnmarshalJSON(dst interface{}) error

UnmarshalJSON unmarhsal json to struct.

func (*Value) UnmarshalTOML Uses

func (v *Value) UnmarshalTOML(dst interface{}) error

UnmarshalTOML unmarhsal toml to struct.

func (*Value) UnmarshalYAML Uses

func (v *Value) UnmarshalYAML(dst interface{}) error

type Watcher Uses

type Watcher interface {
    WatchEvent(context.Context, ...string) <-chan Event
    Close() error
}

Watcher is config watcher.

Package paladin imports 22 packages (graph). Updated 2019-07-11. Refresh now. Tools for package owners.