Skip to content

Commit 9a6c754

Browse files
committed
add support of struct ptr & its slice
1 parent 576ea78 commit 9a6c754

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

defaults.go

+24-3
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ func newDefaultFiller() *Filler {
112112
fields := getDefaultFiller().GetFieldsFromValue(field.Value.Index(i), nil)
113113
getDefaultFiller().SetDefaultValues(fields)
114114
}
115+
case reflect.Ptr:
116+
count := field.Value.Len()
117+
for i := 0; i < count; i++ {
118+
if field.Value.Index(i).IsZero() {
119+
newValue := reflect.New(field.Value.Index(i).Type().Elem())
120+
field.Value.Index(i).Set(newValue)
121+
}
122+
fields := getDefaultFiller().GetFieldsFromValue(field.Value.Index(i).Elem(), nil)
123+
getDefaultFiller().SetDefaultValues(fields)
124+
}
115125
default:
116126
//处理形如 [1,2,3,4]
117127
reg := regexp.MustCompile(`^\[(.*)\]$`)
@@ -140,10 +150,23 @@ func newDefaultFiller() *Filler {
140150
}
141151

142152
funcs[reflect.Ptr] = func(field *FieldData) {
153+
154+
k := field.Value.Type().Elem().Kind()
155+
156+
if k == reflect.Struct {
157+
if field.Value.IsZero() {
158+
newValue := reflect.New(field.Value.Type().Elem())
159+
field.Value.Set(newValue)
160+
}
161+
fields := getDefaultFiller().GetFieldsFromValue(field.Value.Elem(), nil)
162+
getDefaultFiller().SetDefaultValues(fields)
163+
}
164+
143165
if field.TagValue == "" {
144166
return
145167
}
146-
switch field.Value.Type().Elem().Kind() {
168+
169+
switch k {
147170
case reflect.Bool:
148171
value, _ := strconv.ParseBool(field.TagValue)
149172
field.Value.Set(reflect.ValueOf(&value))
@@ -228,13 +251,11 @@ func parseDateTimeString(data string) string {
228251
case "date":
229252
str := time.Now().AddDate(values[0], values[1], values[2]).Format("2006-01-02")
230253
data = strings.Replace(data, match[0], str, -1)
231-
break
232254
case "time":
233255
str := time.Now().Add((time.Duration(values[0]) * time.Hour) +
234256
(time.Duration(values[1]) * time.Minute) +
235257
(time.Duration(values[2]) * time.Second)).Format("15:04:05")
236258
data = strings.Replace(data, match[0], str, -1)
237-
break
238259
}
239260
}
240261
}

defaults_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ type Child struct {
3232
Age int `default:"10"`
3333
}
3434

35+
type ChildPtr struct {
36+
Name *string
37+
Age *int `default:"10"`
38+
}
39+
3540
type ExampleBasic struct {
3641
Bool bool `default:"true"`
3742
Integer int `default:"33"`
@@ -77,6 +82,11 @@ type ExampleBasic struct {
7782
Float64Ptr *float64 `default:"6.4"`
7883
DurationPtr *time.Duration `default:"1s"`
7984
SecondPtr *time.Duration `default:"1s"`
85+
StructPtr *struct {
86+
Bool bool `default:"true"`
87+
Integer *int `default:"33"`
88+
}
89+
ChildrenPtr []*ChildPtr
8090
}
8191

8292
func (s *DefaultsSuite) TestSetDefaultsBasic(c *C) {
@@ -137,6 +147,9 @@ func (s *DefaultsSuite) assertTypes(c *C, foo *ExampleBasic) {
137147
c.Assert(*foo.Float64Ptr, Equals, 6.4)
138148
c.Assert(*foo.DurationPtr, Equals, time.Second)
139149
c.Assert(*foo.SecondPtr, Equals, time.Second)
150+
c.Assert(foo.StructPtr.Bool, Equals, true)
151+
c.Assert(*foo.StructPtr.Integer, Equals, 33)
152+
c.Assert(foo.ChildrenPtr, IsNil)
140153
}
141154

142155
func (s *DefaultsSuite) TestSetDefaultsWithValues(c *C) {
@@ -152,6 +165,10 @@ func (s *DefaultsSuite) TestSetDefaultsWithValues(c *C) {
152165
intzero := 0
153166
foo.IntPtr = &intzero
154167

168+
ageZero := 0
169+
childPtr := &ChildPtr{Age: &ageZero}
170+
foo.ChildrenPtr = append(foo.ChildrenPtr, childPtr)
171+
155172
SetDefaults(foo)
156173

157174
c.Assert(foo.Integer, Equals, 55)
@@ -161,6 +178,8 @@ func (s *DefaultsSuite) TestSetDefaultsWithValues(c *C) {
161178
c.Assert(string(foo.Bytes), Equals, "foo")
162179
c.Assert(foo.Children[0].Age, Equals, 10)
163180
c.Assert(foo.Children[1].Age, Equals, 2)
181+
c.Assert(*foo.ChildrenPtr[0].Age, Equals, 0)
182+
c.Assert(foo.ChildrenPtr[0].Name, IsNil)
164183

165184
c.Assert(*foo.IntPtr, Equals, 0)
166185
}

filler.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,22 @@ func (f *Filler) isEmpty(field *FieldData) bool {
8282
// always assume the structs in the slice is empty and can be filled
8383
// the actually struct filling logic should take care of the rest
8484
return true
85+
case reflect.Ptr:
86+
switch field.Value.Type().Elem().Elem().Kind() {
87+
case reflect.Struct:
88+
return true
89+
default:
90+
return field.Value.Len() == 0
91+
}
8592
default:
8693
return field.Value.Len() == 0
8794
}
8895
case reflect.String:
8996
return field.Value.String() == ""
9097
case reflect.Ptr:
98+
if field.Value.Type().Elem().Kind() == reflect.Struct {
99+
return true
100+
}
91101
return field.Value.IsZero()
92102
}
93103
return true
@@ -107,28 +117,26 @@ func (f *Filler) SetDefaultValue(field *FieldData) {
107117
return
108118
}
109119
}
110-
111-
return
112120
}
113121

114122
func (f *Filler) getFunctionByName(field *FieldData) FillerFunc {
115-
if f, ok := f.FuncByName[field.Field.Name]; ok == true {
123+
if f, ok := f.FuncByName[field.Field.Name]; ok {
116124
return f
117125
}
118126

119127
return nil
120128
}
121129

122130
func (f *Filler) getFunctionByType(field *FieldData) FillerFunc {
123-
if f, ok := f.FuncByType[GetTypeHash(field.Field.Type)]; ok == true {
131+
if f, ok := f.FuncByType[GetTypeHash(field.Field.Type)]; ok {
124132
return f
125133
}
126134

127135
return nil
128136
}
129137

130138
func (f *Filler) getFunctionByKind(field *FieldData) FillerFunc {
131-
if f, ok := f.FuncByKind[field.Field.Type.Kind()]; ok == true {
139+
if f, ok := f.FuncByKind[field.Field.Type.Kind()]; ok {
132140
return f
133141
}
134142

0 commit comments

Comments
 (0)