Skip to content

Commit 34bab33

Browse files
committed
making public some func and types to make Filler usable from other packages
1 parent 0ee2889 commit 34bab33

File tree

4 files changed

+54
-54
lines changed

4 files changed

+54
-54
lines changed

defaults.go

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

3434
func newDefaultFiller() *Filler {
35-
funcs := make(map[reflect.Kind]fillerFunc, 0)
36-
funcs[reflect.Bool] = func(field *fieldData) {
35+
funcs := make(map[reflect.Kind]FillerFunc, 0)
36+
funcs[reflect.Bool] = func(field *FieldData) {
3737
value, _ := strconv.ParseBool(field.TagValue)
3838
field.Value.SetBool(value)
3939
}
4040

41-
funcs[reflect.Int] = func(field *fieldData) {
41+
funcs[reflect.Int] = func(field *FieldData) {
4242
value, _ := strconv.ParseInt(field.TagValue, 10, 64)
4343
field.Value.SetInt(value)
4444
}
@@ -48,14 +48,14 @@ 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) {
51+
funcs[reflect.Float32] = func(field *FieldData) {
5252
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) {
58+
funcs[reflect.Uint] = func(field *FieldData) {
5959
value, _ := strconv.ParseUint(field.TagValue, 10, 64)
6060
field.Value.SetUint(value)
6161
}
@@ -65,11 +65,11 @@ 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) {
68+
funcs[reflect.String] = func(field *FieldData) {
6969
field.Value.SetString(field.TagValue)
7070
}
7171

72-
funcs[reflect.Slice] = func(field *fieldData) {
72+
funcs[reflect.Slice] = func(field *FieldData) {
7373
if field.Value.Type().Elem().Kind() == reflect.Uint8 {
7474
if field.Value.Bytes() != nil {
7575
return
@@ -79,9 +79,9 @@ func newDefaultFiller() *Filler {
7979
}
8080
}
8181

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

8787
return &Filler{FuncByKind: funcs, Tag: "default"}

factory.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ func getFactoryFiller() *Filler {
2525
func newFactoryFiller() *Filler {
2626
rand.Seed(time.Now().UTC().UnixNano())
2727

28-
funcs := make(map[reflect.Kind]fillerFunc, 0)
28+
funcs := make(map[reflect.Kind]FillerFunc, 0)
2929

30-
funcs[reflect.Bool] = func(field *fieldData) {
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) {
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) {
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) {
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) {
62+
funcs[reflect.String] = func(field *FieldData) {
6363
field.Value.SetString(randomString())
6464
}
6565

66-
funcs[reflect.Slice] = func(field *fieldData) {
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,9 +73,9 @@ func newFactoryFiller() *Filler {
7373
}
7474
}
7575

76-
funcs[reflect.Struct] = func(field *fieldData) {
77-
fields := getDefaultFiller().getFieldsFromValue(field.Value, nil)
78-
getDefaultFiller().setDefaultValues(fields)
76+
funcs[reflect.Struct] = func(field *FieldData) {
77+
fields := getFactoryFiller().GetFieldsFromValue(field.Value, nil)
78+
getFactoryFiller().SetDefaultValues(fields)
7979
}
8080

8181
return &Filler{FuncByKind: funcs, Tag: "factory"}

filler.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,48 @@ import (
55
"reflect"
66
)
77

8-
type fieldData struct {
8+
type FieldData struct {
99
Field reflect.StructField
1010
Value reflect.Value
1111
TagValue string
12-
Parent *fieldData
12+
Parent *FieldData
1313
}
1414

15-
type fillerFunc func(field *fieldData)
15+
type FillerFunc func(field *FieldData)
1616

1717
// Filler contains all the functions to fill any struct field with any type
1818
// allowing to define function by Kind, Type of field name
1919
type Filler struct {
20-
FuncByName map[string]fillerFunc
21-
FuncByType map[TypeHash]fillerFunc
22-
FuncByKind map[reflect.Kind]fillerFunc
20+
FuncByName map[string]FillerFunc
21+
FuncByType map[TypeHash]FillerFunc
22+
FuncByKind map[reflect.Kind]FillerFunc
2323
Tag string
2424
}
2525

2626
// Fill apply all the functions contained on Filler, setting all the possible
2727
// values
2828
func (f *Filler) Fill(variable interface{}) {
2929
fields := f.getFields(variable)
30-
f.setDefaultValues(fields)
30+
f.SetDefaultValues(fields)
3131
}
3232

33-
func (f *Filler) getFields(variable interface{}) []*fieldData {
33+
func (f *Filler) getFields(variable interface{}) []*FieldData {
3434
valueObject := reflect.ValueOf(variable).Elem()
3535

36-
return f.getFieldsFromValue(valueObject, nil)
36+
return f.GetFieldsFromValue(valueObject, nil)
3737
}
3838

39-
func (f *Filler) getFieldsFromValue(valueObject reflect.Value, parent *fieldData) []*fieldData {
39+
func (f *Filler) GetFieldsFromValue(valueObject reflect.Value, parent *FieldData) []*FieldData {
4040
typeObject := valueObject.Type()
4141

4242
count := valueObject.NumField()
43-
var results []*fieldData
43+
var results []*FieldData
4444
for i := 0; i < count; i++ {
4545
value := valueObject.Field(i)
4646
field := typeObject.Field(i)
4747

4848
if value.CanSet() {
49-
results = append(results, &fieldData{
49+
results = append(results, &FieldData{
5050
Value: value,
5151
Field: field,
5252
TagValue: field.Tag.Get(f.Tag),
@@ -58,15 +58,15 @@ func (f *Filler) getFieldsFromValue(valueObject reflect.Value, parent *fieldData
5858
return results
5959
}
6060

61-
func (f *Filler) setDefaultValues(fields []*fieldData) {
61+
func (f *Filler) SetDefaultValues(fields []*FieldData) {
6262
for _, field := range fields {
6363
if f.isEmpty(field) {
64-
f.setDefaultValue(field)
64+
f.SetDefaultValue(field)
6565
}
6666
}
6767
}
6868

69-
func (f *Filler) isEmpty(field *fieldData) bool {
69+
func (f *Filler) isEmpty(field *FieldData) bool {
7070
switch field.Value.Kind() {
7171
case reflect.Bool:
7272
if field.Value.Bool() != false {
@@ -97,8 +97,8 @@ func (f *Filler) isEmpty(field *fieldData) bool {
9797
return true
9898
}
9999

100-
func (f *Filler) setDefaultValue(field *fieldData) {
101-
getters := []func(field *fieldData) fillerFunc{
100+
func (f *Filler) SetDefaultValue(field *FieldData) {
101+
getters := []func(field *FieldData) FillerFunc{
102102
f.getFunctionByName,
103103
f.getFunctionByType,
104104
f.getFunctionByKind,
@@ -115,23 +115,23 @@ func (f *Filler) setDefaultValue(field *fieldData) {
115115
return
116116
}
117117

118-
func (f *Filler) getFunctionByName(field *fieldData) fillerFunc {
118+
func (f *Filler) getFunctionByName(field *FieldData) FillerFunc {
119119
if f, ok := f.FuncByName[field.Field.Name]; ok == true {
120120
return f
121121
}
122122

123123
return nil
124124
}
125125

126-
func (f *Filler) getFunctionByType(field *fieldData) fillerFunc {
126+
func (f *Filler) getFunctionByType(field *FieldData) FillerFunc {
127127
if f, ok := f.FuncByType[GetTypeHash(field.Field.Type)]; ok == true {
128128
return f
129129
}
130130

131131
return nil
132132
}
133133

134-
func (f *Filler) getFunctionByKind(field *fieldData) fillerFunc {
134+
func (f *Filler) getFunctionByKind(field *FieldData) FillerFunc {
135135
if f, ok := f.FuncByKind[field.Field.Type.Kind()]; ok == true {
136136
return f
137137
}

filler_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ func (s *FillerSuite) TestFuncByNameIsEmpty(c *C) {
1818
calledB := false
1919

2020
f := &Filler{
21-
FuncByName: map[string]fillerFunc{
22-
"Foo": func(field *fieldData) {
21+
FuncByName: map[string]FillerFunc{
22+
"Foo": func(field *FieldData) {
2323
calledA = true
2424
},
2525
},
26-
FuncByKind: map[reflect.Kind]fillerFunc{
27-
reflect.Int: func(field *fieldData) {
26+
FuncByKind: map[reflect.Kind]FillerFunc{
27+
reflect.Int: func(field *FieldData) {
2828
calledB = true
2929
},
3030
},
@@ -41,13 +41,13 @@ func (s *FillerSuite) TestFuncByTypeIsEmpty(c *C) {
4141

4242
t := GetTypeHash(reflect.TypeOf(new(FixtureTypeInt)))
4343
f := &Filler{
44-
FuncByType: map[TypeHash]fillerFunc{
45-
t: func(field *fieldData) {
44+
FuncByType: map[TypeHash]FillerFunc{
45+
t: func(field *FieldData) {
4646
calledA = true
4747
},
4848
},
49-
FuncByKind: map[reflect.Kind]fillerFunc{
50-
reflect.Int: func(field *fieldData) {
49+
FuncByKind: map[reflect.Kind]FillerFunc{
50+
reflect.Int: func(field *FieldData) {
5151
calledB = true
5252
},
5353
},
@@ -60,8 +60,8 @@ func (s *FillerSuite) TestFuncByTypeIsEmpty(c *C) {
6060

6161
func (s *FillerSuite) TestFuncByKindIsNotEmpty(c *C) {
6262
called := false
63-
f := &Filler{FuncByKind: map[reflect.Kind]fillerFunc{
64-
reflect.Int: func(field *fieldData) {
63+
f := &Filler{FuncByKind: map[reflect.Kind]FillerFunc{
64+
reflect.Int: func(field *FieldData) {
6565
called = true
6666
},
6767
}}
@@ -76,8 +76,8 @@ func (s *FillerSuite) TestFuncByKindSlice(c *C) {
7676

7777
func (s *FillerSuite) TestFuncByKindTag(c *C) {
7878
var called string
79-
f := &Filler{Tag: "foo", FuncByKind: map[reflect.Kind]fillerFunc{
80-
reflect.Int: func(field *fieldData) {
79+
f := &Filler{Tag: "foo", FuncByKind: map[reflect.Kind]FillerFunc{
80+
reflect.Int: func(field *FieldData) {
8181
called = field.TagValue
8282
},
8383
}}
@@ -90,8 +90,8 @@ func (s *FillerSuite) TestFuncByKindTag(c *C) {
9090

9191
func (s *FillerSuite) TestFuncByKindIsEmpty(c *C) {
9292
called := false
93-
f := &Filler{FuncByKind: map[reflect.Kind]fillerFunc{
94-
reflect.Int: func(field *fieldData) {
93+
f := &Filler{FuncByKind: map[reflect.Kind]FillerFunc{
94+
reflect.Int: func(field *FieldData) {
9595
called = true
9696
},
9797
}}

0 commit comments

Comments
 (0)