Skip to content

Commit

Permalink
refactor required handler to be simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinfalting committed Jan 13, 2024
1 parent 470628a commit da64082
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions confhandler/required.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package confhandler
import (
"context"
"fmt"
"reflect"

"github.com/kevinfalting/structconf/stronf"
)
Expand All @@ -14,19 +13,14 @@ import (
type Required struct{}

func (h Required) Handle(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
if proposedValue != nil && reflect.ValueOf(proposedValue).IsZero() {
return proposedValue, nil
}

if !field.IsZero() {
if proposedValue != nil {
return proposedValue, nil
}

_, required := field.LookupTag("conf", "required")

if required && proposedValue == nil {
if required && field.IsZero() {
return nil, fmt.Errorf("structconf: required field %s is not set", field.Name())
}

return proposedValue, nil
return field.Value(), nil
}

0 comments on commit da64082

Please sign in to comment.