You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by la...@apache.org on 2021/12/02 03:13:39 UTC

[dubbo-go] branch 3.0 updated: add:support file extension (#1626)

This is an automated email from the ASF dual-hosted git repository.

laurence pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new f7f1581  add:support file extension (#1626)
f7f1581 is described below

commit f7f15810cef0a85d4e538170bb3f1ea04502df11
Author: zhaoyunxing <zh...@apache.org>
AuthorDate: Thu Dec 2 11:13:34 2021 +0800

    add:support file extension (#1626)
---
 config/config_center_config.go       | 15 ++++++---------
 config/config_loader_options.go      | 12 +++++++++---
 config/config_loader_options_test.go | 11 +++++++++++
 3 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/config/config_center_config.go b/config/config_center_config.go
index 00cb05b..778e644 100644
--- a/config/config_center_config.go
+++ b/config/config_center_config.go
@@ -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"
 )
@@ -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
@@ -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
 }
 
diff --git a/config/config_loader_options.go b/config/config_loader_options.go
index 4994103..92ab4ea 100644
--- a/config/config_loader_options.go
+++ b/config/config_loader_options.go
@@ -18,7 +18,6 @@
 package config
 
 import (
-	"fmt"
 	"io/ioutil"
 	"os"
 	"path/filepath"
@@ -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 {
 
@@ -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 {
-		return errors.New(fmt.Sprintf("no support %s", genre))
+		return errors.Errorf("no support file extension: %s", genre)
 	}
 	return nil
 }
diff --git a/config/config_loader_options_test.go b/config/config_loader_options_test.go
index 8943cc3..0ba55e0 100644
--- a/config/config_loader_options_test.go
+++ b/config/config_loader_options_test.go
@@ -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)
+}