diff --git a/README.md b/README.md index b14f7606..83dfb0c2 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,11 @@ -# spring-core - -[![codecov](https://codecov.io/gh/go-spring/spring-core/branch/main/graph/badge.svg)](https://codecov.io/gh/go-spring/spring-core) +
+ logo +
+ license + go-version + release + test-coverage +
# 介绍 diff --git a/conf/storage/path.go b/conf/storage/path.go index 6ba846c3..3450939b 100644 --- a/conf/storage/path.go +++ b/conf/storage/path.go @@ -79,10 +79,7 @@ func SplitPath(key string) (_ []Path, err error) { return nil, fmt.Errorf("invalid key '%s'", key) } if lastChar != ']' { - path, err = appendKey(path, key[lastPos:i]) - if err != nil { - return nil, fmt.Errorf("invalid key '%s'", key) - } + path = appendKey(path, key[lastPos:i]) } lastPos = i + 1 lastChar = c @@ -91,10 +88,7 @@ func SplitPath(key string) (_ []Path, err error) { return nil, fmt.Errorf("invalid key '%s'", key) } if i > 0 && lastChar != ']' { - path, err = appendKey(path, key[lastPos:i]) - if err != nil { - return nil, fmt.Errorf("invalid key '%s'", key) - } + path = appendKey(path, key[lastPos:i]) } openBracket = true lastPos = i + 1 @@ -121,18 +115,14 @@ func SplitPath(key string) (_ []Path, err error) { return nil, fmt.Errorf("invalid key '%s'", key) } if lastChar != ']' { - path, err = appendKey(path, key[lastPos:]) - if err != nil { - return nil, fmt.Errorf("invalid key '%s'", key) - } + path = appendKey(path, key[lastPos:]) } return path, nil } // appendKey appends a key segment to the path. -func appendKey(path []Path, s string) ([]Path, error) { - path = append(path, Path{PathTypeKey, s}) - return path, nil +func appendKey(path []Path, s string) []Path { + return append(path, Path{PathTypeKey, s}) } // appendIndex appends an index segment to the path. diff --git a/gs/internal/gs_conf/env.go b/gs/internal/gs_conf/env.go index 89a02c6b..12002555 100644 --- a/gs/internal/gs_conf/env.go +++ b/gs/internal/gs_conf/env.go @@ -25,7 +25,6 @@ import ( ) const ( - EnvironmentPrefix = "GS_ENVS_PREFIX" IncludeEnvPatterns = "INCLUDE_ENV_PATTERNS" ExcludeEnvPatterns = "EXCLUDE_ENV_PATTERNS" ) @@ -58,11 +57,6 @@ func (c *Environment) CopyTo(p *conf.MutableProperties) error { return nil } - prefix := "GS_" - if s := strings.TrimSpace(os.Getenv(EnvironmentPrefix)); s != "" { - prefix = s - } - toRex := func(patterns []string) ([]*regexp.Regexp, error) { var rex []*regexp.Regexp for _, v := range patterns { @@ -102,6 +96,7 @@ func (c *Environment) CopyTo(p *conf.MutableProperties) error { return false } + const prefix = "GS_" for _, env := range environ { ss := strings.SplitN(env, "=", 2) k, v := ss[0], "" @@ -112,7 +107,8 @@ func (c *Environment) CopyTo(p *conf.MutableProperties) error { var propKey string if strings.HasPrefix(k, prefix) { propKey = strings.TrimPrefix(k, prefix) - propKey = strings.ToLower(replaceKey(propKey)) + propKey = strings.ReplaceAll(propKey, "_", ".") + propKey = strings.ToLower(propKey) } else if matches(includeRex, k) && !matches(excludeRex, k) { propKey = k } else { @@ -125,19 +121,3 @@ func (c *Environment) CopyTo(p *conf.MutableProperties) error { } return nil } - -// replaceKey replace '_' with '.' -func replaceKey(s string) string { - b := make([]byte, len(s)+2) - b[0] = '_' - b[len(b)-1] = '_' - copy(b[1:len(b)-1], s) - for i := 1; i < len(b)-1; i++ { - if b[i] == '_' { - if b[i-1] != '_' && b[i+1] != '_' { - b[i] = '.' - } - } - } - return string(b[1 : len(b)-1]) -} diff --git a/gs/internal/gs_conf/env_test.go b/gs/internal/gs_conf/env_test.go index 1eca376a..f00f68fb 100644 --- a/gs/internal/gs_conf/env_test.go +++ b/gs/internal/gs_conf/env_test.go @@ -24,23 +24,6 @@ import ( "github.com/go-spring/spring-core/util/assert" ) -func TestReplaceKey(t *testing.T) { - tests := []struct { - input string - want string - }{ - {"MY_ENV_VAR", "MY.ENV.VAR"}, - {"_MY_ENV_", "_MY.ENV_"}, - {"__PREFIX__KEY__", "__PREFIX__KEY__"}, - {"NO_UNDERSCORES", "NO.UNDERSCORES"}, - {"_LEADING_AND_TRAILING_", "_LEADING.AND.TRAILING_"}, - } - for _, tt := range tests { - got := replaceKey(tt.input) - assert.Equal(t, got, tt.want) - } -} - func TestEnvironment(t *testing.T) { os.Clearenv() @@ -65,22 +48,6 @@ func TestEnvironment(t *testing.T) { assert.Equal(t, props.Get("API_KEY"), "key123") }) - t.Run("custom prefix", func(t *testing.T) { - _ = os.Setenv(EnvironmentPrefix, "APP_") - _ = os.Setenv("GS_CACHE_SIZE", "100") - _ = os.Setenv("APP_DB_HOST", "db1") - _ = os.Setenv("API_KEY", "key123") - defer func() { - _ = os.Unsetenv("GS_DB_HOST") - _ = os.Unsetenv("API_KEY") - }() - props := conf.New() - err := NewEnvironment().CopyTo(props) - assert.Nil(t, err) - assert.Equal(t, props.Get("db.host"), "db1") - assert.Equal(t, props.Get("API_KEY"), "key123") - }) - t.Run("custom patterns", func(t *testing.T) { _ = os.Setenv(IncludeEnvPatterns, "^TEST_") _ = os.Setenv(ExcludeEnvPatterns, "^TEST_INTERNAL") diff --git a/gs/prop.go b/gs/prop.go index f337e64a..6803c9b9 100644 --- a/gs/prop.go +++ b/gs/prop.go @@ -31,7 +31,6 @@ const ( EnableServersProp = "spring.app.enable-servers" EnableSimpleHttpServerProp = "spring.enable.simple-http-server" EnableSimplePProfServerProp = "spring.enable.simple-pprof-server" - EnableDefaultServeMuxProp = "spring.enable.default-serve-mux" ) func setProperty(key string, val string) { @@ -74,8 +73,3 @@ func EnableSimpleHttpServer(enable bool) { func EnableSimplePProfServer(enable bool) { setProperty(EnableSimplePProfServerProp, strconv.FormatBool(enable)) } - -// EnableDefaultServeMux enables or disables the default serve mux. -func EnableDefaultServeMux(enable bool) { - setProperty(EnableDefaultServeMuxProp, strconv.FormatBool(enable)) -}