aladin

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2020 License: Apache-2.0 Imports: 8 Imported by: 0

README

⚜️ aladin

Aladin 是一个配置热更新 Golang 包。

Build Status

🚀 Installation

go get github.com/mivinci/aladin@latest

🚗 Quick Start

配置结构体

type appConfig struct {
    Host string
    Port int
}

配置文件 conf/app.yml

host: localhost
port: 8000

通过 aladin.Watch 方法讲该结构体与配置文件里的字段绑定,只要配置文件内容更新,结构体的字段值也会更新。

c := appConfig{}
aladin.Init() // 初始化 aladin
aladin.Watch("conf/app.yml", &c)

完整示例代码见 _examples

📖 Usage

aladdin 默认使用 YAML 做配置文件,若想使用 TOML 或 JSON,可以自定义文件反序列插件。

以 JSON 为例

client := NewClient(aladin.JSONCodec{})
client.Watch("app.json", &c)

也可以使用自己的反序列插件,只要实现了下面接口,就可以传给 NewClient

type Codec interface {
    Unmarshal([]byte, interface{}) error
}

以 TOML 为例

type TOMLCodec struct{}
// 实现借口
func (TOMLCodec) Unmarshal(data []byte, dst interface{}) error {
  // 实现反序列化
}

client := NewClient(TOMLCodec{})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close() error

Close closes

func Init

func Init()

Init initializes

func Watch

func Watch(path string, dst interface{})

Watch watches

Types

type Client

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

Client client

var DefaultClient *Client

DefaultClient default client

func NewClient

func NewClient(codec Codec) *Client

NewClient new

func (*Client) Close

func (c *Client) Close() error

Close closes

func (*Client) Watch

func (c *Client) Watch(path string, dst interface{})

Watch watches

type Codec

type Codec interface {
	Unmarshal([]byte, interface{}) error
}

Codec decoder

type JSONCodec

type JSONCodec struct{}

JSONCodec json codec

func (JSONCodec) Unmarshal

func (JSONCodec) Unmarshal(data []byte, dst interface{}) error

Unmarshal unmarshals

type Map

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

Map map

func NewMap

func NewMap() Map

NewMap new

func (*Map) Get

func (m *Map) Get(key string) []byte

Get gets value by the given key

func (*Map) On

func (m *Map) On(key string, fn MapFunc)

On add a callback function to the given key Would it be better to name this method "OnChange" ?

func (*Map) Set

func (m *Map) Set(key string, value []byte)

Set sets new pair of data and start goroutines that emits callback functions that accord with the given key

type MapFunc

type MapFunc func([]byte)

MapFunc map callback function

type TOMLCodec

type TOMLCodec struct{}

TOMLCodec toml codec

func (TOMLCodec) Unmarshal

func (TOMLCodec) Unmarshal(data []byte, dst interface{}) error

Unmarshal unmarshals

type Watcher

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

Watcher file watcher

func NewWatcher

func NewWatcher() *Watcher

NewWatcher new

func (*Watcher) Close

func (fw *Watcher) Close() error

Close closes

func (*Watcher) Read

func (fw *Watcher) Read(path string) (data []byte, err error)

Read reads

func (*Watcher) Watch

func (fw *Watcher) Watch(fn WatcherFunc)

Watch watches

type WatcherFunc

type WatcherFunc func(string, []byte)

WatcherFunc watcher function

type YAMLCodec

type YAMLCodec struct{}

YAMLCodec yaml codec

func (YAMLCodec) Unmarshal

func (YAMLCodec) Unmarshal(data []byte, dst interface{}) error

Unmarshal unmarshals

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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