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

tagged defaults #45

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 .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
.idea/*
10 changes: 10 additions & 0 deletions defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ func Set(ptr interface{}) error {

for i := 0; i < t.NumField(); i++ {
if defaultVal := t.Field(i).Tag.Get(fieldName); defaultVal != "-" {
if t.Field(i).IsExported() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if t.Field(i).IsExported() {

// Check whether the field is exported
if t.Field(i).PkgPath != "" {

iface := v.Field(i).Addr().Interface()
if taggedSetter, ok := iface.(TaggedSetter); ok {
if err := taggedSetter.SetTaggedDefaults(defaultVal); err != nil {
return err
}
continue
}
}

if err := setField(v.Field(i), defaultVal); err != nil {
return err
}
Expand Down
42 changes: 42 additions & 0 deletions defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"reflect"
"strconv"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -757,3 +758,44 @@ func TestDefaultsSetter(t *testing.T) {
t.Errorf("expected 1 for MainInt, got %d", main.MainInt)
}
}

type Duration struct {
time.Duration
}

func (d *Duration) SetTaggedDefaults(tag string) (err error) {
d.Duration, err = time.ParseDuration(tag)
return
}

type TaggedDefaults struct {
Duration Duration `default:"1s"`
}

func TestTaggedDefaultsSetterHappy(t *testing.T) {
sample := &TaggedDefaults{}
err := Set(sample)
if err != nil {
t.Errorf("unexpected error: %s", err)
}

if sample.Duration.Duration != time.Second {
t.Errorf("expected 1s for Duration, got %s", sample.Duration.Duration)
}
}

type TaggedDefaultsSad struct {
Duration Duration `default:"invalid"`
}

func TestTaggedDefaultsSetterSad(t *testing.T) {
sample := &TaggedDefaultsSad{}
err := Set(sample)
if err == nil {
t.Error("unexpected success?!")
}

if !strings.HasPrefix(err.Error(), "time: invalid duration") {
t.Errorf("unexpected error: %s", err)
}
}
11 changes: 11 additions & 0 deletions setter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,14 @@ func callSetter(v interface{}) {
ds.SetDefaults()
}
}

type TaggedSetter interface {
SetTaggedDefaults(tag string) error
}

func callTaggedSetter(v interface{}, tag string) error {
if ds, ok := v.(TaggedSetter); ok {
return ds.SetTaggedDefaults(tag)
}
return nil
}