From 7a975bcdf1008061e9960b6940ef3cb83fe4cad4 Mon Sep 17 00:00:00 2001 From: Tomas Machalek Date: Tue, 14 Apr 2020 16:43:28 +0200 Subject: [PATCH] Allow stopping the parser via a channel ... this adds a new function to the LineProcessor interface --- go.mod | 4 ++-- go.sum | 9 +++++++-- parser.go | 13 +++++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index bdbd370..f330c3f 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,8 @@ -module github.com/tomachalek/vertigo/v3 +module github.com/tomachalek/vertigo/v4 go 1.12 require ( - github.com/stretchr/testify v1.3.0 + github.com/stretchr/testify v1.5.1 golang.org/x/text v0.3.2 ) diff --git a/go.sum b/go.sum index b8f3baa..f8098be 100644 --- a/go.sum +++ b/go.sum @@ -2,9 +2,14 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/parser.go b/parser.go index 232555a..e45684c 100644 --- a/parser.go +++ b/parser.go @@ -104,10 +104,13 @@ type structAttrAccumulator interface { // -------------------------------------------------------- +// LineProcessor describes an object able to handle +// Vertigo's parsing events. type LineProcessor interface { ProcToken(token *Token, line int, err error) ProcStruct(strc *Structure, line int, err error) ProcStructClose(strc *StructureClose, line int, err error) + StopChannel() chan struct{} } // ---- @@ -255,6 +258,8 @@ func ParseVerticalFile(conf *ParserConf, lproc LineProcessor) error { ch := make(chan []procItem) errCh := make(chan error, 1) chunk := make([]procItem, channelChunkSize) + stop := lproc.StopChannel() + go func() { i := 0 progress := 0 @@ -282,6 +287,14 @@ func ParseVerticalFile(conf *ParserConf, lproc LineProcessor) error { if progress%logProgressEachNth == 0 { log.Printf("...processed %d lines.\n", progress) } + select { + case <-stop: + log.Print("WARNING: Vertigo parser received a stop command") + errCh <- nil + close(ch) + return + default: + } } if i > 0 { ch <- chunk[:i]