Skip to content

feat: support enum field validation #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
13 changes: 10 additions & 3 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ type Setter interface {
// If a field is unexported or required configuration is not
// found, an error will be returned.
func Set(i interface{}) (err error) {
return SetPrefix(i, "")
}

// SetPrefix sets the fields of a struct from environment config
// with a given prefix. If a field is unexported or required
// configuration is not found, an error will be returned.
func SetPrefix(i interface{}, prefix string) (err error) {
v := reflect.ValueOf(i)

// Don't try to process a non-pointer value.
Expand All @@ -35,7 +42,7 @@ func Set(i interface{}) (err error) {
t := reflect.TypeOf(i).Elem()

for i := 0; i < t.NumField(); i++ {
if err = processField(t.Field(i), v.Field(i)); err != nil {
if err = processField(prefix, t.Field(i), v.Field(i)); err != nil {
return
}
}
Expand All @@ -47,7 +54,7 @@ func Set(i interface{}) (err error) {
// and attempt to set it. If not found, another check for the
// "required" tag will be performed to decided whether an error
// needs to be returned.
func processField(t reflect.StructField, v reflect.Value) (err error) {
func processField(prefix string, t reflect.StructField, v reflect.Value) (err error) {
envTag, ok := t.Tag.Lookup("env")
if !ok {
return
Expand All @@ -61,7 +68,7 @@ func processField(t reflect.StructField, v reflect.Value) (err error) {

// Lookup the environment variable and if found, set and
// return
env, ok := os.LookupEnv(envTag)
env, ok := os.LookupEnv(prefix + envTag)
if ok {
return setField(t, v, env)
}
Expand Down
13 changes: 12 additions & 1 deletion env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ func TestInvalidConfigurationForDuration(t *testing.T) {

err := Set(&config)
ErrorNotNil(t, err)
Equals(t, `error setting "Prop": time: unknown unit hh in duration 1hh`, err.Error())
Equals(t, `error setting "Prop": time: unknown unit "hh" in duration "1hh"`, err.Error())
}

func TestEnvNonPointer(t *testing.T) {
Expand Down Expand Up @@ -664,6 +664,17 @@ func TestEnvCustomTypeStructWithError(t *testing.T) {
Assert(t, strings.Contains(err.Error(), errConfigDurationError.Error()))
}

func TestEnvPrefixed(t *testing.T) {
os.Setenv("PROP_PROP", "hello")

config := struct {
Prop string `env:"PROP"`
}{}

ErrorNil(t, SetPrefix(&config, "PROP_"))
Equals(t, "hello", config.Prop)
}

type configDuration struct {
Duration time.Duration
}
Expand Down