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

add:support file extension #1626

Merged
merged 1 commit into from
Dec 2, 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
15 changes: 6 additions & 9 deletions config/config_center_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import (
"github.com/creasty/defaults"

"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/rawbytes"

"github.com/pkg/errors"
)
Expand Down Expand Up @@ -61,6 +59,9 @@ type CenterConfig struct {
AppID string `default:"dubbo" yaml:"app-id" json:"app-id,omitempty"`
Timeout string `default:"10s" yaml:"timeout" json:"timeout,omitempty"`
Params map[string]string `yaml:"params" json:"parameters,omitempty"`

//FileExtension the suffix of config dataId, also the file extension of config content
FileExtension string `default:"yaml" yaml:"file-extension" json:"file-extension" `
}

// Prefix dubbo.config-center
Expand Down Expand Up @@ -146,15 +147,11 @@ func startConfigCenter(rc *RootConfig) error {
"Please check if your config-center config is correct.", cc)
return nil
}
koan := koanf.New(".")
if err = koan.Load(rawbytes.Provider([]byte(strConf)), yaml.Parser()); err != nil {
return err
}
if err = koan.UnmarshalWithConf(rc.Prefix(),
rc, koanf.UnmarshalConf{Tag: "yaml"}); err != nil {
config := NewLoaderConf(WithDelim("."), WithGenre(cc.FileExtension), WithBytes([]byte(strConf)))
koan := GetConfigResolver(config)
if err = koan.UnmarshalWithConf(rc.Prefix(), rc, koanf.UnmarshalConf{Tag: "yaml"}); err != nil {
return err
}

return nil
}

Expand Down
12 changes: 9 additions & 3 deletions config/config_loader_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package config

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -122,6 +121,13 @@ func WithDelim(delim string) LoaderConfOption {
})
}

// WithBytes set load config bytes
func WithBytes(bytes []byte) LoaderConfOption {
return loaderConfigFunc(func(conf *loaderConf) {
conf.bytes = bytes
})
}

// absolutePath get absolut path
func absolutePath(inPath string) string {

Expand Down Expand Up @@ -155,11 +161,11 @@ func userHomeDir() string {

// checkGenre check Genre
func checkGenre(genre string) error {
genres := []string{"json", "toml", "yaml", "yml"}
genres := []string{"json", "toml", "yaml", "yml", "properties"}
sort.Strings(genres)
idx := sort.SearchStrings(genres, genre)
if genres[idx] != genre {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的判断是不是需要判断边界情况 idx == len(genres) || genres[idx] != genre

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1,idx 有没有可能是 -1 的情况?

Copy link
Contributor Author

@zhaoyunxing92 zhaoyunxing92 Dec 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

源码里面就是一个二分搜索,不会存在-1的情况

func Search(n int, f func(int) bool) int {
	// Define f(-1) == false and f(n) == true.
	// Invariant: f(i-1) == false, f(j) == true.
	i, j := 0, n
	for i < j {
		h := int(uint(i+j) >> 1) // avoid overflow when computing h
		// i ≤ h < j
		if !f(h) {
			i = h + 1 // preserves f(i-1) == false
		} else {
			j = h // preserves f(j) == true
		}
	}
	// i == j, f(i-1) == false, and f(j) (= f(i)) == true  =>  answer is i.
	return i
}

return errors.New(fmt.Sprintf("no support %s", genre))
return errors.Errorf("no support file extension: %s", genre)
}
return nil
}
11 changes: 11 additions & 0 deletions config/config_loader_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,14 @@ func TestRootConfig(t *testing.T) {
conf := NewLoaderConf(WithRootConfig(rc))
assert.Equal(t, conf.rc.Application.Name, "test-app")
}

func TestNewLoaderConf_WithBytes(t *testing.T) {
str := `dubbo.application.name=dubbo-go
dubbo.application.module=local
dubbo.services.HelloService.registry=nacos,zk`

conf := NewLoaderConf(WithBytes([]byte(str)), WithGenre("properties"))

assert.NotNil(t, conf)
assert.NotNil(t, conf.bytes)
}