1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-10-30 23:47:59 +02:00

perf(config/env): use strings.Cut to optimize env load method (#3645)

* perf(config/env): use strings.Cut and pre-allocate slice to optimize env load

* refactor(config/env): load returns nil when no kv found

---------

Co-authored-by: 1911860538 <alxps1911@gmail.com>
Co-authored-by: Tony.Chen <zhihui_chen@foxmail.com>
This commit is contained in:
Name
2025-04-25 13:47:16 +08:00
committed by GitHub
parent db4961c254
commit ff2d73a882

27
config/env/env.go vendored
View File

@@ -15,38 +15,33 @@ func NewSource(prefixes ...string) config.Source {
return &env{prefixes: prefixes}
}
func (e *env) Load() (kv []*config.KeyValue, err error) {
func (e *env) Load() (kvs []*config.KeyValue, err error) {
return e.load(os.Environ()), nil
}
func (e *env) load(envs []string) []*config.KeyValue {
var kv []*config.KeyValue
var kvs []*config.KeyValue
for _, env := range envs {
var k, v string
subs := strings.SplitN(env, "=", 2) //nolint:mnd
k = subs[0]
if len(subs) > 1 {
v = subs[1]
k, v, _ := strings.Cut(env, "=")
if k == "" {
continue
}
if len(e.prefixes) > 0 {
p, ok := matchPrefix(e.prefixes, k)
if !ok || len(p) == len(k) {
prefix, ok := matchPrefix(e.prefixes, k)
if !ok || k == prefix {
continue
}
// trim prefix
k = strings.TrimPrefix(k, p)
k = strings.TrimPrefix(k, prefix)
k = strings.TrimPrefix(k, "_")
}
if len(k) != 0 {
kv = append(kv, &config.KeyValue{
if k != "" {
kvs = append(kvs, &config.KeyValue{
Key: k,
Value: []byte(v),
})
}
}
return kv
return kvs
}
func (e *env) Watch() (config.Watcher, error) {