Skip to content

Commit

Permalink
feat: add new method: MapOnExists - mapping data to the dst struct on…
Browse files Browse the repository at this point in the history
…ly on key exists
  • Loading branch information
inhere committed Mar 14, 2022
1 parent c5f64c3 commit f1ab0eb
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ Golang's application config manage tool library.

## Usage

**Install**:

```bash
go get github.com/gookit/config/v2
```

Here using the yaml format as an example(`testdata/yml_other.yml`):

```yaml
Expand Down Expand Up @@ -126,6 +132,8 @@ Can use empty string for bind all config data to a struct:
config.BindStruct("", &myConf)
```

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

- Get integer
Expand Down Expand Up @@ -292,7 +300,11 @@ type Options struct {
- `Strings(key string) (arr []string)`
- `StringMap(key string) (mp map[string]string)`
- `Get(key string, findByPath ...bool) (value interface{})`

**Mapping data to struct:**

- `BindStruct(key string, dst interface{}) error`
- `MapOnExists(key string, dst interface{}) error`

### Setting Values

Expand Down
12 changes: 12 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@

## 快速使用

**获取包**:

```bash
go get github.com/gookit/config/v2
```

这里使用yaml格式作为示例(`testdata/yml_other.yml`):

```yaml
Expand Down Expand Up @@ -126,6 +132,8 @@ config.WithOptions(func(opt *Options) {
config.BindStruct("", &myConf)
```

> `config.MapOnExists``BindStruct` 一样,但仅当 key 存在时才进行映射绑定
### 快速获取数据

```go
Expand Down Expand Up @@ -270,7 +278,11 @@ type Options struct {
- `Strings(key string) (arr []string)`
- `StringMap(key string) (mp map[string]string)`
- `Get(key string, findByPath ...bool) (value interface{})`

**将数据映射到结构体:**

- `BindStruct(key string, dst interface{}) error`
- `MapOnExists(key string, dst interface{}) error`

### 设置值

Expand Down
17 changes: 17 additions & 0 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

// MapStruct alias method of the 'Structure'
//
// Usage:
// dbInfo := &Db{}
// config.MapStruct("db", dbInfo)
Expand All @@ -28,7 +29,23 @@ func (c *Config) BindStruct(key string, dst interface{}) error {
return c.Structure(key, dst)
}

// MapOnExists mapping data to the dst structure only on key exists.
func MapOnExists(key string, dst interface{}) error {
return dc.MapOnExists(key, dst)
}

// MapOnExists mapping data to the dst structure only on key exists.
func (c *Config) MapOnExists(key string, dst interface{}) error {
err := c.Structure(key, dst)
if err != nil && err == errNotFound {
return nil
}

return err
}

// Structure get config data and binding to the dst structure.
//
// Usage:
// dbInfo := Db{}
// config.Structure("db", &dbInfo)
Expand Down
25 changes: 24 additions & 1 deletion export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestConfig_Structure(t *testing.T) {

some := struct {
Age int
Kye string
Key string
Tags []int
}{}
err = BindStruct("sec", &some)
Expand Down Expand Up @@ -228,3 +228,26 @@ func TestMapStruct_embedded_struct_squash_true(t *testing.T) {
dump.Println(cfg1)
assert.Equal(t, 34, cfg1.Test1.B)
}

func TestMapOnExists(t *testing.T) {
cfg := Default()
cfg.ClearAll()

err := cfg.LoadStrings(JSON, `{
"age": 28,
"name": "inhere",
"sports": ["pingPong", "跑步"]
}`)
assert.NoError(t, err)
assert.NoError(t, MapOnExists("not-exists", nil))

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

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

0 comments on commit f1ab0eb

Please sign in to comment.