-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
94 lines (85 loc) · 1.94 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"bufio"
"fmt"
"io"
"strings"
)
type ParsingErr struct {
Line int
Column int
Message string
}
func (e *ParsingErr) Error() string {
return fmt.Sprintf("parsing error found at %d:%d - %s", e.Line, e.Column, e.Message)
}
func ReadAll(p CodeownersParser) ([]Codeowner, []*ParsingErr, error) {
codeowners := []Codeowner{}
errors := []*ParsingErr{}
for {
codeowner, err := p.Read()
if err != nil {
parsingErr, isParsingError := err.(*ParsingErr)
if isParsingError {
errors = append(errors, parsingErr)
continue
}
if err == io.EOF {
return codeowners, errors, nil
}
return codeowners, errors, err
}
codeowners = append(codeowners, codeowner)
}
}
type CodeownersParser interface {
Read() (Codeowner, error)
}
type bufferedCodeownersParser struct {
buf *bufio.Reader
lineCount int
}
func NewBufferedCodeownersParser(buf *bufio.Reader) CodeownersParser {
return &bufferedCodeownersParser{
buf: buf,
lineCount: 0,
}
}
func (p *bufferedCodeownersParser) Read() (Codeowner, error) {
var codeowner Codeowner
input, err := p.readUntilNonEmptyLine()
if err != nil {
return codeowner, err
}
parts := strings.Split(input, " ")
if len(parts) == 1 {
return codeowner, &ParsingErr{
Line: p.lineCount,
Column: len(input),
Message: "No space was found",
}
}
codeowner.Pattern = Pattern(parts[0])
codeowner.Owners = make([]Owner, len(parts)-1)
for i, owner := range parts[1:] {
codeowner.Owners[i] = Owner(owner)
}
return codeowner, nil
}
func (p *bufferedCodeownersParser) readUntilNonEmptyLine() (string, error) {
for {
input, err := p.buf.ReadString('\n')
p.lineCount++
if (err != nil && err != io.EOF) || (err == io.EOF && len(input) == 0) {
return "", err
}
commentStarts := strings.Index(input, "#")
if commentStarts >= 0 {
input = input[:commentStarts]
}
input = strings.TrimSpace(input)
if len(input) > 0 {
return input, nil
}
}
}