Skip to content

Commit a291cf9

Browse files
committed
增加环境变量的Key转为小写的存储和查询
如环境变量为:CONFIG_TEST,则可以通过config.test,进行查询
1 parent 9cf2aa9 commit a291cf9

File tree

5 files changed

+65
-14
lines changed

5 files changed

+65
-14
lines changed

archaius.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func Init(opts ...Option) error {
9999
}
100100
}
101101
if o.UseENVSource {
102-
envSource := env.NewEnvConfigurationSource()
102+
envSource := env.NewEnvConfigurationSource(o.EnvKeyLowerCases)
103103
if err = manager.AddSource(envSource); err != nil {
104104
return err
105105
}

archaius_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,31 @@ func TestConfig_RegisterListener(t *testing.T) {
113113
assert.NoError(t, err)
114114
defer archaius.UnRegisterListener(eventHandler, "a*")
115115

116+
type Info struct {
117+
Addr string `yaml:"address"`
118+
Number int `yaml:"number"`
119+
}
120+
}
121+
122+
func TestUnmarshalConfig2(t *testing.T) {
123+
os.Setenv("INFO_ADDRESS", "a")
124+
os.Setenv("INFO_NUMBER", "8")
125+
err := archaius.Init(archaius.WithENVSource(), archaius.WithENVKeyLowerCase())
126+
assert.NoError(t, err)
127+
type Info struct {
128+
Addr string `yaml:"address"`
129+
Number int `yaml:"number"`
130+
}
131+
type InfoWrapper struct {
132+
I *Info `yaml:"info"`
133+
}
134+
a := archaius.GetString("info.address", "")
135+
assert.Equal(t, "a", a)
136+
info := &InfoWrapper{}
137+
err = archaius.UnmarshalConfig(info)
138+
assert.NoError(t, err)
139+
assert.Equal(t, "a", info.I.Addr)
140+
assert.Equal(t, 8, info.I.Number)
116141
}
117142

118143
func TestUnmarshalConfig(t *testing.T) {

options.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ type RemoteInfo struct {
3636

3737
//Options hold options
3838
type Options struct {
39-
RequiredFiles []string
40-
OptionalFiles []string
41-
FileHandler util.FileHandler
42-
RemoteInfo *RemoteInfo
43-
RemoteSource string
44-
UseCLISource bool
45-
UseENVSource bool
46-
UseMemSource bool
39+
RequiredFiles []string
40+
OptionalFiles []string
41+
FileHandler util.FileHandler
42+
RemoteInfo *RemoteInfo
43+
RemoteSource string
44+
UseCLISource bool
45+
UseENVSource bool
46+
UseMemSource bool
47+
EnvKeyLowerCases bool
4748
}
4849

4950
//Option is a func
@@ -95,6 +96,13 @@ func WithENVSource() Option {
9596
}
9697
}
9798

99+
// WithENVkeyLowerCase enable env key ignore case
100+
func WithENVKeyLowerCase() Option {
101+
return func(options *Options) {
102+
options.EnvKeyLowerCases = true
103+
}
104+
}
105+
98106
//WithMemorySource accept the information for initiating a Memory source
99107
func WithMemorySource() Option {
100108
return func(options *Options) {

source/env/env.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,17 @@ const (
3434

3535
//Source is a struct
3636
type Source struct {
37-
Configs sync.Map
38-
priority int
37+
Configs sync.Map
38+
priority int
39+
lowerCaseForKey bool
3940
}
4041

4142
//NewEnvConfigurationSource configures a new environment configuration
42-
func NewEnvConfigurationSource() source.ConfigSource {
43+
func NewEnvConfigurationSource(openLowerCase bool) source.ConfigSource {
4344
openlog.Info("enable env source")
4445
envConfigSource := new(Source)
4546
envConfigSource.priority = envVariableSourcePriority
47+
envConfigSource.lowerCaseForKey = openLowerCase
4648
envConfigSource.pullConfigurations()
4749
return envConfigSource
4850
}
@@ -55,6 +57,10 @@ func (es *Source) pullConfigurations() {
5557
key := string(rs[0:in])
5658
value := string(rs[in+1:])
5759
envKey := strings.Replace(key, "_", ".", -1)
60+
if es.lowerCaseForKey {
61+
key = strings.ToLower(key)
62+
envKey = strings.ToLower(envKey)
63+
}
5864
es.Configs.Store(key, value)
5965
es.Configs.Store(envKey, value)
6066

source/env/env_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,29 @@ func populatEnvConfiguration() {
4848
func TestEnvConfigurationSource(t *testing.T) {
4949

5050
populatEnvConfiguration()
51-
envsource := env.NewEnvConfigurationSource()
51+
envsource := env.NewEnvConfigurationSource(false)
5252
t.Run("set env with underscore, use dot to get ", func(t *testing.T) {
5353
os.Setenv("a_b_c_d", "asd")
54-
envsource := env.NewEnvConfigurationSource()
54+
envsource := env.NewEnvConfigurationSource(false)
5555
v, err := envsource.GetConfigurationByKey("a.b.c.d")
5656
assert.Equal(t, nil, err)
5757
assert.Equal(t, "asd", v)
5858
v, err = envsource.GetConfigurationByKey("a_b_c_d")
5959
assert.Equal(t, nil, err)
6060
assert.Equal(t, "asd", v)
6161
})
62+
63+
t.Run("set env with underscore, and open lower case, use dot to get", func(t *testing.T) {
64+
os.Setenv("A_B_C_D", "ABCD")
65+
envsource := env.NewEnvConfigurationSource(true)
66+
v, err := envsource.GetConfigurationByKey("a.b.c.d")
67+
assert.Equal(t, nil, err)
68+
assert.Equal(t, "ABCD", v)
69+
v, err = envsource.GetConfigurationByKey("a_b_c_d")
70+
assert.Equal(t, nil, err)
71+
assert.Equal(t, "ABCD", v)
72+
73+
})
6274
t.Log("Test envconfigurationsource.go")
6375

6476
t.Log("verifying envsource configurations by Configs method")

0 commit comments

Comments
 (0)