Documentation ¶
Overview ¶
Package fanunmarshal is a concurrent unmarshaller
use with slices of byte slices [][]byte, for example for data coming from Redis using MGet if we need to unmarshal huge structs, this is where the package shines
Index ¶
- Constants
- type FanUnMarshal
- func (f *FanUnMarshal) DisableAutoScaleDown() IFanUnMarshal
- func (f *FanUnMarshal) MakeChan(data [][]byte) <-chan []byte
- func (f *FanUnMarshal) UnMarshalChan(pipe <-chan []byte, expected interface{}, dataLength *int) <-chan interface{}
- func (f *FanUnMarshal) UnMarshalSlice(data [][]byte, expected interface{}) []interface{}
- func (f *FanUnMarshal) WithUseJsonIter() IFanUnMarshal
- func (f *FanUnMarshal) WithWorkers(workers uint) IFanUnMarshal
- type IFanUnMarshal
Examples ¶
Constants ¶
View Source
const ( // default amount of workers DefaultWorkers = 2 // default scaledown the amount of workers set based on the list size AutoScaleDown = true // default use the standard json encoding lib UseStdLib = true // use jsoniter (faster) UseJsoniter = false )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FanUnMarshal ¶ added in v0.1.3
type FanUnMarshal struct {
// contains filtered or unexported fields
}
func (*FanUnMarshal) DisableAutoScaleDown ¶ added in v0.1.3
func (f *FanUnMarshal) DisableAutoScaleDown() IFanUnMarshal
DisableAutoScaleDown, disable scaling down the max amount of workers based on your list amount
func (*FanUnMarshal) MakeChan ¶ added in v0.1.3
func (f *FanUnMarshal) MakeChan(data [][]byte) <-chan []byte
MakeChan creates a channel from input byte slice
func (*FanUnMarshal) UnMarshalChan ¶ added in v0.1.3
func (f *FanUnMarshal) UnMarshalChan(pipe <-chan []byte, expected interface{}, dataLength *int) <-chan interface{}
UnMarshalChan unmarshal a channel, returning a channel
Example ¶
package main import ( "fmt" fum "github.com/thisisdevelopment/fanunmarshal" ) func main() { // 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
func (*FanUnMarshal) UnMarshalSlice ¶ added in v0.1.3
func (f *FanUnMarshal) UnMarshalSlice(data [][]byte, expected interface{}) []interface{}
UnMarshalSlice unmarshal a slice of []byte slices, returning the a slice
Example ¶
package main import ( "fmt" "sort" fum "github.com/thisisdevelopment/fanunmarshal" ) func main() { // 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 (*FanUnMarshal) WithUseJsonIter ¶ added in v0.1.3
func (f *FanUnMarshal) WithUseJsonIter() IFanUnMarshal
WithUseJsonIter use jsoniter lib instead of default std lib json package
func (*FanUnMarshal) WithWorkers ¶ added in v0.1.3
func (f *FanUnMarshal) WithWorkers(workers uint) IFanUnMarshal
WithWorkers set the amount of workers to work on your list
type IFanUnMarshal ¶ added in v0.0.2
type IFanUnMarshal interface { // WithWorkers set the amount of workers to work on your list WithWorkers(workers uint) IFanUnMarshal // DisableAutoScaleDown, disable scaling down the max amount of workers based on your list amount DisableAutoScaleDown() IFanUnMarshal // WithUseJsonIter use jsoniter lib instead of default std lib json package WithUseJsonIter() IFanUnMarshal // UnMarshalSlice unmarshal a slice, returning the slice UnMarshalSlice(data [][]byte, expected interface{}) []interface{} // MakeChan make a channel based of a slice of byteslices MakeChan(data [][]byte) <-chan []byte // UnMarshalChan unmarshal a channel, returning a channel UnMarshalChan(pipe <-chan []byte, expected interface{}, dataLength *int) <-chan interface{} }
Click to show internal directories.
Click to hide internal directories.