Skip to content

Commit 658dcee

Browse files
committed
support environment variable names that contain commas
1 parent d8f14e9 commit 658dcee

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

env.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,16 @@ func indirect(v reflect.Value) reflect.Value {
161161

162162
// getName generates the environment variable name from a struct field tag and the field name.
163163
func getName(tag string, field string) (string, bool) {
164-
secret := false
165-
if idx := strings.Index(tag, ","); idx != -1 {
166-
tag, secret = tag[:idx], tag[idx+1:] == "secret"
167-
}
168-
if tag == "" {
169-
return camelCaseToSnake(field), secret
164+
name := strings.TrimSuffix(tag, ",secret")
165+
nameLen := len(name)
166+
167+
// If the `,secret` suffix was found, it would have been trimmed, so the length should be different.
168+
secret := nameLen < len(tag)
169+
170+
if nameLen == 0 {
171+
name = camelCaseToSnake(field)
170172
}
171-
return tag, secret
173+
return name, secret
172174
}
173175

174176
// camelCaseToSnake converts a name from camelCase format into snake format.

env_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ package env
66

77
import (
88
"fmt"
9-
"github.com/stretchr/testify/assert"
109
"reflect"
1110
"strconv"
1211
"strings"
1312
"testing"
13+
14+
"github.com/stretchr/testify/assert"
1415
)
1516

1617
func Test_indirect(t *testing.T) {
@@ -160,7 +161,8 @@ func Test_getName(t *testing.T) {
160161
{"t3", "NaME", "Name", "NaME", false},
161162
{"t4", "NaME,secret", "Name", "NaME", true},
162163
{"t5", ",secret", "Name", "Name", true},
163-
{"t6", "NaME,", "Name", "NaME", false},
164+
{"t6", "NameWith,Comma", "Name", "NameWith,Comma", false},
165+
{"t7", "NameWith,Comma,secret", "Name", "NameWith,Comma", true},
164166
}
165167

166168
for _, test := range tests {

0 commit comments

Comments
 (0)