@@ -34,7 +34,7 @@ cvt.BoolE("false") // false, nil
34
34
35
35
### custom type and pointers
36
36
37
- > dereferencing pointer and reach the base type
37
+ > dereferencing pointer and reach the original type
38
38
39
39
``` go
40
40
type Name string
@@ -72,8 +72,38 @@ cvt.Float("hello", 12.34) // 12.34
72
72
73
73
### bool
74
74
- Bool
75
+
76
+ ``` go
77
+ cvt.Bool (0 ) // false
78
+ cvt.Bool (nil ) // false
79
+ cvt.Bool (" 0" ) // false
80
+ cvt.Bool (" false" ) // false
81
+ cvt.Bool ([]int {}) // false
82
+
83
+ cvt.Bool (true ) // true
84
+ cvt.Bool (" true" ) // true
85
+ cvt.Bool ([]int {1 , 2 }) // true
86
+ cvt.Bool ([]byte (" true" )) // true
87
+ ```
88
+
75
89
- BoolE
76
90
91
+ ``` go
92
+ cvt.BoolE (0 ) // false,nil
93
+ cvt.BoolE (nil ) // false,nil
94
+ cvt.BoolE (" 0" ) // false,nil
95
+ cvt.BoolE (" false" ) // false,nil
96
+ cvt.BoolE ([]int {}) // false,nil
97
+
98
+ cvt.BoolE (true ) // true,nil
99
+ cvt.BoolE (" true" ) // true,nil
100
+ cvt.BoolE ([]int {1 , 2 }) // true,nil
101
+ cvt.BoolE ([]byte (" true" )) // true,nil
102
+ ```
103
+
104
+ > more case see [ bool_test.go] ( bool_test.go )
105
+
106
+
77
107
### int
78
108
- Int
79
109
- IntE
@@ -96,30 +126,201 @@ cvt.Float("hello", 12.34) // 12.34
96
126
- Uint64
97
127
- Uint64E
98
128
129
+ ``` go
130
+ cvt.Int (int8 (8 )) // 8
131
+ cvt.Int (int32 (8 )) // 8
132
+ cvt.Int (" -8.01" ) // -8
133
+ cvt.Int ([]byte (" 8.00" )) // 8
134
+ cvt.Int (nil ) // 0
135
+ cvt.IntE (" 8a" ) // 0,err
136
+ cvt.IntE ([]int {}) // 0,err
137
+
138
+ // alias type
139
+ type OrderType uint8
140
+ cvt.Int (OrderType (3 )) // 3
141
+
142
+ var po OrderType = 3
143
+ cvt.Int (&po) // 3
144
+ ```
145
+
146
+ > more case see [ int_test.go] ( int_test.go )
147
+
148
+
99
149
### string
100
150
- String
101
151
- StringE
102
152
153
+ ``` go
154
+ cvt.String (uint (8 )) // "8"
155
+ cvt.String (float32 (8.31 )) // "8.31"
156
+ cvt.String (true ) // "true"
157
+ cvt.String ([]byte (" -8.01" )) // "-8.01"
158
+ cvt.String (nil ) // ""
159
+
160
+ cvt.String (errors.New (" error info" )) // "error info"
161
+ cvt.String (time.Friday ) // "Friday"
162
+ cvt.String (big.NewInt (123 )) // "123"
163
+ cvt.String (template.URL (" https://host.foo" )) // "https://host.foo"
164
+ cvt.String (template.HTML (" <html></html>" )) // "<html></html>"
165
+ cvt.String (json.Number (" 12.34" )) // "12.34"
166
+
167
+ // custom type
168
+ type TestMarshalJSON struct {}
169
+
170
+ func (TestMarshalJSON ) MarshalJSON () ([]byte , error ) {
171
+ return []byte (" custom marshal" ), nil
172
+ }
173
+ cvt.String (TestMarshalJSON{}) // "custom marshal"
174
+ cvt.String (&TestMarshalJSON{}) // "custom marshal"
175
+ ```
176
+
177
+ > more case see [ string_test.go] ( string_test.go )
178
+
179
+
103
180
### float
104
181
- Float32
105
182
- Float32E
106
183
- Float64
107
184
- Float64E
108
185
186
+ ``` go
187
+ cvt.Float64 (int32 (8 )) // 8
188
+ cvt.Float64 (float32 (8.31 )) // 8.31
189
+ cvt.Float64 (" -8" ) // 8
190
+ cvt.Float64 (" -8.01" ) // 8.01
191
+ cvt.Float64 (nil ) // 0
192
+ cvt.Float64 (true ) // 1
193
+ cvt.Float64 (false ) // 0
194
+
195
+ type AliasTypeInt int
196
+ type PointerTypeInt *AliasTypeInt
197
+ cvt.Float64 (AliasTypeInt (8 )) // 8
198
+ cvt.Float64 ((*AliasTypeInt)(nil )) // 0
199
+ cvt.FLoat64 ((*PointerTypeInt)(nil )) // 0
200
+ ```
201
+
202
+ > more case see [ float_test.go] ( float_test.go )
203
+
109
204
### time
110
205
- Time
111
206
- TimeE
112
207
208
+ ``` go
209
+ cvt.Time (" 2009-11-10 23:00:00 +0000 UTC" )
210
+ cvt.Time (" 2018-10-21T23:21:29+0200" )
211
+ cvt.Time (" 10 Nov 09 23:00 UTC" )
212
+ cvt.Time (" 2009-11-10T23:00:00Z" )
213
+ cvt.Time (" 11:00PM" )
214
+ cvt.Time (" 2006-01-02" )
215
+ cvt.Time (" 2016-03-06 15:28:01" )
216
+ cvt.Time (1482597504 )
217
+ cvt.Time (time.Date (2009 , 2 , 13 , 23 , 31 , 30 , 0 , time.Local ))
218
+ ```
219
+
220
+ > more case see [ time_test.go] ( time_test.go )
221
+
113
222
### slice
114
223
- ` ColumnsE ` : the values from a single column in the input array/slice/map of struct/map, ` []interface{} `
224
+
225
+ ``` go
226
+ // []interface{}{"D1", "D2", nil}
227
+ cvt.ColumnsE ([]map [string ]interface {}{
228
+ {" 1" : 111 , " DDD" : " D1" },
229
+ {" 2" : 222 , " DDD" : " D2" },
230
+ {" DDD" : nil },
231
+ }, " DDD" )
232
+
233
+ // test type
234
+ type TestStructD struct {
235
+ D1 int
236
+ }
237
+ type TestStructE struct {
238
+ D1 int
239
+ DD *TestStructD
240
+ }
241
+
242
+ // []interface{}{11, 22}
243
+ cvt.ColumnsE (map [int ]TestStructD{1 : {11 }, 2 : {22 }}, " D1" )
244
+
245
+ // []interface{}{1, 2}
246
+ cvt.ColumnsE ([]TestStructE {{D1: 1 }, {D1: 2 }}, " D1" )
247
+ ```
248
+
115
249
- ` FieldE ` : the field value from map/struct, ` interface{} `
250
+
251
+ ``` go
252
+ // map
253
+ cvt.FieldE (map [int ]interface {}{123 : " 112233" }, " 123" ) // "112233"
254
+ cvt.FieldE (map [string ]interface {}{" 123" : " 112233" }, " 123" ) // "112233"
255
+
256
+ // struct
257
+ cvt.FieldE (struct {
258
+ A string
259
+ B int
260
+ }{" Hello" , 18 }, " A" ) // "Hello"
261
+ cvt.FieldE (struct {
262
+ A string
263
+ B int
264
+ }{" Hello" , 18 }, " B" ) // 18
265
+ ```
266
+
116
267
- ` KeysE ` : the keys of map, or fields of struct, ` []interface{} `
268
+
269
+ ``` go
270
+ cvt.KeysE ()
271
+ // key of map
272
+ cvt.KeysE (map [float64 ]float64 {0.1 : -0.1 , -1.2 : 1.2 }) // []interface{}{-1.2, 0.1}
273
+ cvt.KeysE (map [string ]interface {}{" A" : 1 , " 2" : 2 }) // []interface{}{"2", "A"}
274
+ cvt.KeysE (map [int ]map [string ]interface {}{1 : {" 1" : 111 , " DDD" : 12.3 }, -2 : {" 2" : 222 , " DDD" : " 321" }, 3 : {" DDD" : nil }}) // []interface{}{-2, 1, 3}
275
+
276
+ // field name of struct
277
+ cvt.KeysE (struct {
278
+ A string
279
+ B int
280
+ C float
281
+ }{}) // []interface{}{"A", "B", "C"}
282
+
283
+ type TestStructB {
284
+ B int
285
+ }
286
+ cvt.KeysE (struct {
287
+ A string
288
+ TestStructB
289
+ C float
290
+ }{}) // []interface{}{"A", "B", "C"}
291
+ ```
292
+
117
293
- ` Slice ` / ` SliceE ` : convert an interface to a ` []interface{} ` type
118
294
- ` SliceIntE ` : convert an interface to a ` []int ` type
119
295
- ` SliceInt64E ` : convert an interface to a ` []int64 ` type
120
296
- ` SliceFloat64E ` : convert an interface to a ` []float64 ` type
121
297
- ` SliceStringE ` : convert an interface to a ` []string ` type
122
298
299
+ ``` go
300
+ cvt.SliceE (" hello" ) // []interface{}{'h', 'e', 'l', 'l', 'o'}
301
+ cvt.SliceE ([]byte (" hey" )) // []interface{}{byte('h'), byte('e'), byte('y')}
302
+ cvt.SliceE ([]int {1 , 2 , 3 }) // []interface{}{1, 2, 3}
303
+ cvt.SliceE ([]string {" a" , " b" , " c" }) // []interface{}{"a", "b", "c"}
304
+ cvt.SliceE (map [int ]string {1 : " 111" , 2 : " 222" }) // []interface{}{"111", "222"}
305
+
306
+ // struct values
307
+ type TestStruct struct {
308
+ A int
309
+ B string
310
+ }
311
+ cvt.SliceE (TestStruct{18 ," jhon" }) // []interface{}{18, "jhon"}
312
+
313
+ // SliceIntE
314
+ cvt.SliceIntE ([]string {" 1" , " 2" , " 3" }) // []int{1, 2, 3}
315
+ cvt.SliceIntE (map [int ]string {2 : " 222" , 1 : " 111" }) // []int{111, 222}
316
+
317
+ // SliceStringE
318
+ cvt.SliceStringE ([]float64 {1.1 , 2.2 , 3.0 }) // []string{"1.1", "2.2", "3"}
319
+ cvt.SliceStringE (map [int ]string {2 : " 222" , 1 : " 11.1" }) // []string{"11.1", "222"}
320
+ ```
321
+
322
+ > more case see [ slice_test.go] ( slice_test.go )
323
+
123
324
124
325
## License
125
326
0 commit comments