Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ setting applied it will look for `MYAPP_AUTO_SPLIT_VAR`. Note that numbers
will get globbed into the previous word. If the setting does not do the
right thing, you may use a manual override.

Nested structs are joined with `_` by default (`MYAPP_DATABASE_USER_NAME`).
`ProcessWith` can use a different nested separator without changing
`split_words` field splitting:

```Go
err := envconfig.ProcessWith("myapp", &s, envconfig.Options{NestedSeparator: "__"})
// reads MYAPP__DATABASE__USER_NAME
```

Envconfig will process value for `ManualOverride1` by populating it with the
value for `MYAPP_MANUAL_OVERRIDE_1`. Without this struct tag, it would have
instead looked up `MYAPP_MANUALOVERRIDE1`. With the `split_words:"true"` tag
Expand Down
31 changes: 28 additions & 3 deletions envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,28 @@ type varInfo struct {
Tags reflect.StructTag
}

// Options configures ProcessWith. The zero value matches Process.
type Options struct {
// NestedSeparator is inserted between the prefix, nested struct names, and
// field names when building environment variable keys. Empty means "_".
// Set to "__" to match tools that use a double underscore as hierarchy.
NestedSeparator string
}

func nestedSep(sep string) string {
if sep == "" {
return "_"
}
return sep
}

// GatherInfo gathers information about the specified struct
func gatherInfo(prefix string, spec interface{}) ([]varInfo, error) {
return gatherInfoSep(prefix, spec, "_")
}

func gatherInfoSep(prefix string, spec interface{}, sep string) ([]varInfo, error) {
sep = nestedSep(sep)
s := reflect.ValueOf(spec)

if s.Kind() != reflect.Ptr {
Expand Down Expand Up @@ -122,7 +142,7 @@ func gatherInfo(prefix string, spec interface{}) ([]varInfo, error) {
info.Key = info.Alt
}
if prefix != "" {
info.Key = fmt.Sprintf("%s_%s", prefix, info.Key)
info.Key = prefix + sep + info.Key
}
info.Key = strings.ToUpper(info.Key)
infos = append(infos, info)
Expand All @@ -136,7 +156,7 @@ func gatherInfo(prefix string, spec interface{}) ([]varInfo, error) {
}

embeddedPtr := f.Addr().Interface()
embeddedInfos, err := gatherInfo(innerPrefix, embeddedPtr)
embeddedInfos, err := gatherInfoSep(innerPrefix, embeddedPtr, sep)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -182,7 +202,12 @@ func CheckDisallowed(prefix string, spec interface{}) error {

// Process populates the specified struct based on environment variables
func Process(prefix string, spec interface{}) error {
infos, err := gatherInfo(prefix, spec)
return ProcessWith(prefix, spec, Options{})
}

// ProcessWith is Process with Options. NestedSeparator defaults to "_".
func ProcessWith(prefix string, spec interface{}, opt Options) error {
infos, err := gatherInfoSep(prefix, spec, opt.NestedSeparator)

for _, info := range infos {

Expand Down
29 changes: 29 additions & 0 deletions envconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,35 @@ func TestNestedStructVarName(t *testing.T) {
}
}

func TestProcessWithNestedSeparator(t *testing.T) {
type Database struct {
UserName string `split_words:"true"`
}
type Spec struct {
Database Database
}
os.Clearenv()
os.Setenv("MYAPP__DATABASE__USER_NAME", "alice")
var s Spec
if err := ProcessWith("myapp", &s, Options{NestedSeparator: "__"}); err != nil {
t.Fatal(err)
}
if s.Database.UserName != "alice" {
t.Errorf("expected alice, got %q", s.Database.UserName)
}

// Default Process still uses a single underscore.
os.Clearenv()
os.Setenv("MYAPP_DATABASE_USER_NAME", "bob")
var s2 Spec
if err := Process("myapp", &s2); err != nil {
t.Fatal(err)
}
if s2.Database.UserName != "bob" {
t.Errorf("expected bob, got %q", s2.Database.UserName)
}
}

func TestTextUnmarshalerError(t *testing.T) {
var s Specification
os.Clearenv()
Expand Down