@@ -2,7 +2,9 @@ package defaults
22
33import (
44 "reflect"
5+ "regexp"
56 "strconv"
7+ "strings"
68 "time"
79)
810
@@ -79,6 +81,17 @@ func newDefaultFiller() *Filler {
7981 field .Value .SetString (field .TagValue )
8082 }
8183
84+ funcs [reflect .Struct ] = func (field * FieldData ) {
85+ fields := getDefaultFiller ().GetFieldsFromValue (field .Value , nil )
86+ getDefaultFiller ().SetDefaultValues (fields )
87+ }
88+
89+ types := make (map [TypeHash ]FillerFunc , 1 )
90+ types ["time.Duration" ] = func (field * FieldData ) {
91+ d , _ := time .ParseDuration (field .TagValue )
92+ field .Value .Set (reflect .ValueOf (d ))
93+ }
94+
8295 funcs [reflect .Slice ] = func (field * FieldData ) {
8396 k := field .Value .Type ().Elem ().Kind ()
8497 switch k {
@@ -93,19 +106,32 @@ func newDefaultFiller() *Filler {
93106 fields := getDefaultFiller ().GetFieldsFromValue (field .Value .Index (i ), nil )
94107 getDefaultFiller ().SetDefaultValues (fields )
95108 }
109+ default :
110+ //处理形如 [1,2,3,4]
111+ reg := regexp .MustCompile (`^\[(.*)\]$` )
112+ matchs := reg .FindStringSubmatch (field .TagValue )
113+ if len (matchs ) != 2 {
114+ return
115+ }
116+ if matchs [1 ] == "" {
117+ field .Value .Set (reflect .MakeSlice (field .Value .Type (), 0 , 0 ))
118+ } else {
119+ defaultValue := strings .Split (matchs [1 ], "," )
120+ result := reflect .MakeSlice (field .Value .Type (), len (defaultValue ), len (defaultValue ))
121+ for i := 0 ; i < len (defaultValue ); i ++ {
122+ itemValue := result .Index (i )
123+ item := & FieldData {
124+ Value : itemValue ,
125+ Field : reflect.StructField {},
126+ TagValue : defaultValue [i ],
127+ Parent : nil ,
128+ }
129+ funcs [k ](item )
130+ }
131+ field .Value .Set (result )
132+ }
96133 }
97134 }
98135
99- funcs [reflect .Struct ] = func (field * FieldData ) {
100- fields := getDefaultFiller ().GetFieldsFromValue (field .Value , nil )
101- getDefaultFiller ().SetDefaultValues (fields )
102- }
103-
104- types := make (map [TypeHash ]FillerFunc , 1 )
105- types ["time.Duration" ] = func (field * FieldData ) {
106- d , _ := time .ParseDuration (field .TagValue )
107- field .Value .Set (reflect .ValueOf (d ))
108- }
109-
110136 return & Filler {FuncByKind : funcs , FuncByType : types , Tag : "default" }
111137}
0 commit comments