Skip to content

update README.md #12

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

Merged
merged 3 commits into from
Apr 23, 2025
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
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
<div>
<img src="https://raw.githubusercontent.com/go-spring/go-spring/master/[email protected]" width="140" height="*" alt="logo"/>
<br/>
<img src="https://img.shields.io/github/license/go-spring/spring-core" alt="license"/>
<img src="https://img.shields.io/github/go-mod/go-version/go-spring/spring-core" alt="go-version"/>
<img src="https://img.shields.io/github/v/release/go-spring/spring-core?include_prereleases" alt="release"/>
<img src="https://codecov.io/gh/go-spring/spring-core/branch/main/graph/badge.svg" alt="test-coverage"/>
</div>

# 介绍

Expand Down
20 changes: 5 additions & 15 deletions conf/storage/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down
26 changes: 3 additions & 23 deletions gs/internal/gs_conf/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
)

const (
EnvironmentPrefix = "GS_ENVS_PREFIX"
IncludeEnvPatterns = "INCLUDE_ENV_PATTERNS"
ExcludeEnvPatterns = "EXCLUDE_ENV_PATTERNS"
)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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], ""
Expand All @@ -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 {
Expand All @@ -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])
}
33 changes: 0 additions & 33 deletions gs/internal/gs_conf/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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")
Expand Down
6 changes: 0 additions & 6 deletions gs/prop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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))
}
Loading