Skip to content

Commit 0ee2889

Browse files
committed
filler: moving tagValue inside of fieldData
1 parent 508ece0 commit 0ee2889

File tree

4 files changed

+42
-40
lines changed

4 files changed

+42
-40
lines changed

defaults.go

Lines changed: 14 additions & 14 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, tagValue string) {
37-
value, _ := strconv.ParseBool(tagValue)
36+
funcs[reflect.Bool] = func(field *fieldData) {
37+
value, _ := strconv.ParseBool(field.TagValue)
3838
field.Value.SetBool(value)
3939
}
4040

41-
funcs[reflect.Int] = func(field *fieldData, tagValue string) {
42-
value, _ := strconv.ParseInt(tagValue, 10, 64)
41+
funcs[reflect.Int] = func(field *fieldData) {
42+
value, _ := strconv.ParseInt(field.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, tagValue string) {
52-
value, _ := strconv.ParseFloat(tagValue, 64)
51+
funcs[reflect.Float32] = func(field *fieldData) {
52+
value, _ := strconv.ParseFloat(field.TagValue, 64)
5353
field.Value.SetFloat(value)
5454
}
5555

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

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

@@ -65,22 +65,22 @@ 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, tagValue string) {
69-
field.Value.SetString(tagValue)
68+
funcs[reflect.String] = func(field *fieldData) {
69+
field.Value.SetString(field.TagValue)
7070
}
7171

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

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

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

factory.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ func newFactoryFiller() *Filler {
2727

2828
funcs := make(map[reflect.Kind]fillerFunc, 0)
2929

30-
funcs[reflect.Bool] = func(field *fieldData, _ string) {
30+
funcs[reflect.Bool] = func(field *fieldData) {
3131
if rand.Intn(1) == 1 {
3232
field.Value.SetBool(true)
3333
} else {
3434
field.Value.SetBool(false)
3535
}
3636
}
3737

38-
funcs[reflect.Int] = func(field *fieldData, _ string) {
38+
funcs[reflect.Int] = func(field *fieldData) {
3939
field.Value.SetInt(int64(rand.Int()))
4040
}
4141

@@ -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, tagValue string) {
47+
funcs[reflect.Float32] = func(field *fieldData) {
4848
field.Value.SetFloat(rand.Float64())
4949
}
5050

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

53-
funcs[reflect.Uint] = func(field *fieldData, tagValue string) {
53+
funcs[reflect.Uint] = func(field *fieldData) {
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, tagValue string) {
62+
funcs[reflect.String] = func(field *fieldData) {
6363
field.Value.SetString(randomString())
6464
}
6565

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

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

filler.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import (
66
)
77

88
type fieldData struct {
9-
Field reflect.StructField
10-
Value reflect.Value
9+
Field reflect.StructField
10+
Value reflect.Value
11+
TagValue string
12+
Parent *fieldData
1113
}
1214

13-
type fillerFunc func(field *fieldData, config string)
15+
type fillerFunc func(field *fieldData)
1416

1517
// Filler contains all the functions to fill any struct field with any type
1618
// allowing to define function by Kind, Type of field name
@@ -31,10 +33,10 @@ func (f *Filler) Fill(variable interface{}) {
3133
func (f *Filler) getFields(variable interface{}) []*fieldData {
3234
valueObject := reflect.ValueOf(variable).Elem()
3335

34-
return f.getFieldsFromValue(valueObject)
36+
return f.getFieldsFromValue(valueObject, nil)
3537
}
3638

37-
func (f *Filler) getFieldsFromValue(valueObject reflect.Value) []*fieldData {
39+
func (f *Filler) getFieldsFromValue(valueObject reflect.Value, parent *fieldData) []*fieldData {
3840
typeObject := valueObject.Type()
3941

4042
count := valueObject.NumField()
@@ -45,8 +47,10 @@ func (f *Filler) getFieldsFromValue(valueObject reflect.Value) []*fieldData {
4547

4648
if value.CanSet() {
4749
results = append(results, &fieldData{
48-
Value: value,
49-
Field: field,
50+
Value: value,
51+
Field: field,
52+
TagValue: field.Tag.Get(f.Tag),
53+
Parent: parent,
5054
})
5155
}
5256
}
@@ -94,8 +98,6 @@ func (f *Filler) isEmpty(field *fieldData) bool {
9498
}
9599

96100
func (f *Filler) setDefaultValue(field *fieldData) {
97-
tagValue := field.Field.Tag.Get(f.Tag)
98-
99101
getters := []func(field *fieldData) fillerFunc{
100102
f.getFunctionByName,
101103
f.getFunctionByType,
@@ -105,7 +107,7 @@ func (f *Filler) setDefaultValue(field *fieldData) {
105107
for _, getter := range getters {
106108
filler := getter(field)
107109
if filler != nil {
108-
filler(field, tagValue)
110+
filler(field)
109111
return
110112
}
111113
}

filler_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ func (s *FillerSuite) TestFuncByNameIsEmpty(c *C) {
1919

2020
f := &Filler{
2121
FuncByName: map[string]fillerFunc{
22-
"Foo": func(field *fieldData, tagValue string) {
22+
"Foo": func(field *fieldData) {
2323
calledA = true
2424
},
2525
},
2626
FuncByKind: map[reflect.Kind]fillerFunc{
27-
reflect.Int: func(field *fieldData, tagValue string) {
27+
reflect.Int: func(field *fieldData) {
2828
calledB = true
2929
},
3030
},
@@ -42,12 +42,12 @@ func (s *FillerSuite) TestFuncByTypeIsEmpty(c *C) {
4242
t := GetTypeHash(reflect.TypeOf(new(FixtureTypeInt)))
4343
f := &Filler{
4444
FuncByType: map[TypeHash]fillerFunc{
45-
t: func(field *fieldData, tagValue string) {
45+
t: func(field *fieldData) {
4646
calledA = true
4747
},
4848
},
4949
FuncByKind: map[reflect.Kind]fillerFunc{
50-
reflect.Int: func(field *fieldData, tagValue string) {
50+
reflect.Int: func(field *fieldData) {
5151
calledB = true
5252
},
5353
},
@@ -61,7 +61,7 @@ func (s *FillerSuite) TestFuncByTypeIsEmpty(c *C) {
6161
func (s *FillerSuite) TestFuncByKindIsNotEmpty(c *C) {
6262
called := false
6363
f := &Filler{FuncByKind: map[reflect.Kind]fillerFunc{
64-
reflect.Int: func(field *fieldData, tagValue string) {
64+
reflect.Int: func(field *fieldData) {
6565
called = true
6666
},
6767
}}
@@ -77,8 +77,8 @@ func (s *FillerSuite) TestFuncByKindSlice(c *C) {
7777
func (s *FillerSuite) TestFuncByKindTag(c *C) {
7878
var called string
7979
f := &Filler{Tag: "foo", FuncByKind: map[reflect.Kind]fillerFunc{
80-
reflect.Int: func(field *fieldData, tagValue string) {
81-
called = tagValue
80+
reflect.Int: func(field *fieldData) {
81+
called = field.TagValue
8282
},
8383
}}
8484

@@ -91,7 +91,7 @@ func (s *FillerSuite) TestFuncByKindTag(c *C) {
9191
func (s *FillerSuite) TestFuncByKindIsEmpty(c *C) {
9292
called := false
9393
f := &Filler{FuncByKind: map[reflect.Kind]fillerFunc{
94-
reflect.Int: func(field *fieldData, tagValue string) {
94+
reflect.Int: func(field *fieldData) {
9595
called = true
9696
},
9797
}}

0 commit comments

Comments
 (0)