Skip to content

Commit

Permalink
fix: should clear last error after read by config.Error(). close: #76
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 1, 2022
1 parent e29f6af commit 3be2108
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
7 changes: 5 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ var dc = New("default")

// Config structure definition
type Config struct {
// save latest error, will clear after read.
err error
// config instance name
name string
Expand Down Expand Up @@ -189,9 +190,11 @@ func (c *Config) Name() string {
return c.name
}

// Error get last error
// Error get last error, will clear after read.
func (c *Config) Error() error {
return c.err
err := c.err
c.err = nil
return err
}

// IsEmpty of the config
Expand Down
9 changes: 9 additions & 0 deletions dotnev/dotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ var (
)

// LoadedData get all loaded data by dontenv
// Deprecated: please use github.com/gookit/ini/v2/dotenv
func LoadedData() map[string]string {
return dotenv.LoadedData()
}

// ClearLoaded clear the previously set ENV value
// Deprecated: please use github.com/gookit/ini/v2/dotenv
func ClearLoaded() {
dotenv.ClearLoaded()
}

// DontUpperEnvKey don't change key to upper on set ENV
// Deprecated: please use github.com/gookit/ini/v2/dotenv
func DontUpperEnvKey() {
dotenv.DontUpperEnvKey()
}
Expand All @@ -43,31 +46,37 @@ func DontUpperEnvKey() {
//
// Usage:
// dotenv.Load("./", ".env")
// Deprecated: please use github.com/gookit/ini/v2/dotenv
func Load(dir string, filenames ...string) (err error) {
return dotenv.Load(dir, filenames...)
}

// LoadExists only load on file exists
// Deprecated: please use github.com/gookit/ini/v2/dotenv
func LoadExists(dir string, filenames ...string) error {
return dotenv.LoadExists(dir, filenames...)
}

// LoadFromMap load data from given string map
// Deprecated: please use github.com/gookit/ini/v2/dotenv
func LoadFromMap(kv map[string]string) (err error) {
return dotenv.LoadFromMap(kv)
}

// Get get os ENV value by name
// Deprecated: please use github.com/gookit/ini/v2/dotenv
func Get(name string, defVal ...string) (val string) {
return dotenv.Get(name, defVal...)
}

// Bool get a bool value by key
// Deprecated: please use github.com/gookit/ini/v2/dotenv
func Bool(name string, defVal ...bool) (val bool) {
return dotenv.Bool(name, defVal...)
}

// Int get a int value by key
// Deprecated: please use github.com/gookit/ini/v2/dotenv
func Int(name string, defVal ...int) (val int) {
return dotenv.Int(name, defVal...)
}
26 changes: 26 additions & 0 deletions issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,29 @@ func TestIssues_59(t *testing.T) {
is.Contains(str, "name = app")
is.Contains(str, "key1 = val1")
}

// https://github.com/gookit/config/issues/76
func TestIssues_76(t *testing.T) {
is := assert.New(t)
c := config.New("test")

err := c.LoadStrings(config.JSON, `
{
"lang": {
"allowed": {
"en": "ddd"
}
},
"key0": 234
}
`)
is.NoError(err)

ss := c.Strings("key0")
is.Empty(ss)

lastErr := c.Error()
is.Error(lastErr)
is.Equal("value cannot be convert to []string, key is 'key0'", lastErr.Error())
is.NoError(c.Error())
}

0 comments on commit 3be2108

Please sign in to comment.