Skip to content

Commit

Permalink
pref: add more unit tests, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 16, 2022
1 parent 35f7ece commit fe69183
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 4 deletions.
31 changes: 28 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ config.BindStruct("", &myConf)

> `config.MapOnExists` like `BindStruct`,but map binding only if key exists
### Direct Read data
### Direct read data

- Get integer

Expand Down Expand Up @@ -231,7 +231,7 @@ config.Bool("app_debug") // true
config.String("app_name") // "config"
```

## New Config Instance
## New config instance

You can create custom config instance

Expand All @@ -246,7 +246,32 @@ myConf := config.NewEmpty("my-conf")
myConf := config.NewWithOptions("my-conf", config.ParseEnv, config.ReadOnly)
```

## Available Options
## Listen config change

Now, you can add a hook func for listen config data change. then, you can do something like: write data to file

Add hook func on create config:

```go
hookFn := func(event string, c *Config) {
fmt.Println("fire the:", event)
}

c := NewWithOptions("test", WithHookFunc(hookFn))
// for global config
config.WithOptions(WithHookFunc(hookFn))
```

After that, when calling `LoadXXX, Set, SetData, ClearData` etc methods, it will output:

```text
fire the: load.data
fire the: set.value
fire the: set.data
fire the: clean.data
```

## Available options

```go
// Options config options
Expand Down
25 changes: 25 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,31 @@ myConf := config.NewEmpty("my-conf")
myConf := config.NewWithOptions("my-conf", config.ParseEnv, config.ReadOnly)
```

## 监听配置更改

现在,您可以添加一个挂钩函数来监听配置数据更改。然后,您可以执行一些自定义操作, 例如:将数据写入文件

在创建配置时添加钩子函数:

```go
hookFn := func(event string, c *Config) {
fmt.Println("fire the:", event)
}

c := NewWithOptions("test", WithHookFunc(hookFn))
// for global config
config.WithOptions(WithHookFunc(hookFn))
```

之后, 当调用 `LoadXXX, Set, SetData, ClearData` 等方法时, 就会输出:

```text
fire the: load.data
fire the: set.value
fire the: set.data
fire the: clean.data
```

## 可用选项

```go
Expand Down
11 changes: 11 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,17 @@ func TestDelimiter(t *testing.T) {
is.NoError(err)
// is.Equal(1, c.Int("top0"))
is.Equal(2, c.Int("top1:sub0"))

// load will use defaultDelimiter
c = NewWithOptions("test", Delimiter(0))
is.Equal(byte(0), c.Options().Delimiter)

err = c.LoadData(map[string]interface{}{
"top0": 1,
"top1": map[string]int{"sub0": 2},
})
is.NoError(err)
is.Equal(2, c.Int("top1.sub0"))
}

func TestEnableCache(t *testing.T) {
Expand Down
29 changes: 29 additions & 0 deletions export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,32 @@ func TestMapOnExists(t *testing.T) {
assert.Equal(t, 28, user.Age)
assert.Equal(t, "inhere", user.Name)
}

func TestConfig_BindStruct_set_DecoderConfig(t *testing.T) {
cfg := NewWith("test", func(c *Config) {
c.opts.DecoderConfig = nil
})
err := cfg.LoadStrings(JSON, `{
"age": 28,
"name": "inhere",
"sports": ["pingPong", "跑步"]
}`)
assert.NoError(t, err)

user := &struct {
Age int
Name string
Sports []string
}{}
assert.NoError(t, cfg.BindStruct("", user))

assert.Equal(t, 28, user.Age)
assert.Equal(t, "inhere", user.Name)

// not use ptr
assert.Error(t, cfg.BindStruct("", *user))
}

func TestConfig_BindStruct_error(t *testing.T) {
// cfg := NewEmpty()
}
2 changes: 1 addition & 1 deletion hclv2/hcl_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type hclDriver struct {
name string
}

// Name
// Name get
func (d *hclDriver) Name() string {
return d.name
}
Expand Down

0 comments on commit fe69183

Please sign in to comment.