fix(env): index out panic when env key is preifix (#1247)

pull/1195/head
Kagaya 3 years ago committed by GitHub
parent d09ba5e3ee
commit 3660a8d65d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      config/env/env.go
  2. 3
      config/env/env_test.go

12
config/env/env.go vendored

@ -31,7 +31,7 @@ func (e *env) load(envStrings []string) []*config.KeyValue {
if len(e.prefixs) > 0 {
p, ok := matchPrefix(e.prefixs, envstr)
if !ok {
if !ok || len(p) == len(k) {
continue
}
// trim prefix
@ -41,10 +41,12 @@ func (e *env) load(envStrings []string) []*config.KeyValue {
}
}
kv = append(kv, &config.KeyValue{
Key: k,
Value: []byte(v),
})
if len(k) != 0 {
kv = append(kv, &config.KeyValue{
Key: k,
Value: []byte(v),
})
}
}
return kv
}

@ -49,6 +49,9 @@ func TestEnvWithPrefix(t *testing.T) {
prefix1 + "SERVICE_NAME": "kratos_app",
prefix2 + "ADDR": "192.168.0.1",
prefix1 + "AGE": "20",
// only prefix
prefix2: "foo",
prefix2 + "_": "foo_",
}
for k, v := range envs {

Loading…
Cancel
Save