Merge pull request #1127 from Allenxuxu/master

handle Loader.Load  return value error
This commit is contained in:
Asim Aslam 2020-01-29 22:44:29 +00:00 committed by GitHub
commit 9f1a7e1139
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 13 deletions

View File

@ -43,11 +43,11 @@ type Option func(o *Options)
var ( var (
// Default Config Manager // Default Config Manager
DefaultConfig = NewConfig() DefaultConfig, _ = NewConfig()
) )
// NewConfig returns new config // NewConfig returns new config
func NewConfig(opts ...Option) Config { func NewConfig(opts ...Option) (Config, error) {
return newConfig(opts...) return newConfig(opts...)
} }

View File

@ -30,7 +30,7 @@ type watcher struct {
value reader.Value value reader.Value
} }
func newConfig(opts ...Option) Config { func newConfig(opts ...Option) (Config, error) {
options := Options{ options := Options{
Loader: memory.NewLoader(), Loader: memory.NewLoader(),
Reader: json.NewReader(), Reader: json.NewReader(),
@ -40,9 +40,18 @@ func newConfig(opts ...Option) Config {
o(&options) o(&options)
} }
options.Loader.Load(options.Source...) if err := options.Loader.Load(options.Source...); err != nil {
snap, _ := options.Loader.Snapshot() return nil, err
vals, _ := options.Reader.Values(snap.ChangeSet) }
snap, err := options.Loader.Snapshot()
if err != nil {
return nil, err
}
vals, err := options.Reader.Values(snap.ChangeSet)
if err != nil {
return nil, err
}
c := &config{ c := &config{
exit: make(chan bool), exit: make(chan bool),
@ -53,7 +62,7 @@ func newConfig(opts ...Option) Config {
go c.run() go c.run()
return c return c, nil
} }
func (c *config) run() { func (c *config) run() {

View File

@ -55,7 +55,10 @@ func TestConfigLoadWithGoodFile(t *testing.T) {
}() }()
// Create new config // Create new config
conf := NewConfig() conf, err := NewConfig()
if err != nil {
t.Fatalf("Expected no error but got %v", err)
}
// Load file source // Load file source
if err := conf.Load(file.NewSource( if err := conf.Load(file.NewSource(
file.WithPath(path), file.WithPath(path),
@ -73,9 +76,12 @@ func TestConfigLoadWithInvalidFile(t *testing.T) {
}() }()
// Create new config // Create new config
conf := NewConfig() conf, err := NewConfig()
if err != nil {
t.Fatalf("Expected no error but got %v", err)
}
// Load file source // Load file source
err := conf.Load(file.NewSource( err = conf.Load(file.NewSource(
file.WithPath(path), file.WithPath(path),
file.WithPath("/i/do/not/exists.json"), file.WithPath("/i/do/not/exists.json"),
)) ))
@ -105,13 +111,18 @@ func TestConfigMerge(t *testing.T) {
}() }()
os.Setenv("AMQP_HOST", "rabbit.testing.com") os.Setenv("AMQP_HOST", "rabbit.testing.com")
conf := NewConfig() conf, err := NewConfig()
conf.Load( if err != nil {
t.Fatalf("Expected no error but got %v", err)
}
if err := conf.Load(
file.NewSource( file.NewSource(
file.WithPath(path), file.WithPath(path),
), ),
env.NewSource(), env.NewSource(),
) ); err != nil {
t.Fatalf("Expected no error but got %v", err)
}
actualHost := conf.Get("amqp", "host").String("backup") actualHost := conf.Get("amqp", "host").String("backup")
if actualHost != "rabbit.testing.com" { if actualHost != "rabbit.testing.com" {