diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b6fb3f1..e030d6ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +## [1.2.2] - 2024-07-08 + +### Fixed + +- Allow Config.Explain on empty Config (#408). + ## [1.2.1] - 2024-06-20 ### Fixed diff --git a/config.go b/config.go index 0eeef754..86ee2fdd 100644 --- a/config.go +++ b/config.go @@ -171,7 +171,7 @@ func (c *Config) transformKeys(m map[string]any) { // from loaders for the given path. It blur sensitive information. // The path is case-insensitive unless konf.WithCaseSensitive is set. func (c *Config) Explain(path string) string { - if c == nil { + if c == nil || c.values.Load() == nil { return path + " has no configuration.\n\n" } diff --git a/config_test.go b/config_test.go index c7f8b896..8910ce61 100644 --- a/config_test.go +++ b/config_test.go @@ -20,6 +20,14 @@ func TestConfig_nil(t *testing.T) { var value string assert.NoError(t, config.Unmarshal("key", &value)) assert.Equal(t, "", value) + assert.True(t, len(config.Explain("key")) > 0) + + config = konf.New() + assert.True(t, !config.Exists([]string{"key"})) + assert.Equal(t, "key has no configuration.\n\n", config.Explain("key")) + assert.NoError(t, config.Unmarshal("key", &value)) + assert.Equal(t, "", value) + assert.True(t, len(config.Explain("key")) > 0) } func TestConfig_Load(t *testing.T) { diff --git a/provider.go b/provider.go index e81173ac..e782f908 100644 --- a/provider.go +++ b/provider.go @@ -36,7 +36,7 @@ type Statuser interface { // // It's used by the loader to check if the configuration has been set by other loaders. func (c *Config) Exists(path []string) bool { - if c == nil { + if c == nil || c.values.Load() == nil { return false }