Skip to content
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

Add variable key to error message and allow for custom error #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Environment struct {
Duration time.Duration `env:"TYPE_DURATION"`
DefaultValue string `env:"MISSING_VAR,default=default_value"`
RequiredValue string `env:"IM_REQUIRED,required=true"`
CustomError string `env:"CUSTOM_ERROR,required=true,error=custom_message"`
}

func main() {
Expand Down
36 changes: 28 additions & 8 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,22 @@ var (

// ErrUnexportedField returned when a field with tag "env" is not exported.
ErrUnexportedField = errors.New("field must be exported")

// ErrMissingRequiredValue returned when a field with required=true contains no value or default
ErrMissingRequiredValue = errors.New("value for this field is required")
)

// ErrMissingRequiredValue returned when a field with required=true contains no value or default
type ErrMissingRequiredValue struct {
VariableName string
}

func (e ErrMissingRequiredValue) Error() string {
return fmt.Sprintf("value for '%s' is required", e.VariableName)
}

func IsErrMissingRequiredValue(err error) bool {
_, ok := err.(ErrMissingRequiredValue)
return ok
}

// Unmarshal parses an EnvSet and stores the result in the value pointed to by
// v. Fields that are matched in v will be deleted from EnvSet, resulting in
// an EnvSet with the remaining environment variables. If v is nil or not a
Expand Down Expand Up @@ -93,20 +104,26 @@ func Unmarshal(es EnvSet, v interface{}) error {

var (
envValue string
ok bool
ok bool
lastKey string
)
for _, envKey := range envTag.Keys {
envValue, ok = es[envKey]
if ok {
break
}
lastKey = envKey
}

if !ok {
if envTag.Default != "" {
envValue = envTag.Default
} else if envTag.Required {
return ErrMissingRequiredValue
if envTag.ErrorName != "" {
return ErrMissingRequiredValue{envTag.ErrorName}
} else {
return ErrMissingRequiredValue{lastKey}
}
} else {
continue
}
Expand Down Expand Up @@ -248,9 +265,10 @@ func Marshal(v interface{}) (EnvSet, error) {
}

type tag struct {
Keys []string
Default string
Required bool
Keys []string
Default string
Required bool
ErrorName string
}

func parseTag(tagString string) tag {
Expand All @@ -264,6 +282,8 @@ func parseTag(tagString string) tag {
t.Default = keyData[1]
case "required":
t.Required = strings.ToLower(keyData[1]) == "true"
case "error":
t.ErrorName = keyData[1]
default:
// just ignoring unsupported keys
continue
Expand Down
26 changes: 24 additions & 2 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ type RequiredValueStruct struct {
InvalidExtra string `env:"INVALID,invalid=invalid"`
}

type ErrorNameStruct struct {
Required string `env:"REQUIRED_VAL,required=true"`
RequiredWithName string `env:"REQUIRED_MISSING,required=true,error=NAME"`
NotRequired string `env:"NOT_REQUIRED,required=false,error=NAME"`
}

func TestUnmarshal(t *testing.T) {
environ := map[string]string{
"HOME": "/home/test",
Expand Down Expand Up @@ -251,7 +257,7 @@ func TestUnmarshalUnexported(t *testing.T) {
}

func TestUnmarshalDefaultValues(t *testing.T) {
environ := map[string]string {
environ := map[string]string{
"PRESENT": "youFoundMe",
}
var defaultValueStruct DefaultValueStruct
Expand Down Expand Up @@ -279,7 +285,7 @@ func TestUnmarshalRequiredValues(t *testing.T) {
environ := map[string]string{}
var requiredValuesStruct RequiredValueStruct
err := Unmarshal(environ, &requiredValuesStruct)
if err != ErrMissingRequiredValue {
if !IsErrMissingRequiredValue(err) {
t.Errorf("Expected error 'ErrMissingRequiredValue' but go '%s'", err)
}
environ["REQUIRED_VAL"] = "required"
Expand All @@ -295,6 +301,22 @@ func TestUnmarshalRequiredValues(t *testing.T) {
}
}

func TestUnmarshalRequiredErrorName(t *testing.T) {
var err error
environ := map[string]string{}
var errorNameStruct ErrorNameStruct
err = Unmarshal(environ, &errorNameStruct)
if err.Error() != "value for 'REQUIRED_VAL' is required" {
t.Errorf("Expected response \"value for 'REQUIRED_VAL' is required\" but go '%s'", err)
}

environ["REQUIRED_VAL"] = "required"
err = Unmarshal(environ, &errorNameStruct)
if err.Error() != "value for 'NAME' is required" {
t.Errorf("Expected response \"value for 'NAME' is required\" but go '%s'", err)
}
}

func TestMarshal(t *testing.T) {
validStruct := ValidStruct{
Home: "/home/test",
Expand Down