Skip to content

Commit 90af637

Browse files
authored
Split out common types into descriptor and option packages (#2)
1 parent 17e55c9 commit 90af637

File tree

31 files changed

+609
-487
lines changed

31 files changed

+609
-487
lines changed

.golangci.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ issues:
5555
test: "CheckServiceHandlerOption"
5656
- linters:
5757
- exhaustive
58-
path: check/options.go
58+
path: option/options.go
5959
text: "reflect.Pointer|reflect.Ptr"
6060
- linters:
6161
- gocritic
@@ -101,3 +101,10 @@ issues:
101101
- linters:
102102
- varnamelen
103103
path: internal/pkg/xslices/xslices.go
104+
- linters:
105+
- revive
106+
path: internal/pkg/compare/compare.go
107+
- linters:
108+
- gosec
109+
path: check/checktest/checktest.go
110+
text: "G115:"

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ export GOBIN := $(abspath $(BIN))
1212
COPYRIGHT_YEARS := 2024
1313
LICENSE_IGNORE := --ignore testdata/
1414

15-
BUF_VERSION := v1.39.0
16-
GO_MOD_GOTOOLCHAIN := go1.23.0
17-
GOLANGCI_LINT_VERSION := v1.60.1
15+
BUF_VERSION := v1.42.0
16+
GO_MOD_GOTOOLCHAIN := go1.23.1
17+
GOLANGCI_LINT_VERSION := v1.60.3
1818
# https://github.com/golangci/golangci-lint/issues/4837
1919
GOLANGCI_LINT_GOTOOLCHAIN := $(GO_MOD_GOTOOLCHAIN)
2020
# Remove when we want to upgrade past Go 1.21

check/annotation.go

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ import (
1919
"sort"
2020

2121
checkv1 "buf.build/gen/go/bufbuild/bufplugin/protocolbuffers/go/buf/plugin/check/v1"
22+
descriptorv1 "buf.build/gen/go/bufbuild/bufplugin/protocolbuffers/go/buf/plugin/descriptor/v1"
23+
"buf.build/go/bufplugin/descriptor"
2224
)
2325

2426
// Annotation represents a rule Failure.
2527
//
2628
// An annotation always contains the ID of the Rule that failed. It also optionally
2729
// contains a user-readable message, a location of the failure, and a location of the
28-
// failure in the against Files.
30+
// failure in the against FileDescriptors.
2931
//
3032
// Annotations are created on the server-side via ResponseWriters, and returned
3133
// from Clients on Responses.
@@ -36,12 +38,12 @@ type Annotation interface {
3638
RuleID() string
3739
// Message is a user-readable message describing the failure.
3840
Message() string
39-
// Location is the location of the failure.
40-
Location() Location
41-
// AgainstLocation is the Location of the failure in the against Files.
41+
// FileLocation is the location of the failure.
42+
FileLocation() descriptor.FileLocation
43+
// AgainstFileLocation is the FileLocation of the failure in the against FileDescriptors.
4244
//
4345
// Will only potentially be produced for breaking change rules.
44-
AgainstLocation() Location
46+
AgainstFileLocation() descriptor.FileLocation
4547

4648
toProto() *checkv1.Annotation
4749

@@ -51,26 +53,26 @@ type Annotation interface {
5153
// *** PRIVATE ***
5254

5355
type annotation struct {
54-
ruleID string
55-
message string
56-
location Location
57-
againstLocation Location
56+
ruleID string
57+
message string
58+
fileLocation descriptor.FileLocation
59+
againstFileLocation descriptor.FileLocation
5860
}
5961

6062
func newAnnotation(
6163
ruleID string,
6264
message string,
63-
location Location,
64-
againstLocation Location,
65+
fileLocation descriptor.FileLocation,
66+
againstFileLocation descriptor.FileLocation,
6567
) (*annotation, error) {
6668
if ruleID == "" {
6769
return nil, errors.New("check.Annotation: RuleID is empty")
6870
}
6971
return &annotation{
70-
ruleID: ruleID,
71-
message: message,
72-
location: location,
73-
againstLocation: againstLocation,
72+
ruleID: ruleID,
73+
message: message,
74+
fileLocation: fileLocation,
75+
againstFileLocation: againstFileLocation,
7476
}, nil
7577
}
7678

@@ -82,31 +84,31 @@ func (a *annotation) Message() string {
8284
return a.message
8385
}
8486

85-
func (a *annotation) Location() Location {
86-
return a.location
87+
func (a *annotation) FileLocation() descriptor.FileLocation {
88+
return a.fileLocation
8789
}
8890

89-
func (a *annotation) AgainstLocation() Location {
90-
return a.againstLocation
91+
func (a *annotation) AgainstFileLocation() descriptor.FileLocation {
92+
return a.againstFileLocation
9193
}
9294

9395
func (a *annotation) toProto() *checkv1.Annotation {
9496
if a == nil {
9597
return nil
9698
}
97-
var protoLocation *checkv1.Location
98-
if a.location != nil {
99-
protoLocation = a.location.toProto()
99+
var protoFileLocation *descriptorv1.FileLocation
100+
if a.fileLocation != nil {
101+
protoFileLocation = a.fileLocation.ToProto()
100102
}
101-
var protoAgainstLocation *checkv1.Location
102-
if a.againstLocation != nil {
103-
protoAgainstLocation = a.againstLocation.toProto()
103+
var protoAgainstFileLocation *descriptorv1.FileLocation
104+
if a.againstFileLocation != nil {
105+
protoAgainstFileLocation = a.againstFileLocation.ToProto()
104106
}
105107
return &checkv1.Annotation{
106-
RuleId: a.RuleID(),
107-
Message: a.Message(),
108-
Location: protoLocation,
109-
AgainstLocation: protoAgainstLocation,
108+
RuleId: a.RuleID(),
109+
Message: a.Message(),
110+
FileLocation: protoFileLocation,
111+
AgainstFileLocation: protoAgainstFileLocation,
110112
}
111113
}
112114

check/check_service_handler_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"testing"
2020

2121
checkv1 "buf.build/gen/go/bufbuild/bufplugin/protocolbuffers/go/buf/plugin/check/v1"
22+
descriptorv1 "buf.build/gen/go/bufbuild/bufplugin/protocolbuffers/go/buf/plugin/descriptor/v1"
2223
"github.com/stretchr/testify/require"
2324
"google.golang.org/protobuf/proto"
2425
"google.golang.org/protobuf/types/descriptorpb"
@@ -40,15 +41,15 @@ func TestCheckServiceHandlerUniqueFiles(t *testing.T) {
4041
_, err = checkServiceHandler.Check(
4142
context.Background(),
4243
&checkv1.CheckRequest{
43-
Files: []*checkv1.File{
44+
FileDescriptors: []*descriptorv1.FileDescriptor{
4445
{
4546
FileDescriptorProto: &descriptorpb.FileDescriptorProto{
4647
Name: proto.String("foo.proto"),
4748
SourceCodeInfo: &descriptorpb.SourceCodeInfo{},
4849
},
4950
},
5051
},
51-
AgainstFiles: []*checkv1.File{
52+
AgainstFileDescriptors: []*descriptorv1.FileDescriptor{
5253
{
5354
FileDescriptorProto: &descriptorpb.FileDescriptorProto{
5455
Name: proto.String("foo.proto"),
@@ -63,7 +64,7 @@ func TestCheckServiceHandlerUniqueFiles(t *testing.T) {
6364
_, err = checkServiceHandler.Check(
6465
context.Background(),
6566
&checkv1.CheckRequest{
66-
Files: []*checkv1.File{
67+
FileDescriptors: []*descriptorv1.FileDescriptor{
6768
{
6869
FileDescriptorProto: &descriptorpb.FileDescriptorProto{
6970
Name: proto.String("foo.proto"),
@@ -86,15 +87,15 @@ func TestCheckServiceHandlerUniqueFiles(t *testing.T) {
8687
_, err = checkServiceHandler.Check(
8788
context.Background(),
8889
&checkv1.CheckRequest{
89-
Files: []*checkv1.File{
90+
FileDescriptors: []*descriptorv1.FileDescriptor{
9091
{
9192
FileDescriptorProto: &descriptorpb.FileDescriptorProto{
9293
Name: proto.String("foo.proto"),
9394
SourceCodeInfo: &descriptorpb.SourceCodeInfo{},
9495
},
9596
},
9697
},
97-
AgainstFiles: []*checkv1.File{
98+
AgainstFileDescriptors: []*descriptorv1.FileDescriptor{
9899
{
99100
FileDescriptorProto: &descriptorpb.FileDescriptorProto{
100101
Name: proto.String("bar.proto"),

check/checktest/checktest.go

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ import (
2525
"strconv"
2626
"testing"
2727

28-
checkv1 "buf.build/gen/go/bufbuild/bufplugin/protocolbuffers/go/buf/plugin/check/v1"
28+
descriptorv1 "buf.build/gen/go/bufbuild/bufplugin/protocolbuffers/go/buf/plugin/descriptor/v1"
2929
"buf.build/go/bufplugin/check"
30+
"buf.build/go/bufplugin/descriptor"
3031
"buf.build/go/bufplugin/internal/pkg/xslices"
32+
"buf.build/go/bufplugin/option"
3133
"github.com/bufbuild/protocompile"
3234
"github.com/bufbuild/protocompile/linker"
3335
"github.com/bufbuild/protocompile/parser"
@@ -116,25 +118,25 @@ func (r *RequestSpec) ToRequest(ctx context.Context) (check.Request, error) {
116118
return nil, errors.New("RequestSpec.Files not set")
117119
}
118120

119-
againstFiles, err := r.AgainstFiles.ToFiles(ctx)
121+
againstFileDescriptors, err := r.AgainstFiles.ToFileDescriptors(ctx)
120122
if err != nil {
121123
return nil, err
122124
}
123-
options, err := check.NewOptions(r.Options)
125+
options, err := option.NewOptions(r.Options)
124126
if err != nil {
125127
return nil, err
126128
}
127129
requestOptions := []check.RequestOption{
128-
check.WithAgainstFiles(againstFiles),
130+
check.WithAgainstFileDescriptors(againstFileDescriptors),
129131
check.WithOptions(options),
130132
check.WithRuleIDs(r.RuleIDs...),
131133
}
132134

133-
files, err := r.Files.ToFiles(ctx)
135+
fileDescriptors, err := r.Files.ToFileDescriptors(ctx)
134136
if err != nil {
135137
return nil, err
136138
}
137-
return check.NewRequest(files, requestOptions...)
139+
return check.NewRequest(fileDescriptors, requestOptions...)
138140
}
139141

140142
// ProtoFileSpec specifies files to be compiled for testing.
@@ -160,10 +162,10 @@ type ProtoFileSpec struct {
160162
FilePaths []string
161163
}
162164

163-
// ToFiles compiles the files into check.Files.
165+
// ToFileDescriptors compiles the files into descriptor.FileDescriptors.
164166
//
165167
// If p is nil, this returns an empty slice.
166-
func (p *ProtoFileSpec) ToFiles(ctx context.Context) ([]check.File, error) {
168+
func (p *ProtoFileSpec) ToFileDescriptors(ctx context.Context) ([]descriptor.FileDescriptor, error) {
167169
if p == nil {
168170
return nil, nil
169171
}
@@ -185,22 +187,22 @@ type ExpectedAnnotation struct {
185187
// against the value in Annotation. That is, it is valid to have an Annotation return
186188
// a message but to not set it on ExpectedAnnotation.
187189
Message string
188-
// Location is the location of the failure.
189-
Location *ExpectedLocation
190-
// AgainstLocation is the against location of the failure.
191-
AgainstLocation *ExpectedLocation
190+
// FileLocation is the location of the failure.
191+
FileLocation *ExpectedFileLocation
192+
// AgainstFileLocation is the against location of the failure.
193+
AgainstFileLocation *ExpectedFileLocation
192194
}
193195

194196
// String implements fmt.Stringer.
195197
func (ea ExpectedAnnotation) String() string {
196198
return "ruleID=\"" + ea.RuleID + "\"" +
197199
" message=\"" + ea.Message + "\"" +
198-
" location=\"" + ea.Location.String() + "\"" +
199-
" againstLocation=\"" + ea.AgainstLocation.String() + "\""
200+
" location=\"" + ea.FileLocation.String() + "\"" +
201+
" againstLocation=\"" + ea.AgainstFileLocation.String() + "\""
200202
}
201203

202-
// ExpectedLocation contains the values expected from a Location.
203-
type ExpectedLocation struct {
204+
// ExpectedFileLocation contains the values expected from a Location.
205+
type ExpectedFileLocation struct {
204206
// FileName is the name of the file.
205207
FileName string
206208
// StartLine is the zero-indexed start line.
@@ -214,7 +216,7 @@ type ExpectedLocation struct {
214216
}
215217

216218
// String implements fmt.Stringer.
217-
func (el *ExpectedLocation) String() string {
219+
func (el *ExpectedFileLocation) String() string {
218220
if el == nil {
219221
return "nil"
220222
}
@@ -292,28 +294,28 @@ func expectedAnnotationForAnnotation(annotation check.Annotation) ExpectedAnnota
292294
RuleID: annotation.RuleID(),
293295
Message: annotation.Message(),
294296
}
295-
if location := annotation.Location(); location != nil {
296-
expectedAnnotation.Location = &ExpectedLocation{
297-
FileName: location.File().FileDescriptor().Path(),
298-
StartLine: location.StartLine(),
299-
StartColumn: location.StartColumn(),
300-
EndLine: location.EndLine(),
301-
EndColumn: location.EndColumn(),
297+
if fileLocation := annotation.FileLocation(); fileLocation != nil {
298+
expectedAnnotation.FileLocation = &ExpectedFileLocation{
299+
FileName: fileLocation.FileDescriptor().ProtoreflectFileDescriptor().Path(),
300+
StartLine: fileLocation.StartLine(),
301+
StartColumn: fileLocation.StartColumn(),
302+
EndLine: fileLocation.EndLine(),
303+
EndColumn: fileLocation.EndColumn(),
302304
}
303305
}
304-
if againstLocation := annotation.AgainstLocation(); againstLocation != nil {
305-
expectedAnnotation.AgainstLocation = &ExpectedLocation{
306-
FileName: againstLocation.File().FileDescriptor().Path(),
307-
StartLine: againstLocation.StartLine(),
308-
StartColumn: againstLocation.StartColumn(),
309-
EndLine: againstLocation.EndLine(),
310-
EndColumn: againstLocation.EndColumn(),
306+
if againstFileLocation := annotation.AgainstFileLocation(); againstFileLocation != nil {
307+
expectedAnnotation.AgainstFileLocation = &ExpectedFileLocation{
308+
FileName: againstFileLocation.FileDescriptor().ProtoreflectFileDescriptor().Path(),
309+
StartLine: againstFileLocation.StartLine(),
310+
StartColumn: againstFileLocation.StartColumn(),
311+
EndLine: againstFileLocation.EndLine(),
312+
EndColumn: againstFileLocation.EndColumn(),
311313
}
312314
}
313315
return expectedAnnotation
314316
}
315317

316-
func compile(ctx context.Context, dirPaths []string, filePaths []string) ([]check.File, error) {
318+
func compile(ctx context.Context, dirPaths []string, filePaths []string) ([]descriptor.FileDescriptor, error) {
317319
dirPaths = fromSlashPaths(dirPaths)
318320
filePaths = fromSlashPaths(filePaths)
319321
toSlashFilePathMap := make(map[string]struct{}, len(filePaths))
@@ -351,22 +353,22 @@ func compile(ctx context.Context, dirPaths []string, filePaths []string) ([]chec
351353
}
352354
fileDescriptorSet := fileDescriptorSetForFileDescriptors(files)
353355

354-
protoFiles := make([]*checkv1.File, len(fileDescriptorSet.GetFile()))
356+
protoFileDescriptors := make([]*descriptorv1.FileDescriptor, len(fileDescriptorSet.GetFile()))
355357
for i, fileDescriptorProto := range fileDescriptorSet.GetFile() {
356358
_, isNotImport := toSlashFilePathMap[fileDescriptorProto.GetName()]
357359
_, isSyntaxUnspecified := syntaxUnspecifiedFilePaths[fileDescriptorProto.GetName()]
358360
unusedDependencyIndexes := unusedDependencyIndexesForFilePathToUnusedDependencyFilePaths(
359361
fileDescriptorProto,
360362
filePathToUnusedDependencyFilePaths[fileDescriptorProto.GetName()],
361363
)
362-
protoFiles[i] = &checkv1.File{
364+
protoFileDescriptors[i] = &descriptorv1.FileDescriptor{
363365
FileDescriptorProto: fileDescriptorProto,
364366
IsImport: !isNotImport,
365367
IsSyntaxUnspecified: isSyntaxUnspecified,
366368
UnusedDependency: unusedDependencyIndexes,
367369
}
368370
}
369-
return check.FilesForProtoFiles(protoFiles)
371+
return descriptor.FileDescriptorsForProtoFileDescriptors(protoFileDescriptors)
370372
}
371373

372374
func unusedDependencyIndexesForFilePathToUnusedDependencyFilePaths(

0 commit comments

Comments
 (0)