Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • master
  • v1.0.0
  • v1.0.1
  • v1.1.0
  • v1.10.0
  • v1.11.0
  • v1.11.1
  • v1.11.2
  • v1.11.3
  • v1.11.4
  • v1.12.0
  • v1.13.0
  • v1.14.0
  • v1.15.0
  • v1.16.0
  • v1.17.1
  • v1.17.2
  • v1.17.3
  • v1.17.4
  • v1.17.5
  • v1.17.6
  • v1.17.7
  • v1.18.0
  • v1.18.1
  • v1.18.2
  • v1.18.3
  • v1.19.0
  • v1.2.0
  • v1.20.0
  • v1.20.1
  • v1.20.2
  • v1.20.3
  • v1.20.4
  • v1.20.5
  • v1.21.0
  • v1.22.0
  • v1.22.1
  • v1.22.3
  • v1.22.4
  • v1.22.5
  • v1.22.6
  • v1.22.7
  • v1.22.8
  • v1.22.9
  • v1.3.0
  • v1.4.0
  • v1.4.1
  • v1.4.2
  • v1.4.3
  • v1.5.0
  • v1.6.0
  • v1.7.0
  • v1.7.1
  • v1.8.0
  • v1.9.0
55 results

Target

Select target project
  • oss/libraries/go/application/configuration
1 result
Select Git revision
  • master
  • v1.0.0
  • v1.0.1
  • v1.1.0
  • v1.10.0
  • v1.11.0
  • v1.11.1
  • v1.11.2
  • v1.11.3
  • v1.11.4
  • v1.12.0
  • v1.13.0
  • v1.14.0
  • v1.15.0
  • v1.16.0
  • v1.17.1
  • v1.17.2
  • v1.17.3
  • v1.17.4
  • v1.17.5
  • v1.17.6
  • v1.17.7
  • v1.18.0
  • v1.18.1
  • v1.18.2
  • v1.18.3
  • v1.19.0
  • v1.2.0
  • v1.20.0
  • v1.20.1
  • v1.20.2
  • v1.20.3
  • v1.20.4
  • v1.20.5
  • v1.21.0
  • v1.22.0
  • v1.22.1
  • v1.22.3
  • v1.22.4
  • v1.22.5
  • v1.22.6
  • v1.22.7
  • v1.22.8
  • v1.22.9
  • v1.3.0
  • v1.4.0
  • v1.4.1
  • v1.4.2
  • v1.4.3
  • v1.5.0
  • v1.6.0
  • v1.7.0
  • v1.7.1
  • v1.8.0
  • v1.9.0
55 results
Show changes
Commits on Source (3)
<a name="v1.11.4"></a>
## [v1.11.4] - 2022-10-23
### Bug Fixes
- fix the prefix of the env function now works
<a name="v1.11.3"></a>
## [v1.11.3] - 2022-10-23
### Bug Fixes
......@@ -131,6 +137,7 @@
<a name="v1.0.0"></a>
## v1.0.0 - 2022-09-18
[v1.11.4]: https://gitlab.schukai.com/oss/libraries/go/application/configuration/compare/v1.11.3...v1.11.4
[v1.11.3]: https://gitlab.schukai.com/oss/libraries/go/application/configuration/compare/v1.11.2...v1.11.3
[v1.11.2]: https://gitlab.schukai.com/oss/libraries/go/application/configuration/compare/v1.11.1...v1.11.2
[v1.11.1]: https://gitlab.schukai.com/oss/libraries/go/application/configuration/compare/v1.11.0...v1.11.1
......
......@@ -7,6 +7,7 @@ import (
"os"
"reflect"
"strconv"
"strings"
)
func (s *Settings[C]) InitFromEnv(prefix string) *Settings[C] {
......@@ -21,12 +22,18 @@ func (s *Settings[C]) InitFromEnv(prefix string) *Settings[C] {
}
}()
if prefix != "" {
prefix = strings.ToUpper(prefix) + "_"
}
err := runOnTags(&s.config, []string{envTagKey}, func(k string, field reflect.Value) {
if !field.CanSet() {
return
}
k = prefix + strings.ToUpper(k)
v, found := os.LookupEnv(k)
if !found {
return // env not found
......
......@@ -20,6 +20,7 @@ func TestReadmeExample2(t *testing.T) {
// Set value
os.Setenv("HOST", "www.example.com")
defer os.Unsetenv("HOST")
s := New(config)
//fmt.Println(s.Config().Host) // localhost
......@@ -31,6 +32,27 @@ func TestReadmeExample2(t *testing.T) {
}
func TestWithPrefix(t *testing.T) {
config := struct {
Host string `env:"HOST"`
}{
Host: "localhost",
}
// Set value
os.Setenv("XYZ_HOST", "www.example.com")
defer os.Unsetenv("XYZ_HOST")
s := New(config)
//fmt.Println(s.Config().Host) // localhost
assert.Equal(t, s.Config().Host, "localhost")
s.InitFromEnv("XYZ") // no prefix
//fmt.Println(s.Config().Host) // www.example.com
assert.Equal(t, s.Config().Host, "www.example.com")
}
func TestFromEnvNotSet(t *testing.T) {
config := ConfigStruct6{
IA: 1234,
......@@ -43,6 +65,7 @@ func TestFromEnvNotSet(t *testing.T) {
// Value not set
s.InitFromEnv("VALUE_IB")
defer os.Unsetenv("VALUE_IB")
if s.Config().IA != 1234 {
t.Errorf("Expected 1234, got %d", s.Config().IA)
......@@ -61,7 +84,7 @@ func TestFromEnv(t *testing.T) {
s := New(config)
// Value not set
s.InitFromEnv("VALUE_IB")
s.InitFromEnv("NIX")
if s.Config().IA != 1234 {
t.Errorf("Expected 1234, got %d", s.Config().IA)
......@@ -69,7 +92,8 @@ func TestFromEnv(t *testing.T) {
// Set value
os.Setenv("VALUE_IB", "i-am-b")
s.InitFromEnv("VALUE_IB")
defer os.Unsetenv("VALUE_IB")
s.InitFromEnv("")
if s.Config().IB != "i-am-b" {
t.Errorf("Expected i-am-b, got %s", s.Config().IB)
......
{"version":"1.11.3"}
{"version":"1.11.4"}