Skip to content

Commit c889686

Browse files
committed
Filler.FuncByType
1 parent 794935c commit c889686

File tree

3 files changed

+48
-22
lines changed

3 files changed

+48
-22
lines changed

defaults.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ func getDefaultFiller() *Filler {
3333

3434
func newDefaultFiller() *Filler {
3535
funcs := make(map[reflect.Kind]fillerFunc, 0)
36-
funcs[reflect.Bool] = func(field *fieldData, defaultValue string) {
37-
value, _ := strconv.ParseBool(defaultValue)
36+
funcs[reflect.Bool] = func(field *fieldData, tagValue string) {
37+
value, _ := strconv.ParseBool(tagValue)
3838
field.Value.SetBool(value)
3939
}
4040

41-
funcs[reflect.Int] = func(field *fieldData, defaultValue string) {
42-
value, _ := strconv.ParseInt(defaultValue, 10, 64)
41+
funcs[reflect.Int] = func(field *fieldData, tagValue string) {
42+
value, _ := strconv.ParseInt(tagValue, 10, 64)
4343
field.Value.SetInt(value)
4444
}
4545

@@ -48,15 +48,15 @@ func newDefaultFiller() *Filler {
4848
funcs[reflect.Int32] = funcs[reflect.Int]
4949
funcs[reflect.Int64] = funcs[reflect.Int]
5050

51-
funcs[reflect.Float32] = func(field *fieldData, defaultValue string) {
52-
value, _ := strconv.ParseFloat(defaultValue, 64)
51+
funcs[reflect.Float32] = func(field *fieldData, tagValue string) {
52+
value, _ := strconv.ParseFloat(tagValue, 64)
5353
field.Value.SetFloat(value)
5454
}
5555

5656
funcs[reflect.Float64] = funcs[reflect.Float32]
5757

58-
funcs[reflect.Uint] = func(field *fieldData, defaultValue string) {
59-
value, _ := strconv.ParseUint(defaultValue, 10, 64)
58+
funcs[reflect.Uint] = func(field *fieldData, tagValue string) {
59+
value, _ := strconv.ParseUint(tagValue, 10, 64)
6060
field.Value.SetUint(value)
6161
}
6262

@@ -65,21 +65,21 @@ func newDefaultFiller() *Filler {
6565
funcs[reflect.Uint32] = funcs[reflect.Uint]
6666
funcs[reflect.Uint64] = funcs[reflect.Uint]
6767

68-
funcs[reflect.String] = func(field *fieldData, defaultValue string) {
69-
field.Value.SetString(defaultValue)
68+
funcs[reflect.String] = func(field *fieldData, tagValue string) {
69+
field.Value.SetString(tagValue)
7070
}
7171

72-
funcs[reflect.Slice] = func(field *fieldData, defaultValue string) {
72+
funcs[reflect.Slice] = func(field *fieldData, tagValue string) {
7373
if field.Value.Type().Elem().Kind() == reflect.Uint8 {
7474
if field.Value.Bytes() != nil {
7575
return
7676
}
7777

78-
field.Value.SetBytes([]byte(defaultValue))
78+
field.Value.SetBytes([]byte(tagValue))
7979
}
8080
}
8181

82-
funcs[reflect.Struct] = func(field *fieldData, defaultValue string) {
82+
funcs[reflect.Struct] = func(field *fieldData, tagValue string) {
8383
fields := getDefaultFiller().getFieldsFromValue(field.Value)
8484
getDefaultFiller().setDefaultValues(fields)
8585
}

factory.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ func newFactoryFiller() *Filler {
4444
funcs[reflect.Int32] = funcs[reflect.Int]
4545
funcs[reflect.Int64] = funcs[reflect.Int]
4646

47-
funcs[reflect.Float32] = func(field *fieldData, defaultValue string) {
47+
funcs[reflect.Float32] = func(field *fieldData, tagValue string) {
4848
field.Value.SetFloat(rand.Float64())
4949
}
5050

5151
funcs[reflect.Float64] = funcs[reflect.Float32]
5252

53-
funcs[reflect.Uint] = func(field *fieldData, defaultValue string) {
53+
funcs[reflect.Uint] = func(field *fieldData, tagValue string) {
5454
field.Value.SetUint(uint64(rand.Uint32()))
5555
}
5656

@@ -59,11 +59,11 @@ func newFactoryFiller() *Filler {
5959
funcs[reflect.Uint32] = funcs[reflect.Uint]
6060
funcs[reflect.Uint64] = funcs[reflect.Uint]
6161

62-
funcs[reflect.String] = func(field *fieldData, defaultValue string) {
62+
funcs[reflect.String] = func(field *fieldData, tagValue string) {
6363
field.Value.SetString(randomString())
6464
}
6565

66-
funcs[reflect.Slice] = func(field *fieldData, defaultValue string) {
66+
funcs[reflect.Slice] = func(field *fieldData, tagValue string) {
6767
if field.Value.Type().Elem().Kind() == reflect.Uint8 {
6868
if field.Value.Bytes() != nil {
6969
return
@@ -73,7 +73,7 @@ func newFactoryFiller() *Filler {
7373
}
7474
}
7575

76-
funcs[reflect.Struct] = func(field *fieldData, defaultValue string) {
76+
funcs[reflect.Struct] = func(field *fieldData, tagValue string) {
7777
fields := getDefaultFiller().getFieldsFromValue(field.Value)
7878
getDefaultFiller().setDefaultValues(fields)
7979
}

filler.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package defaults
22

33
import (
4+
"fmt"
45
"reflect"
56
)
67

@@ -12,7 +13,7 @@ type fieldData struct {
1213
type fillerFunc func(field *fieldData, config string)
1314

1415
type Filler struct {
15-
FuncByName map[string]fillerFunc
16+
FuncByType map[TypeHash]fillerFunc
1617
FuncByKind map[reflect.Kind]fillerFunc
1718
Tag string
1819
}
@@ -90,12 +91,27 @@ func (f *Filler) isEmpty(field *fieldData) bool {
9091
func (f *Filler) setDefaultValue(field *fieldData) {
9192
tagValue := field.Field.Tag.Get(f.Tag)
9293

93-
function := f.getFunctionByKind(field.Field.Type.Kind())
94-
if function == nil {
94+
function := f.getFunctionByType(field.Field.Type)
95+
if function != nil {
96+
function(field, tagValue)
9597
return
9698
}
9799

98-
function(field, tagValue)
100+
function = f.getFunctionByKind(field.Field.Type.Kind())
101+
if function != nil {
102+
function(field, tagValue)
103+
return
104+
}
105+
106+
return
107+
}
108+
109+
func (f *Filler) getFunctionByType(t reflect.Type) fillerFunc {
110+
if f, ok := f.FuncByType[GetTypeHash(t)]; ok == true {
111+
return f
112+
}
113+
114+
return nil
99115
}
100116

101117
func (f *Filler) getFunctionByKind(k reflect.Kind) fillerFunc {
@@ -105,3 +121,13 @@ func (f *Filler) getFunctionByKind(k reflect.Kind) fillerFunc {
105121

106122
return nil
107123
}
124+
125+
type TypeHash string
126+
127+
func GetTypeHash(t reflect.Type) TypeHash {
128+
if t.Kind() == reflect.Ptr {
129+
t = t.Elem()
130+
}
131+
132+
return TypeHash(fmt.Sprintf("%s.%s", t.PkgPath(), t.Name()))
133+
}

0 commit comments

Comments
 (0)