From dfde62cd8e8eb2b455b32a9c4f31215e3782b92a Mon Sep 17 00:00:00 2001 From: Ilya Ageev Date: Tue, 30 Sep 2025 18:10:16 +0400 Subject: [PATCH] Support pass reflect value --- defaults.go | 21 +++++++++++++-------- filler.go | 5 +++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/defaults.go b/defaults.go index 5513b98..a899085 100644 --- a/defaults.go +++ b/defaults.go @@ -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 { diff --git a/filler.go b/filler.go index abacefa..43dc8f2 100644 --- a/filler.go +++ b/filler.go @@ -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()