paladin

package
v0.1.41 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2020 License: MIT Imports: 23 Imported by: 0

README

paladin

项目简介

paladin 是一个config SDK客户端,包括了file、mock几个抽象功能,方便使用本地文件或者sven\apollo配置中心,并且集成了对象自动reload功能。

local files:

demo -conf=/data/conf/app/msm-servie.toml
// or dir
demo -conf=/data/conf/app/

注:使用远程配置中心的用户在执行应用,如这里的demo时务必不要带上-conf参数,具体见下文远程配置中心的例子

local file example:

type exampleConf struct {
	Bool   bool
	Int    int64
	Float  float64
	String 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 ExampleClient() {
	if err := paladin.Init(); err != nil {
		panic(err)
	}
	var (
		ec   exampleConf
		eo   exampleConf
		m    paladin.TOML
		strs []string
	)
	// config unmarshal
	if err := paladin.Get("example.toml").UnmarshalTOML(&ec); err != nil {
		panic(err)
	}
	// config setter
	if err := paladin.Watch("example.toml", &ec); err != nil {
        panic(err)
    }
	// paladin map
	if err := paladin.Watch("example.toml", &m); err != nil {
        panic(err)
    }
	s, err := m.Value("key").String()
	b, err := m.Value("key").Bool()
	i, err := m.Value("key").Int64()
	f, err := m.Value("key").Float64()
	// value slice
	err = m.Value("strings").Slice(&strs)
	// watch key
	for event := range paladin.WatchEvent(context.TODO(), "key") {
		fmt.Println(event)
	}
}

remote config center example:

type exampleConf struct {
	Bool   bool
	Int    int64
	Float  float64
	String string
}

func (e *exampleConf) Set(text string) error {
	var ec exampleConf
	if err := yaml.Unmarshal([]byte(text), &ec); err != nil {
		return err
	}
	*e = ec
	return nil
}

func ExampleApolloClient() {
	/*
		pass flags or set envs that apollo needs, for example:

		```
		export APOLLO_APP_ID=SampleApp
		export APOLLO_CLUSTER=default
		export APOLLO_CACHE_DIR=/tmp
		export APOLLO_META_ADDR=localhost:8080
		export APOLLO_NAMESPACES=example.yml
		```
	*/

	if err := paladin.Init(apollo.PaladinDriverApollo); err != nil {
		panic(err)
	}
	var (
		ec   exampleConf
		eo   exampleConf
		m    paladin.Map
		strs []string
	)
	// config unmarshal
	if err := paladin.Get("example.yml").UnmarshalYAML(&ec); err != nil {
		panic(err)
	}
	// config setter
	if err := paladin.Watch("example.yml", &ec); err != nil {
        panic(err)
    }
	// paladin map
	if err := paladin.Watch("example.yml", &m); err != nil {
        panic(err)
    }
	s, err := m.Value("key").String()
	b, err := m.Value("key").Bool()
	i, err := m.Value("key").Int64()
	f, err := m.Value("key").Float64()
	// value slice
	err = m.Value("strings").Slice(&strs)
	// watch key
	for event := range paladin.WatchEvent(context.TODO(), "key") {
		fmt.Println(event)
	}
}

编译环境

  • 请只用 Golang v1.12.x 以上版本编译执行

依赖包

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
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 Bool

func Bool(v *Value, def bool) bool

Bool return bool value.

func Close

func Close() error

Close close watcher.

func Drivers added in v0.1.7

func Drivers() []string

Drivers returns a sorted list of the names of the registered paladin driver.

func Duration

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

func Float32(v *Value, def float32) float32

Float32 return float32 value.

func Float64

func Float64(v *Value, def float64) float64

Float64 return float32 value.

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 Int

func Int(v *Value, def int) int

Int return int value.

func Int32

func Int32(v *Value, def int32) int32

Int32 return int32 value.

func Int64

func Int64(v *Value, def int64) int64

Int64 return int64 value.

func KeyNamed added in v0.1.7

func KeyNamed(key string) string

KeyNamed key naming to lower case.

func Keys

func Keys() []string

Keys return values key.

func Register added in v0.1.7

func Register(name string, driver Driver)

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.

func String

func String(v *Value, def string) string

String return string value.

func Watch

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

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

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

Types

type Client

type Client interface {
	Watcher
	Getter
}

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/mapgoo-lab/atreus/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
)

func NewFile

func NewFile(base string) (Client, error)

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

func NewMock

func NewMock(vs map[string]string) Client

NewMock new a config mock client.

type Driver added in v0.1.7

type Driver interface {
	New() (Client, error)
}

Driver defined paladin remote client impl each remote config center driver must do 1. implements `New` method 2. call `Register` to register itself

func GetDriver added in v0.1.7

func GetDriver(name string) (Driver, error)

GetDriver returns a driver implement by name.

type Event

type Event struct {
	Event EventType
	Key   string
	Value string
}

Event is watch event.

type EventType

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

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/mapgoo-lab/atreus/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:

func GetAll

func GetAll() *Map

GetAll return all config map.

func (*Map) Exist

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

Exist check if values map exist a key.

func (*Map) Get

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

Get return get value by key.

func (*Map) Keys

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

Keys return map keys.

func (*Map) Load

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

Load returns the value set by the most recent Store.

func (*Map) Store

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

Store sets the value of the Value to values map.

type Mock

type Mock struct {
	C chan Event
	*Map
}

Mock is Mock config client.

func (*Mock) Close

func (m *Mock) Close() error

Close close watcher.

func (*Mock) GetAll

func (m *Mock) GetAll() *Map

GetAll return value map.

func (*Mock) WatchEvent

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

WatchEvent watch multi key.

type Setter

type Setter interface {
	Set(string) error
}

Setter is value setter.

type TOML

type TOML = Map

TOML is toml map.

func (*TOML) Set

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

Set set the map by value.

func (*TOML) UnmarshalText

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

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 Get

func Get(key string) *Value

Get return value by key.

func NewValue added in v0.1.7

func NewValue(val interface{}, raw string) *Value

NewValue new a value

func (*Value) Bool

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

Bool return bool value.

func (*Value) Duration

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

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

Float32 return float32 value.

func (*Value) Float64

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

Float64 return float64 value.

func (*Value) Int

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

Int return int value.

func (*Value) Int32

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

Int32 return int32 value.

func (*Value) Int64

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

Int64 return int64 value.

func (*Value) Raw

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

Raw return raw value.

func (*Value) Slice

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

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

func (*Value) String

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

String return string value.

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

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

UnmarshalJSON unmarhsal json to struct.

func (*Value) UnmarshalTOML

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

UnmarshalTOML unmarhsal toml to struct.

func (*Value) UnmarshalYAML

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

UnmarshalYAML unmarshal yaml to struct.

type Watcher

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

Watcher is config watcher.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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