diff --git a/README.md b/README.md index 3dbbe7f6..e7e965c2 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/envconfig.go b/envconfig.go index 3a2aac7a..94a781b9 100644 --- a/envconfig.go +++ b/envconfig.go @@ -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 { @@ -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) @@ -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 } @@ -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 { diff --git a/envconfig_test.go b/envconfig_test.go index c184fc3b..99c02b96 100644 --- a/envconfig_test.go +++ b/envconfig_test.go @@ -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()