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

alexstocks 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 db09e49  FIX:Array boundary problem (#1630)
db09e49 is described below

commit db09e4962e8d8b0e4aca69f91a36f0d1e854c74e
Author: zhaoyunxing <[email protected]>
AuthorDate: Fri Dec 3 19:50:45 2021 +0800

    FIX:Array boundary problem (#1630)
    
    * fix:解决数组越界问题
    
    * up:代码格式化,注释完善
    
    * rm:replace WithGenre method
---
 common/file/suffix.go                | 33 ++++++++++++++++++++++
 config/config_loader_options.go      | 53 ++++++++++++++++++++++--------------
 config/config_loader_options_test.go | 22 +++++++++++++--
 config/config_resolver.go            |  9 +++---
 4 files changed, 89 insertions(+), 28 deletions(-)

diff --git a/common/file/suffix.go b/common/file/suffix.go
new file mode 100644
index 0000000..804f077
--- /dev/null
+++ b/common/file/suffix.go
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package file
+
+type Suffix string
+
+// file suffix
+const (
+       JSON = Suffix("json")
+
+       TOML = Suffix("toml")
+
+       YAML = Suffix("yaml")
+
+       YML = Suffix("yml")
+
+       PROPERTIES = Suffix("properties")
+)
diff --git a/config/config_loader_options.go b/config/config_loader_options.go
index 92ab4ea..d2cd1b7 100644
--- a/config/config_loader_options.go
+++ b/config/config_loader_options.go
@@ -22,7 +22,6 @@ import (
        "os"
        "path/filepath"
        "runtime"
-       "sort"
        "strings"
 )
 
@@ -32,17 +31,22 @@ import (
 
 import (
        "dubbo.apache.org/dubbo-go/v3/common/constant"
+       "dubbo.apache.org/dubbo-go/v3/common/file"
 )
 
 type loaderConf struct {
-       // loaderConf file type default yaml
-       genre string
+       // loaderConf file extension default yaml
+       suffix string
+
        // loaderConf file path default ./conf
        path string
+
        // loaderConf file delim default .
        delim string
+
        // config bytes
        bytes []byte
+
        // user provide rootConfig built by config api
        rc *RootConfig
 }
@@ -52,11 +56,11 @@ func NewLoaderConf(opts ...LoaderConfOption) *loaderConf {
        if configFilePathFromEnv := os.Getenv(constant.ConfigFileEnvKey); 
configFilePathFromEnv != "" {
                configFilePath = configFilePathFromEnv
        }
-       genre := strings.Split(configFilePath, ".")
+       suffix := strings.Split(configFilePath, ".")
        conf := &loaderConf{
-               genre: genre[len(genre)-1],
-               path:  absolutePath(configFilePath),
-               delim: ".",
+               suffix: suffix[len(suffix)-1],
+               path:   absolutePath(configFilePath),
+               delim:  ".",
        }
        for _, opt := range opts {
                opt.apply(conf)
@@ -84,14 +88,22 @@ func (fn loaderConfigFunc) apply(vc *loaderConf) {
        fn(vc)
 }
 
-// WithGenre set load config  genre
-func WithGenre(genre string) LoaderConfOption {
+// WithGenre set load config file suffix
+//Deprecated: replaced by WithSuffix
+func WithGenre(suffix string) LoaderConfOption {
        return loaderConfigFunc(func(conf *loaderConf) {
-               g := strings.ToLower(genre)
-               if err := checkGenre(g); err != nil {
+               g := strings.ToLower(suffix)
+               if err := checkFileSuffix(g); err != nil {
                        panic(err)
                }
-               conf.genre = g
+               conf.suffix = g
+       })
+}
+
+// WithSuffix set load config file suffix
+func WithSuffix(suffix file.Suffix) LoaderConfOption {
+       return loaderConfigFunc(func(conf *loaderConf) {
+               conf.suffix = string(suffix)
        })
 }
 
@@ -105,7 +117,7 @@ func WithPath(path string) LoaderConfOption {
                }
                conf.bytes = bytes
                genre := strings.Split(path, ".")
-               conf.genre = genre[len(genre)-1]
+               conf.suffix = genre[len(genre)-1]
        })
 }
 
@@ -159,13 +171,12 @@ func userHomeDir() string {
        return os.Getenv("HOME")
 }
 
-// checkGenre check Genre
-func checkGenre(genre string) error {
-       genres := []string{"json", "toml", "yaml", "yml", "properties"}
-       sort.Strings(genres)
-       idx := sort.SearchStrings(genres, genre)
-       if genres[idx] != genre {
-               return errors.Errorf("no support file extension: %s", genre)
+// checkFileSuffix check file suffix
+func checkFileSuffix(suffix string) error {
+       for _, g := range []string{"json", "toml", "yaml", "yml", "properties"} 
{
+               if g == suffix {
+                       return nil
+               }
        }
-       return nil
+       return errors.Errorf("no support file suffix: %s", suffix)
 }
diff --git a/config/config_loader_options_test.go 
b/config/config_loader_options_test.go
index 0ba55e0..01b517a 100644
--- a/config/config_loader_options_test.go
+++ b/config/config_loader_options_test.go
@@ -25,18 +25,25 @@ import (
        "github.com/stretchr/testify/assert"
 )
 
+import (
+       "dubbo.apache.org/dubbo-go/v3/common/file"
+)
+
 func TestCheckGenre(t *testing.T) {
 
-       err := checkGenre("abc")
+       err := checkFileSuffix("abc")
+       assert.NotNil(t, err)
+
+       err = checkFileSuffix("zc")
        assert.NotNil(t, err)
 
-       err = checkGenre("json")
+       err = checkFileSuffix("json")
        assert.Nil(t, err)
 }
 
 func TestFileGenre(t *testing.T) {
        conf := 
NewLoaderConf(WithPath("../config/testdata/config/properties/application.properties"))
-       assert.Equal(t, conf.genre, "properties")
+       assert.Equal(t, conf.suffix, "properties")
 }
 
 func TestRootConfig(t *testing.T) {
@@ -55,3 +62,12 @@ dubbo.services.HelloService.registry=nacos,zk`
        assert.NotNil(t, conf)
        assert.NotNil(t, conf.bytes)
 }
+
+func TestNewLoaderConf_WithSuffix(t *testing.T) {
+       conf := NewLoaderConf(
+               WithSuffix(file.JSON),
+               
WithPath("../config/testdata/config/properties/application.properties"),
+       )
+
+       assert.Equal(t, conf.suffix, string(file.PROPERTIES))
+}
diff --git a/config/config_resolver.go b/config/config_resolver.go
index d170b36..f94fce2 100644
--- a/config/config_resolver.go
+++ b/config/config_resolver.go
@@ -28,6 +28,7 @@ import (
 )
 
 import (
+       "dubbo.apache.org/dubbo-go/v3/common/file"
        "dubbo.apache.org/dubbo-go/v3/config/parsers/properties"
 )
 
@@ -37,8 +38,8 @@ func GetConfigResolver(conf *loaderConf) *koanf.Koanf {
                k   *koanf.Koanf
                err error
        )
-       if len(conf.genre) <= 0 {
-               conf.genre = "yaml"
+       if len(conf.suffix) <= 0 {
+               conf.suffix = string(file.YAML)
        }
        if len(conf.delim) <= 0 {
                conf.delim = "."
@@ -49,7 +50,7 @@ func GetConfigResolver(conf *loaderConf) *koanf.Koanf {
        }
        k = koanf.New(conf.delim)
 
-       switch conf.genre {
+       switch conf.suffix {
        case "yaml", "yml":
                err = k.Load(rawbytes.Provider(bytes), yaml.Parser())
        case "json":
@@ -59,7 +60,7 @@ func GetConfigResolver(conf *loaderConf) *koanf.Koanf {
        case "properties":
                err = k.Load(rawbytes.Provider(bytes), properties.Parser())
        default:
-               err = errors.Errorf("no support %s file type", conf.genre)
+               err = errors.Errorf("no support %s file suffix", conf.suffix)
        }
 
        if err != nil {

Reply via email to