Skip to content

Commit

Permalink
🐛 fix: bind struct - if c.data is nil, directly return. see #162
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 5, 2023
1 parent abe21b0 commit 2d6eb56
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
6 changes: 5 additions & 1 deletion export.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ func (c *Config) MapOnExists(key string, dst any) error {
// config.Structure("db", &dbInfo)
func (c *Config) Structure(key string, dst any) error {
var data any
// binding all data
// binding all data on key is empty.
if key == "" {
// fix: if c.data is nil, directly return
if len(c.data) == 0 {
return nil
}
data = c.data
} else {
// binding sub-data of the config
Expand Down
26 changes: 25 additions & 1 deletion issues_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package config_test

import (
"fmt"
"os"
"testing"
"time"
"fmt"

"github.com/gookit/config/v2"
"github.com/gookit/config/v2/ini"
Expand Down Expand Up @@ -461,3 +461,27 @@ func TestDuration(t *testing.T) {
is.Equal(float64(seconds), dur.Duration.Seconds())
}
}

// https://github.com/gookit/config/issues/162
func TestIssues_162(t *testing.T) {
type Logger struct {
Name string `json:"name"`
LogFile string `json:"logFile"`
MaxSize int `json:"maxSize" default:"1024"` // MB
MaxDays int `json:"maxDays" default:"7"`
Compress bool `json:"compress" default:"true"`
}

type LogConfig struct {
Loggers []*Logger `default:""` // mark for parse default
}

c := config.New("issues_162", config.ParseDefault)
err := c.LoadStrings(config.JSON, `{}`)
assert.NoErr(t, err)

opt := &LogConfig{}
err = c.Decode(opt)
// dump.Println(opt)
assert.Empty(t, opt.Loggers)
}

0 comments on commit 2d6eb56

Please sign in to comment.