Skip to content

Commit 3f5880c

Browse files
rvolosatovsckaznocha
authored andcommitted
Add sort_imports parameter for import statement order check (#13)
* Add sort_imports parameter for import statement order check * Fail on unmatched parameters
1 parent 95de9d7 commit 3f5880c

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

linter/linter.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,36 +46,41 @@ var linterErrors = []errorDescription{
4646
"Use CamelCase (with an initial capital) for RPC method names.",
4747
}
4848

49+
type Config struct {
50+
ProtoFile *descriptor.FileDescriptorProto
51+
OutFile io.WriteCloser
52+
SortImports bool
53+
}
54+
4955
// LintProtoFile takes a file name, proto file description, and a file.
5056
// It checks the file for errors and writes them to the output file
51-
func LintProtoFile(
52-
protoFile *descriptor.FileDescriptorProto,
53-
outFile io.WriteCloser,
54-
) (int, error) {
57+
func LintProtoFile(conf Config) (int, error) {
5558
var (
5659
errors = protoBufErrors{}
57-
protoSource = protoFile.GetSourceCodeInfo()
60+
protoSource = conf.ProtoFile.GetSourceCodeInfo()
5861
)
5962

60-
errors.lintImportOrder(protoFile.GetDependency())
63+
if conf.SortImports {
64+
errors.lintImportOrder(conf.ProtoFile.GetDependency())
65+
}
6166

62-
for i, v := range protoFile.GetMessageType() {
67+
for i, v := range conf.ProtoFile.GetMessageType() {
6368
errors.lintProtoMessage(int32(i), pathMessage, []int32{}, v)
6469
}
6570

66-
for i, v := range protoFile.GetEnumType() {
71+
for i, v := range conf.ProtoFile.GetEnumType() {
6772
errors.lintProtoEnumType(int32(i), pathEnumType, []int32{}, v)
6873
}
6974

70-
for i, v := range protoFile.GetService() {
75+
for i, v := range conf.ProtoFile.GetService() {
7176
errors.lintProtoService(int32(i), v)
7277
}
7378
for _, v := range errors {
7479
line, col := v.getSourceLineNumber(protoSource)
7580
fmt.Fprintf(
76-
outFile,
81+
conf.OutFile,
7782
"%s:%d:%d: '%s' - %s\n",
78-
*protoFile.Name,
83+
*conf.ProtoFile.Name,
7984
line,
8085
col,
8186
v.errorString,

main.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,59 @@
11
package main
22

33
import (
4+
"fmt"
45
"io/ioutil"
56
"os"
7+
"strings"
68

79
"github.com/ckaznocha/protoc-gen-lint/linter"
810
"github.com/golang/protobuf/proto"
911
protoc "github.com/golang/protobuf/protoc-gen-go/plugin"
1012
)
1113

14+
// SortImports represents the parameter, which can be specified to the tool invocation
15+
// to enable checking, whether the proto file imports are sorted alphabetically.
16+
const SortImports = "sort_imports"
17+
1218
func panicOnError(err error) {
1319
if err != nil {
1420
panic(err)
1521
}
1622
}
1723

1824
func main() {
19-
2025
var (
2126
totalErrors int
2227
generatorRequest protoc.CodeGeneratorRequest
28+
parameters struct {
29+
SortImports bool
30+
}
2331
)
2432

33+
for _, p := range strings.Split(generatorRequest.GetParameter(), ",") {
34+
switch p {
35+
case SortImports:
36+
parameters.SortImports = true
37+
default:
38+
fmt.Fprintf(os.Stderr, "Unmatched parameter: %s", p)
39+
totalErrors++
40+
}
41+
}
42+
if totalErrors != 0 {
43+
os.Exit(1)
44+
}
45+
2546
data, err := ioutil.ReadAll(os.Stdin)
2647
panicOnError(err)
2748

2849
panicOnError(proto.Unmarshal(data, &generatorRequest))
2950

3051
for _, file := range generatorRequest.GetProtoFile() {
31-
numErrors, err := linter.LintProtoFile(file, os.Stderr)
52+
numErrors, err := linter.LintProtoFile(linter.Config{
53+
ProtoFile: file,
54+
OutFile: os.Stderr,
55+
SortImports: parameters.SortImports,
56+
})
3257
panicOnError(err)
3358
totalErrors += numErrors
3459
}

0 commit comments

Comments
 (0)