Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Configuration
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container registry
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Libraries
Go
Application
Configuration
Commits
06ee56ee
Verified
Commit
06ee56ee
authored
2 years ago
by
Volker Schukai
Browse files
Options
Downloads
Patches
Plain Diff
fix the prefix of the env function now works
parent
44601b62
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
env.go
+7
-0
7 additions, 0 deletions
env.go
env_test.go
+26
-2
26 additions, 2 deletions
env_test.go
with
33 additions
and
2 deletions
env.go
+
7
−
0
View file @
06ee56ee
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
env_test.go
+
26
−
2
View file @
06ee56ee
...
...
@@ -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
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment