Skip to content

Commit 2bf74c8

Browse files
committed
make it clearer that the name generation uses UPPER_SNAKE_CASE
1 parent 658dcee commit 2bf74c8

File tree

3 files changed

+29
-30
lines changed

3 files changed

+29
-30
lines changed

README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ go get github.com/qiangxue/go-env
2727

2828
### Loading From Environment Variables
2929

30-
The easiest way of using go-env is to call `env.Load()`, like the following:
30+
The easiest way of using go-env is to call `env.Load()`, like the following:
3131

3232
```go
3333
package main
@@ -66,10 +66,9 @@ a struct field with an environment variable:
6666
- Only public struct fields will be populated
6767
- If the field has an `env` tag, use the tag value as the name, unless the tag value is `-` in which case it means
6868
the field should NOT be populated.
69-
- If the field has no `env` tag, turn the field name into snake format and use that as the name. For example,
70-
a field name `HostName` will be turned into `Host_Name`, and `MyURL` becomes `My_URL`.
71-
- Names are turned into upper case and prefixed with the specified prefix when they are used to look up
72-
in the environment variables.
69+
- If the field has no `env` tag, turn the field name into UPPER_SNAKE_CASE format and use that as the name. For example,
70+
a field name `HostName` will be turned into `HOST_NAME`, and `MyURL` becomes `MY_URL`.
71+
- Names are prefixed with the specified prefix when they are used to look up in the environment variables.
7372

7473
By default, prefix `APP_` will be used. You can customize the prefix by using `env.New()` to create
7574
a customized loader. For example,
@@ -111,24 +110,24 @@ func main() {
111110
```
112111

113112
In the above code, the `Password` field is tagged as `secret`. The log function respects this flag by masking
114-
the field value when logging it in order not to reveal sensitive information.
113+
the field value when logging it in order not to reveal sensitive information.
115114

116115
By setting the prefix to an empty string, you can disable the name prefix completely.
117116

118117

119118
### Data Parsing Rules
120119

121120
Because the values of environment variables are strings, if the corresponding struct fields are of different types,
122-
go-env will convert the string values into appropriate types before assigning them to the struct fields.
121+
go-env will convert the string values into appropriate types before assigning them to the struct fields.
123122

124123
- If a struct contains embedded structs, the fields of the embedded structs will be populated like they are directly
125-
under the containing struct.
124+
under the containing struct.
126125

127126
- If a struct field type implements `env.Setter`, `env.TextMarshaler`, or `env.BinaryMarshaler` interface,
128127
the corresponding interface method will be used to load a string value into the field.
129128

130129
- If a struct field is of a primary type, such as `int`, `string`, `bool`, etc., a string value will be parsed
131-
accordingly and assigned to the field. For example, the string value `TRUE` can be parsed correctly into a
130+
accordingly and assigned to the field. For example, the string value `TRUE` can be parsed correctly into a
132131
boolean `true` value, while `TrUE` will cause a parsing error.
133132

134133
- If a struct field is of a complex type, such as map, slice, struct, the string value will be treated as a JSON

env.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var (
4646
// TagName specifies the tag name for customizing struct field names when loading environment variables
4747
TagName = "env"
4848

49-
// nameRegex is used to convert a string from camelCase into snake format
49+
// nameRegex is used to convert a string from camelCase into snake case format
5050
nameRegex = regexp.MustCompile(`([^A-Z_])([A-Z])`)
5151
// loader is the default loader used by the "Load" function at the package level.
5252
loader = New("APP_", log.Printf)
@@ -81,8 +81,8 @@ func Load(structPtr interface{}) error {
8181
// Load uses the following rules to determine what name should be used to look up the value for a struct field:
8282
// - If the field has an "env" tag, use the tag value as the name, unless the tag is "-" in which case it means
8383
// the field should be skipped.
84-
// - If the field has no "env" tag, turn the field name into snake format and use that as the name.
85-
// - Names are turned into upper case and prefixed with the specified prefix.
84+
// - If the field has no "env" tag, turn the field name into UPPER_SNAKE_CASE format and use that as the name.
85+
// - Names are prefixed with the specified prefix.
8686
//
8787
// The following types of struct fields are supported:
8888
// - types implementing Setter, TextUnmarshaler, BinaryUnmarshaler: the corresponding interface method will be used
@@ -128,7 +128,7 @@ func (l *Loader) Load(structPtr interface{}) error {
128128
continue
129129
}
130130

131-
name = l.prefix + strings.ToUpper(name)
131+
name = l.prefix + name
132132

133133
if value, ok := l.lookup(name); ok {
134134
logValue := value
@@ -168,14 +168,14 @@ func getName(tag string, field string) (string, bool) {
168168
secret := nameLen < len(tag)
169169

170170
if nameLen == 0 {
171-
name = camelCaseToSnake(field)
171+
name = camelCaseToUpperSnakeCase(field)
172172
}
173173
return name, secret
174174
}
175175

176-
// camelCaseToSnake converts a name from camelCase format into snake format.
177-
func camelCaseToSnake(name string) string {
178-
return nameRegex.ReplaceAllString(name, "${1}_$2")
176+
// camelCaseToUpperSnakeCase converts a name from camelCase format into UPPER_SNAKE_CASE format.
177+
func camelCaseToUpperSnakeCase(name string) string {
178+
return strings.ToUpper(nameRegex.ReplaceAllString(name, "${1}_$2"))
179179
}
180180

181181
// setValue assigns a string value to a reflection value using appropriate string parsing and conversion logic.

env_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,24 +126,24 @@ func Test_setValue(t *testing.T) {
126126
}
127127
}
128128

129-
func Test_camelCaseToSnake(t *testing.T) {
129+
func Test_camelCaseToUpperSnakeCase(t *testing.T) {
130130
tests := []struct {
131131
tag string
132132
input string
133133
expected string
134134
}{
135-
{"t1", "test", "test"},
136-
{"t2", "MyName", "My_Name"},
137-
{"t3", "My2Name", "My2_Name"},
138-
{"t4", "MyID", "My_ID"},
139-
{"t5", "My_Name", "My_Name"},
140-
{"t6", "MyFullName", "My_Full_Name"},
141-
{"t7", "URLName", "URLName"},
142-
{"t8", "MyURLName", "My_URLName"},
135+
{"t1", "test", "TEST"},
136+
{"t2", "MyName", "MY_NAME"},
137+
{"t3", "My2Name", "MY2_NAME"},
138+
{"t4", "MyID", "MY_ID"},
139+
{"t5", "My_Name", "MY_NAME"},
140+
{"t6", "MyFullName", "MY_FULL_NAME"},
141+
{"t7", "URLName", "URLNAME"},
142+
{"t8", "MyURLName", "MY_URLNAME"},
143143
}
144144

145145
for _, test := range tests {
146-
output := camelCaseToSnake(test.input)
146+
output := camelCaseToUpperSnakeCase(test.input)
147147
assert.Equal(t, test.expected, output, test.tag)
148148
}
149149
}
@@ -156,11 +156,11 @@ func Test_getName(t *testing.T) {
156156
name string
157157
secret bool
158158
}{
159-
{"t1", "", "Name", "Name", false},
160-
{"t2", "", "MyName", "My_Name", false},
159+
{"t1", "", "Name", "NAME", false},
160+
{"t2", "", "MyName", "MY_NAME", false},
161161
{"t3", "NaME", "Name", "NaME", false},
162162
{"t4", "NaME,secret", "Name", "NaME", true},
163-
{"t5", ",secret", "Name", "Name", true},
163+
{"t5", ",secret", "Name", "NAME", true},
164164
{"t6", "NameWith,Comma", "Name", "NameWith,Comma", false},
165165
{"t7", "NameWith,Comma,secret", "Name", "NameWith,Comma", true},
166166
}

0 commit comments

Comments
 (0)