-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfanunmarshal_examples_test.go
More file actions
93 lines (75 loc) · 2.25 KB
/
fanunmarshal_examples_test.go
File metadata and controls
93 lines (75 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package fanunmarshal_test
import (
"fmt"
"sort"
fum "github.com/thisisdevelopment/fanunmarshal"
)
func ExampleFanUnMarshal_UnMarshalSlice() {
// small tiny example
testdata := [][]byte{
[]byte(`{ "data": "1", "other": 42}`),
[]byte(`{ "data": "2", "other": 1337}`),
[]byte(`{ "data": "3", "other": 161803398875}`),
}
// setup receiver struct
type SomeData struct {
Data string `json:"data"`
Other int `json:"other"`
}
// we expect slice of SomeData
var expected = SomeData{}
data := fum.New().
WithWorkers(10).
WithUseJsonIter().
UnMarshalSlice(testdata, &expected)
/** sort it for comparation, just for the example to pass
take notice that order of the returned slice is not in order you may expect it to be
**/
sort.Slice(data, func(i, j int) bool {
return data[i].(*SomeData).Other < data[j].(*SomeData).Other
})
// data is the slice returned from the UnMarshalSlice method
for _, d := range data {
fmt.Printf("%+v\n", d)
}
// Output: &{Data:1 Other:42}
// &{Data:2 Other:1337}
// &{Data:3 Other:161803398875}
}
func ExampleFanUnMarshal_UnMarshalChan() {
// small tiny example
testdata := [][]byte{
[]byte(`{ "data": "1", "other": 42}`),
[]byte(`{ "data": "2", "other": 1337}`),
[]byte(`{ "data": "3", "other": 161803398875}`),
}
// setup receiver struct
type SomeData struct {
Data string `json:"data"`
Other int `json:"other"`
}
// we expect slice of SomeData
// setup expected what we receive in the output channel
var expected = SomeData{}
// setup instance
var fm = fum.New().
WithWorkers(3). // length of data
WithUseJsonIter(). // use jsoniter lib
DisableAutoScaleDown() // because we're not sending the length of the data into UnMarshalChan we need to disable the scaledown
// setup our input channel
var pipe = fm.MakeChan(testdata)
// here we send in a nil as the length of the data, otherwise a pointer to int of length
var outputChan = fm.UnMarshalChan(pipe, &expected, nil)
/**
take notice that order of the channel is not in order you may expect it to be
**/
var summed = 0
// 42 + 1337 + 161803398875 = 161803400254
for d := range outputChan {
if data, ok := d.(*SomeData); ok {
summed += data.Other
}
}
fmt.Printf("%d\n", summed)
// Output: 161803400254
}