Skip to content

Commit

Permalink
Allow stopping the parser via a channel
Browse files Browse the repository at this point in the history
... this adds a new function to the LineProcessor interface
  • Loading branch information
tomachalek committed Apr 14, 2020
1 parent edf0969 commit 7a975bc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
9 changes: 7 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
13 changes: 13 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}

// ----
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 7a975bc

Please sign in to comment.