Files
learngo/logparser/v5/pipe/logcount.go

37 lines
762 B
Go
Raw Normal View History

2019-08-26 21:52:47 +03:00
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
2019-08-28 18:54:57 +03:00
package pipe
2019-08-26 21:52:47 +03:00
import "fmt"
2019-08-28 18:54:57 +03:00
// logCount counts the yielded records.
2019-08-26 21:52:47 +03:00
type logCount struct {
2019-08-28 18:54:57 +03:00
Iterator
2019-08-26 21:52:47 +03:00
n int
}
2019-08-28 18:54:57 +03:00
// Each yields to the inner iterator while counting the records.
// Reports the record number on an error.
func (lc *logCount) Each(yield func(Record)) error {
err := lc.Iterator.Each(func(r Record) {
2019-08-26 21:52:47 +03:00
lc.n++
yield(r)
})
if err != nil {
// lc.n+1: iterator.each won't call yield on err
return fmt.Errorf("record %d: %v", lc.n+1, err)
}
return nil
}
2019-08-28 18:54:57 +03:00
// count returns the last read record number.
2019-08-26 21:52:47 +03:00
func (lc *logCount) count() int {
return lc.n
}