Skip to content

Commit ad8be1b

Browse files
committed
update doc examples
1 parent 1644815 commit ad8be1b

File tree

2 files changed

+403
-1
lines changed

2 files changed

+403
-1
lines changed

README.md

+202-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ cvt.BoolE("false") // false, nil
3434

3535
### custom type and pointers
3636

37-
> dereferencing pointer and reach the base type
37+
> dereferencing pointer and reach the original type
3838
3939
```go
4040
type Name string
@@ -72,8 +72,38 @@ cvt.Float("hello", 12.34) // 12.34
7272

7373
### bool
7474
- 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+
7589
- BoolE
7690

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+
77107
### int
78108
- Int
79109
- IntE
@@ -96,30 +126,201 @@ cvt.Float("hello", 12.34) // 12.34
96126
- Uint64
97127
- Uint64E
98128

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+
99149
### string
100150
- String
101151
- StringE
102152

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+
103180
### float
104181
- Float32
105182
- Float32E
106183
- Float64
107184
- Float64E
108185

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+
109204
### time
110205
- Time
111206
- TimeE
112207

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+
113222
### slice
114223
- `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+
115249
- `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+
116267
- `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+
117293
- `Slice` / `SliceE`: convert an interface to a `[]interface{}` type
118294
- `SliceIntE`: convert an interface to a `[]int` type
119295
- `SliceInt64E`: convert an interface to a `[]int64` type
120296
- `SliceFloat64E`: convert an interface to a `[]float64` type
121297
- `SliceStringE`: convert an interface to a `[]string` type
122298

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+
123324

124325
## License
125326

0 commit comments

Comments
 (0)