@@ -12,12 +12,17 @@ type fieldData struct {
1212
1313type fillerFunc func (field * fieldData , config string )
1414
15+ // Filler contains all the functions to fill any struct field with any type
16+ // allowing to define function by Kind, Type of field name
1517type Filler struct {
18+ FuncByName map [string ]fillerFunc
1619 FuncByType map [TypeHash ]fillerFunc
1720 FuncByKind map [reflect.Kind ]fillerFunc
1821 Tag string
1922}
2023
24+ // Fill apply all the functions contained on Filler, setting all the possible
25+ // values
2126func (f * Filler ) Fill (variable interface {}) {
2227 fields := f .getFields (variable )
2328 f .setDefaultValues (fields )
@@ -33,7 +38,7 @@ func (f *Filler) getFieldsFromValue(valueObject reflect.Value) []*fieldData {
3338 typeObject := valueObject .Type ()
3439
3540 count := valueObject .NumField ()
36- results := make ( []* fieldData , 0 )
41+ var results []* fieldData
3742 for i := 0 ; i < count ; i ++ {
3843 value := valueObject .Field (i )
3944 field := typeObject .Field (i )
@@ -91,39 +96,52 @@ func (f *Filler) isEmpty(field *fieldData) bool {
9196func (f * Filler ) setDefaultValue (field * fieldData ) {
9297 tagValue := field .Field .Tag .Get (f .Tag )
9398
94- function := f . getFunctionByType (field . Field . Type )
95- if function != nil {
96- function ( field , tagValue )
97- return
99+ getters := [] func (field * fieldData ) fillerFunc {
100+ f . getFunctionByName ,
101+ f . getFunctionByType ,
102+ f . getFunctionByKind ,
98103 }
99104
100- function = f .getFunctionByKind (field .Field .Type .Kind ())
101- if function != nil {
102- function (field , tagValue )
103- return
105+ for _ , getter := range getters {
106+ filler := getter (field )
107+ if filler != nil {
108+ filler (field , tagValue )
109+ return
110+ }
104111 }
105112
106113 return
107114}
108115
109- func (f * Filler ) getFunctionByType (t reflect.Type ) fillerFunc {
110- if f , ok := f .FuncByType [GetTypeHash (t )]; ok == true {
116+ func (f * Filler ) getFunctionByName (field * fieldData ) fillerFunc {
117+ if f , ok := f .FuncByName [field .Field .Name ]; ok == true {
118+ return f
119+ }
120+
121+ return nil
122+ }
123+
124+ func (f * Filler ) getFunctionByType (field * fieldData ) fillerFunc {
125+ if f , ok := f .FuncByType [GetTypeHash (field .Field .Type )]; ok == true {
111126 return f
112127 }
113128
114129 return nil
115130}
116131
117- func (f * Filler ) getFunctionByKind (k reflect. Kind ) fillerFunc {
118- if f , ok := f .FuncByKind [k ]; ok == true {
132+ func (f * Filler ) getFunctionByKind (field * fieldData ) fillerFunc {
133+ if f , ok := f .FuncByKind [field . Field . Type . Kind () ]; ok == true {
119134 return f
120135 }
121136
122137 return nil
123138}
124139
140+ // TypeHash is a string representing a reflect.Type following the next pattern:
141+ // <package.name>.<type.name>
125142type TypeHash string
126143
144+ // GetTypeHash returns the TypeHash for a given reflect.Type
127145func GetTypeHash (t reflect.Type ) TypeHash {
128146 if t .Kind () == reflect .Ptr {
129147 t = t .Elem ()
0 commit comments