Skip to content

Add getFunctionByInterface #16

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions filler.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func (f *Filler) SetDefaultValue(field *FieldData) {
f.getFunctionByName,
f.getFunctionByType,
f.getFunctionByKind,
f.getFunctionByInterface,
}

for _, getter := range getters {
Expand Down Expand Up @@ -133,6 +134,28 @@ func (f *Filler) getFunctionByKind(field *FieldData) FillerFunc {
return nil
}

func (f *Filler) getFunctionByInterface(field *FieldData) FillerFunc {
if !field.Field.Type.Implements(defaultsType) {
return nil
}
return func(field *FieldData) {
if !field.Value.CanInterface() {
return
}
asDefaults, ok := field.Value.Interface().(Defaults)
if !ok {
return
}
field.Value.Set(reflect.ValueOf(asDefaults.Defaults(field.TagValue)))
}
}

var defaultsType = reflect.TypeOf((*Defaults)(nil)).Elem()

type Defaults interface {
Defaults(tagValue string) interface{}
}

// TypeHash is a string representing a reflect.Type following the next pattern:
// <package.name>.<type.name>
type TypeHash string
Expand Down