@@ -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+
3540type ExampleBasic struct {
3641 Bool bool `default:"true"`
3742 Integer int `default:"33"`
@@ -61,6 +66,39 @@ type ExampleBasic struct {
6166 StringSliceSlice [][]string `default:"[[1],[]]"`
6267
6368 DateTime string `default:"{{date:1,-10,0}} {{time:1,-5,10}}"`
69+
70+ BoolPtr * bool `default:"false"`
71+ IntPtr * int `default:"33"`
72+ Int8Ptr * int8 `default:"8"`
73+ Int16Ptr * int16 `default:"16"`
74+ Int32Ptr * int32 `default:"32"`
75+ Int64Ptr * int64 `default:"64"`
76+ UIntPtr * uint `default:"11"`
77+ UInt8Ptr * uint8 `default:"18"`
78+ UInt16Ptr * uint16 `default:"116"`
79+ UInt32Ptr * uint32 `default:"132"`
80+ UInt64Ptr * uint64 `default:"164"`
81+ Float32Ptr * float32 `default:"3.2"`
82+ Float64Ptr * float64 `default:"6.4"`
83+ DurationPtr * time.Duration `default:"1s"`
84+ SecondPtr * time.Duration `default:"1s"`
85+ StructPtr * struct {
86+ Bool bool `default:"true"`
87+ Integer * int `default:"33"`
88+ }
89+ PtrStructPtr * * struct {
90+ Bool bool `default:"false"`
91+ Integer * int `default:"33"`
92+ }
93+ ChildrenPtr []* ChildPtr
94+ PtrChildrenPtr * []* ChildPtr
95+ PtrPtrChildrenPtr * * []* ChildPtr
96+ PtrStringSliceNoTag * []string
97+ PtrStringSlice * []string `default:"[1,2,3,4]"`
98+ PtrIntSlice * []int `default:"[1,2,3,4]"`
99+ PtrIntSliceSlice * [][]int `default:"[[1],[2],[3],[4]]"`
100+ PtrStringSliceSlice * [][]string `default:"[[1],[]]"`
101+ Float64PtrNoTag * float64
64102}
65103
66104func (s * DefaultsSuite ) TestSetDefaultsBasic (c * C ) {
@@ -106,6 +144,34 @@ func (s *DefaultsSuite) assertTypes(c *C, foo *ExampleBasic) {
106144 c .Assert (foo .IntSliceSlice , DeepEquals , [][]int {[]int {1 }, []int {2 }, []int {3 }, []int {4 }})
107145 c .Assert (foo .StringSliceSlice , DeepEquals , [][]string {[]string {"1" }, []string {}})
108146 c .Assert (foo .DateTime , Equals , "2020-08-10 12:55:10" )
147+ c .Assert (* foo .BoolPtr , Equals , false )
148+ c .Assert (* foo .IntPtr , Equals , 33 )
149+ c .Assert (* foo .Int8Ptr , Equals , int8 (8 ))
150+ c .Assert (* foo .Int16Ptr , Equals , int16 (16 ))
151+ c .Assert (* foo .Int32Ptr , Equals , int32 (32 ))
152+ c .Assert (* foo .Int64Ptr , Equals , int64 (64 ))
153+ c .Assert (* foo .UIntPtr , Equals , uint (11 ))
154+ c .Assert (* foo .UInt8Ptr , Equals , uint8 (18 ))
155+ c .Assert (* foo .UInt16Ptr , Equals , uint16 (116 ))
156+ c .Assert (* foo .UInt32Ptr , Equals , uint32 (132 ))
157+ c .Assert (* foo .UInt64Ptr , Equals , uint64 (164 ))
158+ c .Assert (* foo .Float32Ptr , Equals , float32 (3.2 ))
159+ c .Assert (* foo .Float64Ptr , Equals , 6.4 )
160+ c .Assert (* foo .DurationPtr , Equals , time .Second )
161+ c .Assert (* foo .SecondPtr , Equals , time .Second )
162+ c .Assert (foo .StructPtr .Bool , Equals , true )
163+ c .Assert (* foo .StructPtr .Integer , Equals , 33 )
164+ c .Assert ((* foo .PtrStructPtr ).Bool , Equals , false )
165+ c .Assert (* (* foo .PtrStructPtr ).Integer , Equals , 33 )
166+ c .Assert (foo .ChildrenPtr , IsNil )
167+ c .Assert (* foo .PtrChildrenPtr , IsNil )
168+ c .Assert (* * foo .PtrPtrChildrenPtr , IsNil )
169+ c .Assert (* foo .PtrStringSliceNoTag , IsNil )
170+ c .Assert (* foo .PtrStringSlice , DeepEquals , []string {"1" , "2" , "3" , "4" })
171+ c .Assert (* foo .PtrIntSlice , DeepEquals , []int {1 , 2 , 3 , 4 })
172+ c .Assert (* foo .PtrIntSliceSlice , DeepEquals , [][]int {[]int {1 }, []int {2 }, []int {3 }, []int {4 }})
173+ c .Assert (* foo .PtrStringSliceSlice , DeepEquals , [][]string {[]string {"1" }, []string {}})
174+ c .Assert (foo .Float64PtrNoTag , IsNil )
109175}
110176
111177func (s * DefaultsSuite ) TestSetDefaultsWithValues (c * C ) {
@@ -118,6 +184,13 @@ func (s *DefaultsSuite) TestSetDefaultsWithValues(c *C) {
118184 Children : []Child {{Name : "alice" }, {Name : "bob" , Age : 2 }},
119185 }
120186
187+ intzero := 0
188+ foo .IntPtr = & intzero
189+
190+ ageZero := 0
191+ childPtr := & ChildPtr {Age : & ageZero }
192+ foo .ChildrenPtr = append (foo .ChildrenPtr , childPtr )
193+
121194 SetDefaults (foo )
122195
123196 c .Assert (foo .Integer , Equals , 55 )
@@ -127,6 +200,10 @@ func (s *DefaultsSuite) TestSetDefaultsWithValues(c *C) {
127200 c .Assert (string (foo .Bytes ), Equals , "foo" )
128201 c .Assert (foo .Children [0 ].Age , Equals , 10 )
129202 c .Assert (foo .Children [1 ].Age , Equals , 2 )
203+ c .Assert (* foo .ChildrenPtr [0 ].Age , Equals , 0 )
204+ c .Assert (foo .ChildrenPtr [0 ].Name , IsNil )
205+
206+ c .Assert (* foo .IntPtr , Equals , 0 )
130207}
131208
132209func (s * DefaultsSuite ) BenchmarkLogic (c * C ) {
0 commit comments