/sources/mpr/lib/processes/providers-line.go
https://bitbucket.org/cipriancraciun/mosaic-packages-repository · Go · 309 lines · 201 code · 89 blank · 19 comment · 25 complexity · c6134f9ce342b47c1d4b72b43b801395 MD5 · raw file
- // ---------------------------------------------------------------------------
- // ---------------------------------------------------------------------------
- package processes
- import "bytes"
- import "io"
- import "os"
- import "regexp"
- import "sync"
- // ---------------------------------------------------------------------------
- // ---------------------------------------------------------------------------
- type recordSink struct {
- pipedProvider
- recordSink RecordSinkFunc
- separator byte
- bufferSize uint
- buffer *bytes.Buffer
- }
- type RecordSinkFunc func ([]byte) (error)
- // ---------------------------------------------------------------------------
- type lineSink struct {
- recordSink
- }
- type LineChanSink struct {
- lineSink
- LineSink LineSinkChan
- ErrorSink ErrorSinkChan
- Separator byte
- BufferSize uint
- }
- type LineFuncSink struct {
- lineSink
- LineSink LineSinkFunc
- ErrorSink ErrorSinkFunc
- Separator byte
- BufferSize uint
- }
- type LineCollector struct {
- lineSink
- Collected []string
- Error error
- Separator byte
- BufferSize uint
- }
- type LineSinkChan chan<- *string
- type LineSinkFunc func (*string) (error)
- // ---------------------------------------------------------------------------
- type matchSink struct {
- recordSink
- }
- type MatchChanSink struct {
- matchSink
- MatchSink MatchSinkChan
- ErrorSink ErrorSinkChan
- Regexp *regexp.Regexp
- Separator byte
- BufferSize uint
- }
- type MatchFuncSink struct {
- matchSink
- MatchSink MatchSinkFunc
- ErrorSink ErrorSinkFunc
- Regexp *regexp.Regexp
- Separator byte
- BufferSize uint
- }
- type MatchCollector struct {
- matchSink
- Regexp *regexp.Regexp
- Separator byte
- BufferSize uint
- Collected [][]string
- Error error
- }
- type MatchSinkChan chan<- []string
- type MatchSinkFunc func ([]string) (error)
- // ---------------------------------------------------------------------------
- // ---------------------------------------------------------------------------
- func (_provider *recordSink) initialize_2 (_recordSink RecordSinkFunc, _errorSink ErrorSinkFunc, _separator byte, _bufferSize uint, _waitGroup *sync.WaitGroup) (*os.File, bool, error) {
-
- _provider.recordSink = _recordSink
- _provider.separator = _separator
- _provider.bufferSize = _bufferSize
- _provider.buffer = bytes.NewBuffer (make ([]byte, _provider.bufferSize))
-
- _dataSink := func (_data []byte) (error) {
- return _provider.dataSink (_data)
- }
-
- return _provider.initialize_1 (2, func () (error) {
- return pipeInbound (_provider.inboundStream, _provider.bufferSize, _dataSink, _errorSink, _waitGroup)
- })
- }
- func (_provider *recordSink) dataSink (_data []byte) (error) {
-
- if _data == nil {
- return _provider.recordSink (nil)
- }
-
- for {
- switch _record, _error := _provider.dataPush (_data); {
- case _error != nil :
- return _error
- case _record != nil :
- _data = nil
- switch _error := _provider.recordSink (_record); {
- case _error != nil :
- return _error
- }
- case _record == nil :
- return nil
- default :
- panic ("assertion")
- }
- }
-
- panic ("assertion")
- }
- func (_provider *recordSink) dataPush (_data []byte) ([]byte, error) {
-
- if _data != nil {
- _provider.buffer.Write (_data)
- }
-
- switch _bytes, _error := _provider.buffer.ReadBytes (_provider.separator); {
- case _error == io.EOF :
- return nil, nil
- case _error != nil :
- panic ("assertion")
- case _bytes[len (_bytes) - 1] == _provider.separator :
- return _bytes[:len (_bytes) - 1], nil
- default :
- panic ("assertion")
- }
-
- panic ("assertion")
- }
- // ---------------------------------------------------------------------------
- // ---------------------------------------------------------------------------
- func (_provider *lineSink) initialize_3 (_lineSink LineSinkFunc, _errorSink ErrorSinkFunc, _separator byte, _bufferSize uint, _waitGroup *sync.WaitGroup) (*os.File, bool, error) {
-
- _recordSink := func (_record []byte) (error) {
- if _record == nil {
- return _lineSink (nil)
- }
- _line := string (_record)
- return _lineSink (&_line)
- }
-
- return _provider.initialize_2 (_recordSink, _errorSink, _separator, _bufferSize, _waitGroup)
- }
- // ---------------------------------------------------------------------------
- func (_provider *LineChanSink) Initialize (_waitGroup *sync.WaitGroup) (*os.File, bool, error) {
-
- _lineSink := func (_line *string) (error) {
- _provider.LineSink <- _line
- return nil
- }
-
- _errorSink := func (_error error) (error) {
- _provider.ErrorSink <- _error
- return nil
- }
-
- return _provider.initialize_3 (_lineSink, _errorSink, _provider.Separator, _provider.BufferSize, _waitGroup)
- }
- // ---------------------------------------------------------------------------
- func (_provider *LineFuncSink) Initialize (_waitGroup *sync.WaitGroup) (*os.File, bool, error) {
-
- return _provider.initialize_3 (_provider.LineSink, _provider.ErrorSink, _provider.Separator, _provider.BufferSize, _waitGroup)
- }
- // ---------------------------------------------------------------------------
- func (_provider *LineCollector) Initialize (_waitGroup *sync.WaitGroup) (*os.File, bool, error) {
-
- _lineSink := func (_line *string) (error) {
- if _line == nil {
- return nil
- }
- _provider.Collected = append (_provider.Collected, *_line)
- return nil
- }
-
- _errorSink := func (_error error) (error) {
- _provider.Error = _error
- return nil
- }
-
- return _provider.initialize_3 (_lineSink, _errorSink, _provider.Separator, _provider.BufferSize, _waitGroup)
- }
- // ---------------------------------------------------------------------------
- // ---------------------------------------------------------------------------
- func (_provider *matchSink) initialize_3 (_matchSink MatchSinkFunc, _errorSink ErrorSinkFunc, _regexp *regexp.Regexp, _separator byte, _bufferSize uint, _waitGroup *sync.WaitGroup) (*os.File, bool, error) {
-
- _recordSink := func (_record []byte) (error) {
- if _record == nil {
- return _matchSink (nil)
- }
- _line := string (_record)
- switch _match := _regexp.FindStringSubmatch (_line); {
- case _match == nil :
- panic ("error")
- default :
- return _matchSink (_match)
- }
- panic ("assertion")
- }
-
- return _provider.initialize_2 (_recordSink, _errorSink, _separator, _bufferSize, _waitGroup)
- }
- func (_provider *MatchChanSink) Initialize (_waitGroup *sync.WaitGroup) (*os.File, bool, error) {
-
- _matchSink := func (_match []string) (error) {
- _provider.MatchSink <- _match
- return nil
- }
-
- _errorSink := func (_error error) (error) {
- _provider.ErrorSink <- _error
- return nil
- }
-
- return _provider.initialize_3 (_matchSink, _errorSink, _provider.Regexp, _provider.Separator, _provider.BufferSize, _waitGroup)
- }
- // ---------------------------------------------------------------------------
- func (_provider *MatchFuncSink) Initialize (_waitGroup *sync.WaitGroup) (*os.File, bool, error) {
-
- return _provider.initialize_3 (_provider.MatchSink, _provider.ErrorSink, _provider.Regexp, _provider.Separator, _provider.BufferSize, _waitGroup)
- }
- // ---------------------------------------------------------------------------
- func (_provider *MatchCollector) Initialize (_waitGroup *sync.WaitGroup) (*os.File, bool, error) {
-
- _matchSink := func (_match []string) (error) {
- if _match == nil {
- return nil
- }
- _provider.Collected = append (_provider.Collected, _match)
- return nil
- }
-
- _errorSink := func (_error error) (error) {
- _provider.Error = _error
- return nil
- }
-
- return _provider.initialize_3 (_matchSink, _errorSink, _provider.Regexp, _provider.Separator, _provider.BufferSize, _waitGroup)
- }
- // ---------------------------------------------------------------------------
- // ---------------------------------------------------------------------------