-
Notifications
You must be signed in to change notification settings - Fork 16
/
linter.go
93 lines (79 loc) · 2.09 KB
/
linter.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
package linter
import (
"fmt"
"io"
"google.golang.org/protobuf/types/descriptorpb"
)
const (
// Path Types.
pathMessage = 4
pathEnumType = 5
pathEnumValue = 2
pathService = 6
pathRPCMethod = 2
pathMessageName = 1
pathMessageField = 2
pathMessageMessage = 3
pathMessageEnum = 4
)
type (
errorCode int
errorDescription string
)
const (
// Error Types.
errorImportOrder errorCode = iota
errorMessageCase
errorFieldCase
errorEnumTypeCase
errorEnumValueCase
errorServiceCase
errorRPCMethodCase
)
var linterErrors = []errorDescription{ //nolint:gochecknoglobals // Global enum values.
"Sort import statements alphabetically.",
"Use CamelCase (with an initial capital) for message names.",
"Use underscore_separated_names for field names.",
"Use CamelCase (with an initial capital) for enum type names.",
"Use CAPITALS_WITH_UNDERSCORES for enum value names.",
"Use CamelCase (with an initial capital) for service names.",
"Use CamelCase (with an initial capital) for RPC method names.",
}
type Config struct {
ProtoFile *descriptorpb.FileDescriptorProto
OutFile io.Writer
SortImports bool
}
// LintProtoFile takes a file name, proto file description, and a file.
// It checks the file for errors and writes them to the output file.
func LintProtoFile(conf Config) (int, error) {
var (
errors = protoBufErrors{}
protoSource = conf.ProtoFile.GetSourceCodeInfo()
)
if conf.SortImports {
errors.lintImportOrder(conf.ProtoFile.GetDependency())
}
for i, v := range conf.ProtoFile.GetMessageType() {
errors.lintProtoMessage(int32(i), pathMessage, []int32{}, v)
}
for i, v := range conf.ProtoFile.GetEnumType() {
errors.lintProtoEnumType(int32(i), pathEnumType, []int32{}, v)
}
for i, v := range conf.ProtoFile.GetService() {
errors.lintProtoService(int32(i), v)
}
for _, v := range errors {
line, col := v.getSourceLineNumber(protoSource)
fmt.Fprintf(
conf.OutFile,
"%s:%d:%d: '%s' - %s\n",
conf.ProtoFile.GetName(),
line,
col,
v.errorString,
linterErrors[v.errorCode],
)
}
return len(errors), nil
}