Skip to content
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
21 changes: 13 additions & 8 deletions defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,24 @@ import (
// the StructTag with name "default" and the directed value.
//
// Usage
// type ExampleBasic struct {
// Foo bool `default:"true"`
// Bar string `default:"33"`
// Qux int8
// Dur time.Duration `default:"2m3s"`
// }
//
// foo := &ExampleBasic{}
// SetDefaults(foo)
// type ExampleBasic struct {
// Foo bool `default:"true"`
// Bar string `default:"33"`
// Qux int8
// Dur time.Duration `default:"2m3s"`
// }
//
// foo := &ExampleBasic{}
// SetDefaults(foo)
func SetDefaults(variable interface{}) {
getDefaultFiller().Fill(variable)
}

func SetDefaultsReflect(value reflect.Value) {
getDefaultFiller().FillReflect(value)
}

var defaultFiller *Filler = nil

func getDefaultFiller() *Filler {
Expand Down
5 changes: 5 additions & 0 deletions filler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func (f *Filler) Fill(variable interface{}) {
f.SetDefaultValues(fields)
}

func (f *Filler) FillReflect(value reflect.Value) {
fields := f.GetFieldsFromValue(value, nil)
f.SetDefaultValues(fields)
}

func (f *Filler) getFields(variable interface{}) []*FieldData {
valueObject := reflect.ValueOf(variable).Elem()

Expand Down