@@ -6,6 +6,16 @@ import (
6
6
"io"
7
7
)
8
8
9
+ // LineReader is an interface to read lines with optional Metadata.
10
+ type LineReader interface {
11
+ ReadLine () (string , * Metadata , error )
12
+ }
13
+
14
+ // Metadata contains metadata that belongs to a line.
15
+ type Metadata struct {
16
+ Package string
17
+ }
18
+
9
19
// LimitedLineReader reads lines from an io.Reader object with a configurable
10
20
// line size limit. Lines exceeding the limit will be truncated, but read
11
21
// completely from the underlying io.Reader.
@@ -14,6 +24,8 @@ type LimitedLineReader struct {
14
24
limit int
15
25
}
16
26
27
+ var _ LineReader = & LimitedLineReader {}
28
+
17
29
// NewLimitedLineReader returns a LimitedLineReader to read lines from r with a
18
30
// maximum line size of limit.
19
31
func NewLimitedLineReader (r io.Reader , limit int ) * LimitedLineReader {
@@ -23,14 +35,14 @@ func NewLimitedLineReader(r io.Reader, limit int) *LimitedLineReader {
23
35
// ReadLine returns the next line from the underlying reader. The length of the
24
36
// line will not exceed the configured limit. ReadLine either returns a line or
25
37
// it returns an error, never both.
26
- func (r * LimitedLineReader ) ReadLine () (string , error ) {
38
+ func (r * LimitedLineReader ) ReadLine () (string , * Metadata , error ) {
27
39
line , isPrefix , err := r .r .ReadLine ()
28
40
if err != nil {
29
- return "" , err
41
+ return "" , nil , err
30
42
}
31
43
32
44
if ! isPrefix {
33
- return string (line ), nil
45
+ return string (line ), nil , nil
34
46
}
35
47
36
48
// Line is incomplete, keep reading until we reach the end of the line.
@@ -39,7 +51,7 @@ func (r *LimitedLineReader) ReadLine() (string, error) {
39
51
for isPrefix {
40
52
line , isPrefix , err = r .r .ReadLine ()
41
53
if err != nil {
42
- return "" , err
54
+ return "" , nil , err
43
55
}
44
56
45
57
if buf .Len () >= r .limit {
@@ -54,5 +66,5 @@ func (r *LimitedLineReader) ReadLine() (string, error) {
54
66
if buf .Len () > r .limit {
55
67
buf .Truncate (r .limit )
56
68
}
57
- return buf .String (), nil
69
+ return buf .String (), nil , nil
58
70
}
0 commit comments