Skip to content

Commit 4469bf8

Browse files
authored
Merge pull request #12 from lvan100/main
update README.md
2 parents ab99a00 + 2ae1b66 commit 4469bf8

File tree

5 files changed

+16
-80
lines changed

5 files changed

+16
-80
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
# spring-core
2-
3-
[![codecov](https://codecov.io/gh/go-spring/spring-core/branch/main/graph/badge.svg)](https://codecov.io/gh/go-spring/spring-core)
1+
<div>
2+
<img src="https://raw.githubusercontent.com/go-spring/go-spring/master/[email protected]" width="140" height="*" alt="logo"/>
3+
<br/>
4+
<img src="https://img.shields.io/github/license/go-spring/spring-core" alt="license"/>
5+
<img src="https://img.shields.io/github/go-mod/go-version/go-spring/spring-core" alt="go-version"/>
6+
<img src="https://img.shields.io/github/v/release/go-spring/spring-core?include_prereleases" alt="release"/>
7+
<img src="https://codecov.io/gh/go-spring/spring-core/branch/main/graph/badge.svg" alt="test-coverage"/>
8+
</div>
49

510
# 介绍
611

conf/storage/path.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,7 @@ func SplitPath(key string) (_ []Path, err error) {
7979
return nil, fmt.Errorf("invalid key '%s'", key)
8080
}
8181
if lastChar != ']' {
82-
path, err = appendKey(path, key[lastPos:i])
83-
if err != nil {
84-
return nil, fmt.Errorf("invalid key '%s'", key)
85-
}
82+
path = appendKey(path, key[lastPos:i])
8683
}
8784
lastPos = i + 1
8885
lastChar = c
@@ -91,10 +88,7 @@ func SplitPath(key string) (_ []Path, err error) {
9188
return nil, fmt.Errorf("invalid key '%s'", key)
9289
}
9390
if i > 0 && lastChar != ']' {
94-
path, err = appendKey(path, key[lastPos:i])
95-
if err != nil {
96-
return nil, fmt.Errorf("invalid key '%s'", key)
97-
}
91+
path = appendKey(path, key[lastPos:i])
9892
}
9993
openBracket = true
10094
lastPos = i + 1
@@ -121,18 +115,14 @@ func SplitPath(key string) (_ []Path, err error) {
121115
return nil, fmt.Errorf("invalid key '%s'", key)
122116
}
123117
if lastChar != ']' {
124-
path, err = appendKey(path, key[lastPos:])
125-
if err != nil {
126-
return nil, fmt.Errorf("invalid key '%s'", key)
127-
}
118+
path = appendKey(path, key[lastPos:])
128119
}
129120
return path, nil
130121
}
131122

132123
// appendKey appends a key segment to the path.
133-
func appendKey(path []Path, s string) ([]Path, error) {
134-
path = append(path, Path{PathTypeKey, s})
135-
return path, nil
124+
func appendKey(path []Path, s string) []Path {
125+
return append(path, Path{PathTypeKey, s})
136126
}
137127

138128
// appendIndex appends an index segment to the path.

gs/internal/gs_conf/env.go

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
)
2626

2727
const (
28-
EnvironmentPrefix = "GS_ENVS_PREFIX"
2928
IncludeEnvPatterns = "INCLUDE_ENV_PATTERNS"
3029
ExcludeEnvPatterns = "EXCLUDE_ENV_PATTERNS"
3130
)
@@ -58,11 +57,6 @@ func (c *Environment) CopyTo(p *conf.MutableProperties) error {
5857
return nil
5958
}
6059

61-
prefix := "GS_"
62-
if s := strings.TrimSpace(os.Getenv(EnvironmentPrefix)); s != "" {
63-
prefix = s
64-
}
65-
6660
toRex := func(patterns []string) ([]*regexp.Regexp, error) {
6761
var rex []*regexp.Regexp
6862
for _, v := range patterns {
@@ -102,6 +96,7 @@ func (c *Environment) CopyTo(p *conf.MutableProperties) error {
10296
return false
10397
}
10498

99+
const prefix = "GS_"
105100
for _, env := range environ {
106101
ss := strings.SplitN(env, "=", 2)
107102
k, v := ss[0], ""
@@ -112,7 +107,8 @@ func (c *Environment) CopyTo(p *conf.MutableProperties) error {
112107
var propKey string
113108
if strings.HasPrefix(k, prefix) {
114109
propKey = strings.TrimPrefix(k, prefix)
115-
propKey = strings.ToLower(replaceKey(propKey))
110+
propKey = strings.ReplaceAll(propKey, "_", ".")
111+
propKey = strings.ToLower(propKey)
116112
} else if matches(includeRex, k) && !matches(excludeRex, k) {
117113
propKey = k
118114
} else {
@@ -125,19 +121,3 @@ func (c *Environment) CopyTo(p *conf.MutableProperties) error {
125121
}
126122
return nil
127123
}
128-
129-
// replaceKey replace '_' with '.'
130-
func replaceKey(s string) string {
131-
b := make([]byte, len(s)+2)
132-
b[0] = '_'
133-
b[len(b)-1] = '_'
134-
copy(b[1:len(b)-1], s)
135-
for i := 1; i < len(b)-1; i++ {
136-
if b[i] == '_' {
137-
if b[i-1] != '_' && b[i+1] != '_' {
138-
b[i] = '.'
139-
}
140-
}
141-
}
142-
return string(b[1 : len(b)-1])
143-
}

gs/internal/gs_conf/env_test.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,6 @@ import (
2424
"github.com/go-spring/spring-core/util/assert"
2525
)
2626

27-
func TestReplaceKey(t *testing.T) {
28-
tests := []struct {
29-
input string
30-
want string
31-
}{
32-
{"MY_ENV_VAR", "MY.ENV.VAR"},
33-
{"_MY_ENV_", "_MY.ENV_"},
34-
{"__PREFIX__KEY__", "__PREFIX__KEY__"},
35-
{"NO_UNDERSCORES", "NO.UNDERSCORES"},
36-
{"_LEADING_AND_TRAILING_", "_LEADING.AND.TRAILING_"},
37-
}
38-
for _, tt := range tests {
39-
got := replaceKey(tt.input)
40-
assert.Equal(t, got, tt.want)
41-
}
42-
}
43-
4427
func TestEnvironment(t *testing.T) {
4528
os.Clearenv()
4629

@@ -65,22 +48,6 @@ func TestEnvironment(t *testing.T) {
6548
assert.Equal(t, props.Get("API_KEY"), "key123")
6649
})
6750

68-
t.Run("custom prefix", func(t *testing.T) {
69-
_ = os.Setenv(EnvironmentPrefix, "APP_")
70-
_ = os.Setenv("GS_CACHE_SIZE", "100")
71-
_ = os.Setenv("APP_DB_HOST", "db1")
72-
_ = os.Setenv("API_KEY", "key123")
73-
defer func() {
74-
_ = os.Unsetenv("GS_DB_HOST")
75-
_ = os.Unsetenv("API_KEY")
76-
}()
77-
props := conf.New()
78-
err := NewEnvironment().CopyTo(props)
79-
assert.Nil(t, err)
80-
assert.Equal(t, props.Get("db.host"), "db1")
81-
assert.Equal(t, props.Get("API_KEY"), "key123")
82-
})
83-
8451
t.Run("custom patterns", func(t *testing.T) {
8552
_ = os.Setenv(IncludeEnvPatterns, "^TEST_")
8653
_ = os.Setenv(ExcludeEnvPatterns, "^TEST_INTERNAL")

gs/prop.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ const (
3131
EnableServersProp = "spring.app.enable-servers"
3232
EnableSimpleHttpServerProp = "spring.enable.simple-http-server"
3333
EnableSimplePProfServerProp = "spring.enable.simple-pprof-server"
34-
EnableDefaultServeMuxProp = "spring.enable.default-serve-mux"
3534
)
3635

3736
func setProperty(key string, val string) {
@@ -74,8 +73,3 @@ func EnableSimpleHttpServer(enable bool) {
7473
func EnableSimplePProfServer(enable bool) {
7574
setProperty(EnableSimplePProfServerProp, strconv.FormatBool(enable))
7675
}
77-
78-
// EnableDefaultServeMux enables or disables the default serve mux.
79-
func EnableDefaultServeMux(enable bool) {
80-
setProperty(EnableDefaultServeMuxProp, strconv.FormatBool(enable))
81-
}

0 commit comments

Comments
 (0)