Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #1558: Set root config to global ptr in Init() function. #1564

Merged
merged 2 commits into from
Nov 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions config/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,16 @@ var (
func Load(opts ...LoaderConfOption) error {
// conf
conf := NewLoaderConf(opts...)
koan := GetConfigResolver(conf)
if err := koan.UnmarshalWithConf(rootConfig.Prefix(),
rootConfig, koanf.UnmarshalConf{Tag: "yaml"}); err != nil {
return err
if conf.rc == nil {
koan := GetConfigResolver(conf)
if err := koan.UnmarshalWithConf(rootConfig.Prefix(),
rootConfig, koanf.UnmarshalConf{Tag: "yaml"}); err != nil {
return err
}
} else {
rootConfig = conf.rc
}

if err := rootConfig.Init(); err != nil {
return err
}
Expand Down
11 changes: 11 additions & 0 deletions config/config_loader_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type loaderConf struct {
delim string
// config bytes
bytes []byte
// user provide rootConfig built by config api
rc *RootConfig
}

func NewLoaderConf(opts ...LoaderConfOption) *loaderConf {
Expand All @@ -60,6 +62,9 @@ func NewLoaderConf(opts ...LoaderConfOption) *loaderConf {
for _, opt := range opts {
opt.apply(conf)
}
if conf.rc != nil {
return conf
}
if len(conf.bytes) <= 0 {
bytes, err := ioutil.ReadFile(conf.path)
if err != nil {
Expand Down Expand Up @@ -105,6 +110,12 @@ func WithPath(path string) LoaderConfOption {
})
}

func WithRootConfig(rc *RootConfig) LoaderConfOption {
return loaderConfigFunc(func(conf *loaderConf) {
conf.rc = rc
})
}

func WithDelim(delim string) LoaderConfOption {
return loaderConfigFunc(func(conf *loaderConf) {
conf.delim = delim
Expand Down
6 changes: 6 additions & 0 deletions config/config_loader_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ func TestFileGenre(t *testing.T) {
conf := NewLoaderConf(WithPath("../config/testdata/config/properties/application.properties"))
assert.Equal(t, conf.genre, "properties")
}

func TestRootConfig(t *testing.T) {
rc := NewRootConfigBuilder().SetApplication(NewApplicationConfigBuilder().SetName("test-app").Build()).Build()
conf := NewLoaderConf(WithRootConfig(rc))
assert.Equal(t, conf.rc.Application.Name, "test-app")
}
2 changes: 2 additions & 0 deletions config/root_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ func registerPOJO() {
hessian.RegisterPOJO(&common.URL{})
}

// Init is to start dubbo-go framework, load local configuration, or read configuration from config-center if necessary.
// It's deprecated for user to call rootConfig.Init() manually, try config.Load(config.WithRootConfig(rootConfig)) instead.
func (rc *RootConfig) Init() error {
registerPOJO()
if err := rc.Logger.Init(); err != nil { // init default logger
Expand Down