-
Notifications
You must be signed in to change notification settings - Fork 353
Expand file tree
/
Copy pathbreaking_util.go
More file actions
332 lines (307 loc) · 11.9 KB
/
breaking_util.go
File metadata and controls
332 lines (307 loc) · 11.9 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Copyright 2020-2025 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bufcheckserverhandle
import (
"fmt"
"sort"
"strconv"
"strings"
"github.com/bufbuild/buf/private/bufpkg/bufcheck/bufcheckserver/internal/bufcheckserverutil/customfeatures/customfeatures"
"github.com/bufbuild/buf/private/bufpkg/bufprotosource"
"github.com/bufbuild/buf/private/gen/proto/go/google/protobuf"
"github.com/bufbuild/protocompile/protoutil"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
gofeaturespb "google.golang.org/protobuf/types/gofeaturespb"
)
const (
featuresFieldName = "features"
featureNameUTF8Validation = "utf8_validation"
featureNameJSONFormat = "json_format"
cppFeatureNameStringType = "string_type"
javaFeatureNameUTF8Validation = "utf8_validation"
goFeatureNameStripEnumPrefix = "strip_enum_prefix"
goFeatureNameAPILevel = "api_level"
)
var (
// https://developers.google.com/protocol-buffers/docs/proto3#updating
fieldKindToWireCompatibilityGroup = map[protoreflect.Kind]int{
protoreflect.Int32Kind: 1,
protoreflect.Int64Kind: 1,
protoreflect.Uint32Kind: 1,
protoreflect.Uint64Kind: 1,
protoreflect.BoolKind: 1,
protoreflect.Sint32Kind: 2,
protoreflect.Sint64Kind: 2,
// While string and bytes are compatible if the bytes are valid UTF-8, we cannot
// determine if a field will actually be valid UTF-8, as we are concerned with the
// definitions and not individual messages, so we have these in different
// compatibility groups. We allow string to evolve to bytes, but not bytes to
// string, but we need them to be in different compatibility groups so that
// we have to manually detect this.
protoreflect.StringKind: 3,
protoreflect.BytesKind: 4,
protoreflect.Fixed32Kind: 5,
protoreflect.Sfixed32Kind: 5,
protoreflect.Fixed64Kind: 6,
protoreflect.Sfixed64Kind: 6,
protoreflect.DoubleKind: 7,
protoreflect.FloatKind: 8,
protoreflect.GroupKind: 9,
// Embedded messages are compatible with bytes if the bytes are serialized versions
// of the message, but we have no way of verifying this.
protoreflect.MessageKind: 10,
// Enum is compatible with int32, uint32, int64, uint64 if the values match
// an enum value, but we have no way of verifying this.
protoreflect.EnumKind: 11,
}
// httpsKind://developers.google.com/protocol-buffers/docs/proto3#json
// this is not just JSON-compatible, but also wire-compatible, i.e. the intersection
fieldKindToWireJSONCompatibilityGroup = map[protoreflect.Kind]int{
// fixed32 not compatible for wire so not included
protoreflect.Int32Kind: 1,
protoreflect.Uint32Kind: 1,
// fixed64 not compatible for wire so not included
protoreflect.Int64Kind: 2,
protoreflect.Uint64Kind: 2,
protoreflect.Fixed32Kind: 3,
protoreflect.Sfixed32Kind: 3,
protoreflect.Fixed64Kind: 4,
protoreflect.Sfixed64Kind: 4,
protoreflect.BoolKind: 5,
protoreflect.Sint32Kind: 6,
protoreflect.Sint64Kind: 7,
protoreflect.StringKind: 8,
protoreflect.BytesKind: 9,
protoreflect.DoubleKind: 10,
protoreflect.FloatKind: 11,
protoreflect.GroupKind: 12,
protoreflect.MessageKind: 13,
protoreflect.EnumKind: 14,
}
)
func fieldDescriptorTypePrettyString(descriptor protoreflect.FieldDescriptor) string {
if descriptor.Kind() == protoreflect.GroupKind && descriptor.Syntax() != protoreflect.Proto2 {
// Kind will be set to "group", but it's really a "delimited-encoded message"
return "message (delimited encoding)"
}
return descriptor.Kind().String()
}
func getDescriptorAndLocationForDeletedElement(
file bufprotosource.File,
previousNestedName string,
) (bufprotosource.Descriptor, bufprotosource.Location, error) {
if strings.Contains(previousNestedName, ".") {
nestedNameToMessage, err := bufprotosource.NestedNameToMessage(file)
if err != nil {
return nil, nil, err
}
split := strings.Split(previousNestedName, ".")
for i := len(split) - 1; i > 0; i-- {
if message, ok := nestedNameToMessage[strings.Join(split[0:i], ".")]; ok {
return message, message.Location(), nil
}
}
}
return file, nil, nil
}
func getDescriptorAndLocationForDeletedMessage(
file bufprotosource.File,
nestedNameToMessage map[string]bufprotosource.Message,
previousNestedName string,
) (bufprotosource.Descriptor, bufprotosource.Location) {
if strings.Contains(previousNestedName, ".") {
split := strings.Split(previousNestedName, ".")
for i := len(split) - 1; i > 0; i-- {
if message, ok := nestedNameToMessage[strings.Join(split[0:i], ".")]; ok {
return message, message.Location()
}
}
}
return file, nil
}
func getSortedEnumValueNames(nameToEnumValue map[string]bufprotosource.EnumValue) []string {
names := make([]string, 0, len(nameToEnumValue))
for name := range nameToEnumValue {
names = append(names, name)
}
sort.Strings(names)
return names
}
func getEnumByFullName(files []bufprotosource.File, enumFullName string) (bufprotosource.Enum, error) {
fullNameToEnum, err := bufprotosource.FullNameToEnum(files...)
if err != nil {
return nil, err
}
enum, ok := fullNameToEnum[enumFullName]
if !ok {
return nil, fmt.Errorf("expected enum %q to exist but was not found", enumFullName)
}
return enum, nil
}
func withBackupLocation(locs ...bufprotosource.Location) bufprotosource.Location {
for _, loc := range locs {
if loc != nil {
return loc
}
}
return nil
}
func findFeatureField(name protoreflect.Name, expectedKind protoreflect.Kind) (protoreflect.FieldDescriptor, error) {
featureSetDescriptor := (*descriptorpb.FeatureSet)(nil).ProtoReflect().Descriptor()
featureField := featureSetDescriptor.Fields().ByName(name)
if featureField == nil {
return nil, fmt.Errorf("unable to resolve field descriptor for %s.%s", featureSetDescriptor.FullName(), name)
}
if featureField.Kind() != expectedKind || featureField.IsList() {
return nil, fmt.Errorf("resolved field descriptor for %s.%s has unexpected type: expected optional %s, got %s %s",
featureSetDescriptor.FullName(), name, expectedKind, featureField.Cardinality(), featureField.Kind())
}
return featureField, nil
}
func fieldCppStringType(field bufprotosource.Field, descriptor protoreflect.FieldDescriptor) (protobuf.CppFeatures_StringType, bool, error) {
// We don't support Edition 2024 yet. But we know of this rule, so we can go ahead and
// implement it so it's one less thing to do when we DO add support for 2024.
if field.File().Edition() < descriptorpb.Edition_EDITION_2024 {
opts, _ := descriptor.Options().(*descriptorpb.FieldOptions)
// TODO: In Edition 2024, it will be *required* to use the new (pb.cpp).string_type option. So
// we shouldn't bother checking the ctype option in editions >= 2024.
if opts != nil && opts.Ctype != nil {
switch opts.GetCtype() {
case descriptorpb.FieldOptions_CORD:
return protobuf.CppFeatures_CORD, false, nil
case descriptorpb.FieldOptions_STRING_PIECE:
return protobuf.CppFeatures_STRING, true, nil
case descriptorpb.FieldOptions_STRING:
return protobuf.CppFeatures_STRING, false, nil
default:
if descriptor.ParentFile().Syntax() != protoreflect.Editions {
return protobuf.CppFeatures_STRING, false, nil
}
// If the file is edition 2023, we fall through to below since 2023 allows either
// the ctype field or the (pb.cpp).string_type feature.
}
}
}
val, err := customfeatures.ResolveCppFeature(descriptor, cppFeatureNameStringType, protoreflect.EnumKind)
if err != nil {
return 0, false, err
}
return protobuf.CppFeatures_StringType(val.Enum()), false, nil
}
func fieldCppStringTypeLocation(field bufprotosource.Field) bufprotosource.Location {
ext := protobuf.E_Cpp.TypeDescriptor()
if ext.Message() == nil {
return nil
}
return getCustomFeatureLocation(field, ext, cppFeatureNameStringType)
}
func fieldJavaUTF8Validation(field protoreflect.FieldDescriptor) (descriptorpb.FeatureSet_Utf8Validation, error) {
standardFeatureField, err := findFeatureField(featureNameUTF8Validation, protoreflect.EnumKind)
if err != nil {
return 0, err
}
val, err := protoutil.ResolveFeature(field, standardFeatureField)
if err != nil {
return 0, fmt.Errorf("unable to resolve value of %s feature: %w", standardFeatureField.Name(), err)
}
defaultValue := descriptorpb.FeatureSet_Utf8Validation(val.Enum())
opts, _ := field.ParentFile().Options().(*descriptorpb.FileOptions)
if field.ParentFile().Syntax() != protoreflect.Editions || (opts != nil && opts.JavaStringCheckUtf8 != nil) {
if opts.GetJavaStringCheckUtf8() {
return descriptorpb.FeatureSet_VERIFY, nil
}
return defaultValue, nil
}
val, err = customfeatures.ResolveJavaFeature(field, javaFeatureNameUTF8Validation, protoreflect.EnumKind)
if err != nil {
return 0, err
}
if protobuf.JavaFeatures_Utf8Validation(val.Enum()) == protobuf.JavaFeatures_VERIFY {
return descriptorpb.FeatureSet_VERIFY, nil
}
return defaultValue, nil
}
func fieldJavaUTF8ValidationLocation(field bufprotosource.Field) bufprotosource.Location {
ext := protobuf.E_Java.TypeDescriptor()
if ext.Message() == nil {
return nil
}
return getCustomFeatureLocation(field, ext, javaFeatureNameUTF8Validation)
}
func enumGoStripEnumPrefix(enum protoreflect.EnumDescriptor) (gofeaturespb.GoFeatures_StripEnumPrefix, error) {
val, err := customfeatures.ResolveGoFeatureForEnum(enum, goFeatureNameStripEnumPrefix, protoreflect.EnumKind)
if err != nil {
return 0, err
}
return gofeaturespb.GoFeatures_StripEnumPrefix(val.Enum()), nil
}
func enumGoStripEnumPrefixLocation(enum bufprotosource.Enum) bufprotosource.Location {
// For enums, we use the enum's features location
// This is similar to how other enum features are handled
return enum.Features().EnumTypeLocation()
}
func getCustomFeatureLocation(field bufprotosource.Field, extension protoreflect.ExtensionTypeDescriptor, fieldName protoreflect.Name) bufprotosource.Location {
if extension.Message() == nil {
return nil
}
feature := extension.Message().Fields().ByName(fieldName)
if feature == nil {
return nil
}
featureField := (*descriptorpb.FieldOptions)(nil).ProtoReflect().Descriptor().Fields().ByName(featuresFieldName)
if featureField == nil {
// should not be possible
return nil
}
return field.OptionLocation(featureField, int32(extension.Number()), int32(feature.Number()))
}
func fieldDescription(field bufprotosource.Field) string {
var name string
if field.Extendee() != "" {
// extensions are known by fully-qualified name
name = field.FullName()
} else {
name = field.Name()
}
return fieldDescriptionWithName(field, name)
}
func fieldDescriptionWithName(field bufprotosource.Field, name string) string {
if name != "" {
name = fmt.Sprintf(" with name %q", name)
}
// otherwise prints as hex
numberString := strconv.FormatInt(int64(field.Number()), 10)
var kind, message string
if field.Extendee() != "" {
kind = "Extension"
message = field.Extendee()
} else {
kind = "Field"
message = field.ParentMessage().Name()
}
return fmt.Sprintf("%s %q%s on message %q", kind, numberString, name, message)
}
func is64bitInteger(fieldType descriptorpb.FieldDescriptorProto_Type) bool {
switch fieldType {
case descriptorpb.FieldDescriptorProto_TYPE_INT64,
descriptorpb.FieldDescriptorProto_TYPE_SINT64,
descriptorpb.FieldDescriptorProto_TYPE_UINT64,
descriptorpb.FieldDescriptorProto_TYPE_FIXED64,
descriptorpb.FieldDescriptorProto_TYPE_SFIXED64:
return true
default:
return false
}
}