/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

  1. // ---------------------------------------------------------------------------
  2. // ---------------------------------------------------------------------------
  3. package processes
  4. import "bytes"
  5. import "io"
  6. import "os"
  7. import "regexp"
  8. import "sync"
  9. // ---------------------------------------------------------------------------
  10. // ---------------------------------------------------------------------------
  11. type recordSink struct {
  12. pipedProvider
  13. recordSink RecordSinkFunc
  14. separator byte
  15. bufferSize uint
  16. buffer *bytes.Buffer
  17. }
  18. type RecordSinkFunc func ([]byte) (error)
  19. // ---------------------------------------------------------------------------
  20. type lineSink struct {
  21. recordSink
  22. }
  23. type LineChanSink struct {
  24. lineSink
  25. LineSink LineSinkChan
  26. ErrorSink ErrorSinkChan
  27. Separator byte
  28. BufferSize uint
  29. }
  30. type LineFuncSink struct {
  31. lineSink
  32. LineSink LineSinkFunc
  33. ErrorSink ErrorSinkFunc
  34. Separator byte
  35. BufferSize uint
  36. }
  37. type LineCollector struct {
  38. lineSink
  39. Collected []string
  40. Error error
  41. Separator byte
  42. BufferSize uint
  43. }
  44. type LineSinkChan chan<- *string
  45. type LineSinkFunc func (*string) (error)
  46. // ---------------------------------------------------------------------------
  47. type matchSink struct {
  48. recordSink
  49. }
  50. type MatchChanSink struct {
  51. matchSink
  52. MatchSink MatchSinkChan
  53. ErrorSink ErrorSinkChan
  54. Regexp *regexp.Regexp
  55. Separator byte
  56. BufferSize uint
  57. }
  58. type MatchFuncSink struct {
  59. matchSink
  60. MatchSink MatchSinkFunc
  61. ErrorSink ErrorSinkFunc
  62. Regexp *regexp.Regexp
  63. Separator byte
  64. BufferSize uint
  65. }
  66. type MatchCollector struct {
  67. matchSink
  68. Regexp *regexp.Regexp
  69. Separator byte
  70. BufferSize uint
  71. Collected [][]string
  72. Error error
  73. }
  74. type MatchSinkChan chan<- []string
  75. type MatchSinkFunc func ([]string) (error)
  76. // ---------------------------------------------------------------------------
  77. // ---------------------------------------------------------------------------
  78. func (_provider *recordSink) initialize_2 (_recordSink RecordSinkFunc, _errorSink ErrorSinkFunc, _separator byte, _bufferSize uint, _waitGroup *sync.WaitGroup) (*os.File, bool, error) {
  79. _provider.recordSink = _recordSink
  80. _provider.separator = _separator
  81. _provider.bufferSize = _bufferSize
  82. _provider.buffer = bytes.NewBuffer (make ([]byte, _provider.bufferSize))
  83. _dataSink := func (_data []byte) (error) {
  84. return _provider.dataSink (_data)
  85. }
  86. return _provider.initialize_1 (2, func () (error) {
  87. return pipeInbound (_provider.inboundStream, _provider.bufferSize, _dataSink, _errorSink, _waitGroup)
  88. })
  89. }
  90. func (_provider *recordSink) dataSink (_data []byte) (error) {
  91. if _data == nil {
  92. return _provider.recordSink (nil)
  93. }
  94. for {
  95. switch _record, _error := _provider.dataPush (_data); {
  96. case _error != nil :
  97. return _error
  98. case _record != nil :
  99. _data = nil
  100. switch _error := _provider.recordSink (_record); {
  101. case _error != nil :
  102. return _error
  103. }
  104. case _record == nil :
  105. return nil
  106. default :
  107. panic ("assertion")
  108. }
  109. }
  110. panic ("assertion")
  111. }
  112. func (_provider *recordSink) dataPush (_data []byte) ([]byte, error) {
  113. if _data != nil {
  114. _provider.buffer.Write (_data)
  115. }
  116. switch _bytes, _error := _provider.buffer.ReadBytes (_provider.separator); {
  117. case _error == io.EOF :
  118. return nil, nil
  119. case _error != nil :
  120. panic ("assertion")
  121. case _bytes[len (_bytes) - 1] == _provider.separator :
  122. return _bytes[:len (_bytes) - 1], nil
  123. default :
  124. panic ("assertion")
  125. }
  126. panic ("assertion")
  127. }
  128. // ---------------------------------------------------------------------------
  129. // ---------------------------------------------------------------------------
  130. func (_provider *lineSink) initialize_3 (_lineSink LineSinkFunc, _errorSink ErrorSinkFunc, _separator byte, _bufferSize uint, _waitGroup *sync.WaitGroup) (*os.File, bool, error) {
  131. _recordSink := func (_record []byte) (error) {
  132. if _record == nil {
  133. return _lineSink (nil)
  134. }
  135. _line := string (_record)
  136. return _lineSink (&_line)
  137. }
  138. return _provider.initialize_2 (_recordSink, _errorSink, _separator, _bufferSize, _waitGroup)
  139. }
  140. // ---------------------------------------------------------------------------
  141. func (_provider *LineChanSink) Initialize (_waitGroup *sync.WaitGroup) (*os.File, bool, error) {
  142. _lineSink := func (_line *string) (error) {
  143. _provider.LineSink <- _line
  144. return nil
  145. }
  146. _errorSink := func (_error error) (error) {
  147. _provider.ErrorSink <- _error
  148. return nil
  149. }
  150. return _provider.initialize_3 (_lineSink, _errorSink, _provider.Separator, _provider.BufferSize, _waitGroup)
  151. }
  152. // ---------------------------------------------------------------------------
  153. func (_provider *LineFuncSink) Initialize (_waitGroup *sync.WaitGroup) (*os.File, bool, error) {
  154. return _provider.initialize_3 (_provider.LineSink, _provider.ErrorSink, _provider.Separator, _provider.BufferSize, _waitGroup)
  155. }
  156. // ---------------------------------------------------------------------------
  157. func (_provider *LineCollector) Initialize (_waitGroup *sync.WaitGroup) (*os.File, bool, error) {
  158. _lineSink := func (_line *string) (error) {
  159. if _line == nil {
  160. return nil
  161. }
  162. _provider.Collected = append (_provider.Collected, *_line)
  163. return nil
  164. }
  165. _errorSink := func (_error error) (error) {
  166. _provider.Error = _error
  167. return nil
  168. }
  169. return _provider.initialize_3 (_lineSink, _errorSink, _provider.Separator, _provider.BufferSize, _waitGroup)
  170. }
  171. // ---------------------------------------------------------------------------
  172. // ---------------------------------------------------------------------------
  173. func (_provider *matchSink) initialize_3 (_matchSink MatchSinkFunc, _errorSink ErrorSinkFunc, _regexp *regexp.Regexp, _separator byte, _bufferSize uint, _waitGroup *sync.WaitGroup) (*os.File, bool, error) {
  174. _recordSink := func (_record []byte) (error) {
  175. if _record == nil {
  176. return _matchSink (nil)
  177. }
  178. _line := string (_record)
  179. switch _match := _regexp.FindStringSubmatch (_line); {
  180. case _match == nil :
  181. panic ("error")
  182. default :
  183. return _matchSink (_match)
  184. }
  185. panic ("assertion")
  186. }
  187. return _provider.initialize_2 (_recordSink, _errorSink, _separator, _bufferSize, _waitGroup)
  188. }
  189. func (_provider *MatchChanSink) Initialize (_waitGroup *sync.WaitGroup) (*os.File, bool, error) {
  190. _matchSink := func (_match []string) (error) {
  191. _provider.MatchSink <- _match
  192. return nil
  193. }
  194. _errorSink := func (_error error) (error) {
  195. _provider.ErrorSink <- _error
  196. return nil
  197. }
  198. return _provider.initialize_3 (_matchSink, _errorSink, _provider.Regexp, _provider.Separator, _provider.BufferSize, _waitGroup)
  199. }
  200. // ---------------------------------------------------------------------------
  201. func (_provider *MatchFuncSink) Initialize (_waitGroup *sync.WaitGroup) (*os.File, bool, error) {
  202. return _provider.initialize_3 (_provider.MatchSink, _provider.ErrorSink, _provider.Regexp, _provider.Separator, _provider.BufferSize, _waitGroup)
  203. }
  204. // ---------------------------------------------------------------------------
  205. func (_provider *MatchCollector) Initialize (_waitGroup *sync.WaitGroup) (*os.File, bool, error) {
  206. _matchSink := func (_match []string) (error) {
  207. if _match == nil {
  208. return nil
  209. }
  210. _provider.Collected = append (_provider.Collected, _match)
  211. return nil
  212. }
  213. _errorSink := func (_error error) (error) {
  214. _provider.Error = _error
  215. return nil
  216. }
  217. return _provider.initialize_3 (_matchSink, _errorSink, _provider.Regexp, _provider.Separator, _provider.BufferSize, _waitGroup)
  218. }
  219. // ---------------------------------------------------------------------------
  220. // ---------------------------------------------------------------------------