| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 1 | // Copyright 2020-2024 Buf Technologies, Inc. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // Package editions contains helpers related to resolving features for |
| 16 | // Protobuf editions. These are lower-level helpers. Higher-level helpers |
| 17 | // (which use this package under the hood) can be found in the exported |
| 18 | // protoutil package. |
| 19 | package editions |
| 20 | |
| 21 | import ( |
| 22 | "fmt" |
| 23 | "strings" |
| 24 | "sync" |
| 25 | |
| 26 | "google.golang.org/protobuf/encoding/prototext" |
| 27 | "google.golang.org/protobuf/proto" |
| 28 | "google.golang.org/protobuf/reflect/protoreflect" |
| 29 | "google.golang.org/protobuf/reflect/protoregistry" |
| 30 | "google.golang.org/protobuf/types/descriptorpb" |
| 31 | "google.golang.org/protobuf/types/dynamicpb" |
| 32 | ) |
| 33 | |
| 34 | const ( |
| 35 | // MinSupportedEdition is the earliest edition supported by this module. |
| 36 | // It should be 2023 (the first edition) for the indefinite future. |
| 37 | MinSupportedEdition = descriptorpb.Edition_EDITION_2023 |
| 38 | |
| 39 | // MaxSupportedEdition is the most recent edition supported by this module. |
| 40 | MaxSupportedEdition = descriptorpb.Edition_EDITION_2023 |
| 41 | ) |
| 42 | |
| 43 | var ( |
| 44 | // SupportedEditions is the exhaustive set of editions that protocompile |
| 45 | // can support. We don't allow it to compile future/unknown editions, to |
| 46 | // make sure we don't generate incorrect descriptors, in the event that |
| 47 | // a future edition introduces a change or new feature that requires |
| 48 | // new logic in the compiler. |
| 49 | SupportedEditions = computeSupportedEditions(MinSupportedEdition, MaxSupportedEdition) |
| 50 | |
| 51 | // FeatureSetDescriptor is the message descriptor for the compiled-in |
| 52 | // version (in the descriptorpb package) of the google.protobuf.FeatureSet |
| 53 | // message type. |
| 54 | FeatureSetDescriptor = (*descriptorpb.FeatureSet)(nil).ProtoReflect().Descriptor() |
| 55 | // FeatureSetType is the message type for the compiled-in version (in |
| 56 | // the descriptorpb package) of google.protobuf.FeatureSet. |
| 57 | FeatureSetType = (*descriptorpb.FeatureSet)(nil).ProtoReflect().Type() |
| 58 | |
| 59 | editionDefaults map[descriptorpb.Edition]*descriptorpb.FeatureSet |
| 60 | editionDefaultsInit sync.Once |
| 61 | ) |
| 62 | |
| 63 | // HasFeatures is implemented by all options messages and provides a |
| 64 | // nil-receiver-safe way of accessing the features explicitly configured |
| 65 | // in those options. |
| 66 | type HasFeatures interface { |
| 67 | GetFeatures() *descriptorpb.FeatureSet |
| 68 | } |
| 69 | |
| 70 | var _ HasFeatures = (*descriptorpb.FileOptions)(nil) |
| 71 | var _ HasFeatures = (*descriptorpb.MessageOptions)(nil) |
| 72 | var _ HasFeatures = (*descriptorpb.FieldOptions)(nil) |
| 73 | var _ HasFeatures = (*descriptorpb.OneofOptions)(nil) |
| 74 | var _ HasFeatures = (*descriptorpb.ExtensionRangeOptions)(nil) |
| 75 | var _ HasFeatures = (*descriptorpb.EnumOptions)(nil) |
| 76 | var _ HasFeatures = (*descriptorpb.EnumValueOptions)(nil) |
| 77 | var _ HasFeatures = (*descriptorpb.ServiceOptions)(nil) |
| 78 | var _ HasFeatures = (*descriptorpb.MethodOptions)(nil) |
| 79 | |
| 80 | // ResolveFeature resolves a feature for the given descriptor. This simple |
| 81 | // helper examines the given element and its ancestors, searching for an |
| 82 | // override. If there is no overridden value, it returns a zero value. |
| 83 | func ResolveFeature( |
| 84 | element protoreflect.Descriptor, |
| 85 | fields ...protoreflect.FieldDescriptor, |
| 86 | ) (protoreflect.Value, error) { |
| 87 | for { |
| 88 | var features *descriptorpb.FeatureSet |
| 89 | if withFeatures, ok := element.Options().(HasFeatures); ok { |
| 90 | // It should not really be possible for 'ok' to ever be false... |
| 91 | features = withFeatures.GetFeatures() |
| 92 | } |
| 93 | |
| 94 | // TODO: adaptFeatureSet is only looking at the first field. But if we needed to |
| 95 | // support an extension field inside a custom feature, we'd really need |
| 96 | // to check all fields. That gets particularly complicated if the traversal |
| 97 | // path of fields includes list and map values. Luckily, features are not |
| 98 | // supposed to be repeated and not supposed to themselves have extensions. |
| 99 | // So this should be fine, at least for now. |
| 100 | msgRef, err := adaptFeatureSet(features, fields[0]) |
| 101 | if err != nil { |
| 102 | return protoreflect.Value{}, err |
| 103 | } |
| 104 | // Navigate the fields to find the value |
| 105 | var val protoreflect.Value |
| 106 | for i, field := range fields { |
| 107 | if i > 0 { |
| 108 | msgRef = val.Message() |
| 109 | } |
| 110 | if !msgRef.Has(field) { |
| 111 | val = protoreflect.Value{} |
| 112 | break |
| 113 | } |
| 114 | val = msgRef.Get(field) |
| 115 | } |
| 116 | if val.IsValid() { |
| 117 | // All fields were set! |
| 118 | return val, nil |
| 119 | } |
| 120 | |
| 121 | parent := element.Parent() |
| 122 | if parent == nil { |
| 123 | // We've reached the end of the inheritance chain. |
| 124 | return protoreflect.Value{}, nil |
| 125 | } |
| 126 | element = parent |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // HasEdition should be implemented by values that implement |
| 131 | // [protoreflect.FileDescriptor], to provide access to the file's |
| 132 | // edition when its syntax is [protoreflect.Editions]. |
| 133 | type HasEdition interface { |
| 134 | // Edition returns the numeric value of a google.protobuf.Edition enum |
| 135 | // value that corresponds to the edition of this file. If the file does |
| 136 | // not use editions, it should return the enum value that corresponds |
| 137 | // to the syntax level, EDITION_PROTO2 or EDITION_PROTO3. |
| 138 | Edition() int32 |
| 139 | } |
| 140 | |
| 141 | // GetEdition returns the edition for a given element. It returns |
| 142 | // EDITION_PROTO2 or EDITION_PROTO3 if the element is in a file that |
| 143 | // uses proto2 or proto3 syntax, respectively. It returns EDITION_UNKNOWN |
| 144 | // if the syntax of the given element is not recognized or if the edition |
| 145 | // cannot be ascertained from the element's [protoreflect.FileDescriptor]. |
| 146 | func GetEdition(d protoreflect.Descriptor) descriptorpb.Edition { |
| 147 | switch d.ParentFile().Syntax() { |
| 148 | case protoreflect.Proto2: |
| 149 | return descriptorpb.Edition_EDITION_PROTO2 |
| 150 | case protoreflect.Proto3: |
| 151 | return descriptorpb.Edition_EDITION_PROTO3 |
| 152 | case protoreflect.Editions: |
| 153 | withEdition, ok := d.ParentFile().(HasEdition) |
| 154 | if !ok { |
| 155 | // The parent file should always be a *result, so we should |
| 156 | // never be able to actually get in here. If we somehow did |
| 157 | // have another implementation of protoreflect.FileDescriptor, |
| 158 | // it doesn't provide a way to get the edition, other than the |
| 159 | // potentially expensive step of generating a FileDescriptorProto |
| 160 | // and then querying for the edition from that. :/ |
| 161 | return descriptorpb.Edition_EDITION_UNKNOWN |
| 162 | } |
| 163 | return descriptorpb.Edition(withEdition.Edition()) |
| 164 | default: |
| 165 | return descriptorpb.Edition_EDITION_UNKNOWN |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // GetEditionDefaults returns the default feature values for the given edition. |
| 170 | // It returns nil if the given edition is not known. |
| 171 | // |
| 172 | // This only populates known features, those that are fields of [*descriptorpb.FeatureSet]. |
| 173 | // It does not populate any extension fields. |
| 174 | // |
| 175 | // The returned value must not be mutated as it references shared package state. |
| 176 | func GetEditionDefaults(edition descriptorpb.Edition) *descriptorpb.FeatureSet { |
| 177 | editionDefaultsInit.Do(func() { |
| 178 | editionDefaults = make(map[descriptorpb.Edition]*descriptorpb.FeatureSet, len(descriptorpb.Edition_name)) |
| 179 | // Compute default for all known editions in descriptorpb. |
| 180 | for editionInt := range descriptorpb.Edition_name { |
| 181 | edition := descriptorpb.Edition(editionInt) |
| 182 | defaults := &descriptorpb.FeatureSet{} |
| 183 | defaultsRef := defaults.ProtoReflect() |
| 184 | fields := defaultsRef.Descriptor().Fields() |
| 185 | // Note: we are not computing defaults for extensions. Those are not needed |
| 186 | // by anything in the compiler, so we can get away with just computing |
| 187 | // defaults for these static, non-extension fields. |
| 188 | for i, length := 0, fields.Len(); i < length; i++ { |
| 189 | field := fields.Get(i) |
| 190 | val, err := GetFeatureDefault(edition, FeatureSetType, field) |
| 191 | if err != nil { |
| 192 | // should we fail somehow?? |
| 193 | continue |
| 194 | } |
| 195 | defaultsRef.Set(field, val) |
| 196 | } |
| 197 | editionDefaults[edition] = defaults |
| 198 | } |
| 199 | }) |
| 200 | return editionDefaults[edition] |
| 201 | } |
| 202 | |
| 203 | // GetFeatureDefault computes the default value for a feature. The given container |
| 204 | // is the message type that contains the field. This should usually be the descriptor |
| 205 | // for google.protobuf.FeatureSet, but can be a different message for computing the |
| 206 | // default value of custom features. |
| 207 | // |
| 208 | // Note that this always re-computes the default. For known fields of FeatureSet, |
| 209 | // it is more efficient to query from the statically computed default messages, |
| 210 | // like so: |
| 211 | // |
| 212 | // editions.GetEditionDefaults(edition).ProtoReflect().Get(feature) |
| 213 | func GetFeatureDefault(edition descriptorpb.Edition, container protoreflect.MessageType, feature protoreflect.FieldDescriptor) (protoreflect.Value, error) { |
| 214 | opts, ok := feature.Options().(*descriptorpb.FieldOptions) |
| 215 | if !ok { |
| 216 | // this is most likely impossible except for contrived use cases... |
| 217 | return protoreflect.Value{}, fmt.Errorf("options is %T instead of *descriptorpb.FieldOptions", feature.Options()) |
| 218 | } |
| 219 | maxEdition := descriptorpb.Edition(-1) |
| 220 | var maxVal string |
| 221 | for _, def := range opts.EditionDefaults { |
| 222 | if def.GetEdition() <= edition && def.GetEdition() > maxEdition { |
| 223 | maxEdition = def.GetEdition() |
| 224 | maxVal = def.GetValue() |
| 225 | } |
| 226 | } |
| 227 | if maxEdition == -1 { |
| 228 | // no matching default found |
| 229 | return protoreflect.Value{}, fmt.Errorf("no relevant default for edition %s", edition) |
| 230 | } |
| 231 | // We use a typed nil so that it won't fall back to the global registry. Features |
| 232 | // should not use extensions or google.protobuf.Any, so a nil *Types is fine. |
| 233 | unmarshaler := prototext.UnmarshalOptions{Resolver: (*protoregistry.Types)(nil)} |
| 234 | // The string value is in the text format: either a field value literal or a |
| 235 | // message literal. (Repeated and map features aren't supported, so there's no |
| 236 | // array or map literal syntax to worry about.) |
| 237 | if feature.Kind() == protoreflect.MessageKind || feature.Kind() == protoreflect.GroupKind { |
| 238 | fldVal := container.Zero().NewField(feature) |
| 239 | err := unmarshaler.Unmarshal([]byte(maxVal), fldVal.Message().Interface()) |
| 240 | if err != nil { |
| 241 | return protoreflect.Value{}, err |
| 242 | } |
| 243 | return fldVal, nil |
| 244 | } |
| 245 | // The value is the textformat for the field. But prototext doesn't provide a way |
| 246 | // to unmarshal a single field value. To work around, we unmarshal into an enclosing |
| 247 | // message, which means we must prefix the value with the field name. |
| 248 | if feature.IsExtension() { |
| 249 | maxVal = fmt.Sprintf("[%s]: %s", feature.FullName(), maxVal) |
| 250 | } else { |
| 251 | maxVal = fmt.Sprintf("%s: %s", feature.Name(), maxVal) |
| 252 | } |
| 253 | empty := container.New() |
| 254 | err := unmarshaler.Unmarshal([]byte(maxVal), empty.Interface()) |
| 255 | if err != nil { |
| 256 | return protoreflect.Value{}, err |
| 257 | } |
| 258 | return empty.Get(feature), nil |
| 259 | } |
| 260 | |
| 261 | func adaptFeatureSet(msg *descriptorpb.FeatureSet, field protoreflect.FieldDescriptor) (protoreflect.Message, error) { |
| 262 | msgRef := msg.ProtoReflect() |
| 263 | var actualField protoreflect.FieldDescriptor |
| 264 | switch { |
| 265 | case field.IsExtension(): |
| 266 | // Extensions can be used directly with the feature set, even if |
| 267 | // field.ContainingMessage() != FeatureSetDescriptor. But only if |
| 268 | // the value is either not a message or is a message with the |
| 269 | // right descriptor, i.e. val.Descriptor() == field.Message(). |
| 270 | if actualField = actualDescriptor(msgRef, field); actualField == nil || actualField == field { |
| 271 | if msgRef.Has(field) || len(msgRef.GetUnknown()) == 0 { |
| 272 | return msgRef, nil |
| 273 | } |
| 274 | // The field is not present, but the message has unrecognized values. So |
| 275 | // let's try to parse the unrecognized bytes, just in case they contain |
| 276 | // this extension. |
| 277 | temp := &descriptorpb.FeatureSet{} |
| 278 | unmarshaler := proto.UnmarshalOptions{ |
| 279 | AllowPartial: true, |
| 280 | Resolver: resolverForExtension{field}, |
| 281 | } |
| 282 | if err := unmarshaler.Unmarshal(msgRef.GetUnknown(), temp); err != nil { |
| 283 | return nil, fmt.Errorf("failed to parse unrecognized fields of FeatureSet: %w", err) |
| 284 | } |
| 285 | return temp.ProtoReflect(), nil |
| 286 | } |
| 287 | case field.ContainingMessage() == FeatureSetDescriptor: |
| 288 | // Known field, not dynamically generated. Can directly use with the feature set. |
| 289 | return msgRef, nil |
| 290 | default: |
| 291 | actualField = FeatureSetDescriptor.Fields().ByNumber(field.Number()) |
| 292 | } |
| 293 | |
| 294 | // If we get here, we have a dynamic field descriptor or an extension |
| 295 | // descriptor whose message type does not match the descriptor of the |
| 296 | // stored value. We need to copy its value into a dynamic message, |
| 297 | // which requires marshalling/unmarshalling. |
| 298 | // We only need to copy over the unrecognized bytes (if any) |
| 299 | // and the same field (if present). |
| 300 | data := msgRef.GetUnknown() |
| 301 | if actualField != nil && msgRef.Has(actualField) { |
| 302 | subset := &descriptorpb.FeatureSet{} |
| 303 | subset.ProtoReflect().Set(actualField, msgRef.Get(actualField)) |
| 304 | var err error |
| 305 | data, err = proto.MarshalOptions{AllowPartial: true}.MarshalAppend(data, subset) |
| 306 | if err != nil { |
| 307 | return nil, fmt.Errorf("failed to marshal FeatureSet field %s to bytes: %w", field.Name(), err) |
| 308 | } |
| 309 | } |
| 310 | if len(data) == 0 { |
| 311 | // No relevant data to copy over, so we can just return |
| 312 | // a zero value message |
| 313 | return dynamicpb.NewMessageType(field.ContainingMessage()).Zero(), nil |
| 314 | } |
| 315 | |
| 316 | other := dynamicpb.NewMessage(field.ContainingMessage()) |
| 317 | // We don't need to use a resolver for this step because we know that |
| 318 | // field is not an extension. And features are not allowed to themselves |
| 319 | // have extensions. |
| 320 | if err := (proto.UnmarshalOptions{AllowPartial: true}).Unmarshal(data, other); err != nil { |
| 321 | return nil, fmt.Errorf("failed to marshal FeatureSet field %s to bytes: %w", field.Name(), err) |
| 322 | } |
| 323 | return other, nil |
| 324 | } |
| 325 | |
| 326 | type resolverForExtension struct { |
| 327 | ext protoreflect.ExtensionDescriptor |
| 328 | } |
| 329 | |
| 330 | func (r resolverForExtension) FindMessageByName(_ protoreflect.FullName) (protoreflect.MessageType, error) { |
| 331 | return nil, protoregistry.NotFound |
| 332 | } |
| 333 | |
| 334 | func (r resolverForExtension) FindMessageByURL(_ string) (protoreflect.MessageType, error) { |
| 335 | return nil, protoregistry.NotFound |
| 336 | } |
| 337 | |
| 338 | func (r resolverForExtension) FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error) { |
| 339 | if field == r.ext.FullName() { |
| 340 | return asExtensionType(r.ext), nil |
| 341 | } |
| 342 | return nil, protoregistry.NotFound |
| 343 | } |
| 344 | |
| 345 | func (r resolverForExtension) FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error) { |
| 346 | if message == r.ext.ContainingMessage().FullName() && field == r.ext.Number() { |
| 347 | return asExtensionType(r.ext), nil |
| 348 | } |
| 349 | return nil, protoregistry.NotFound |
| 350 | } |
| 351 | |
| 352 | func asExtensionType(ext protoreflect.ExtensionDescriptor) protoreflect.ExtensionType { |
| 353 | if xtd, ok := ext.(protoreflect.ExtensionTypeDescriptor); ok { |
| 354 | return xtd.Type() |
| 355 | } |
| 356 | return dynamicpb.NewExtensionType(ext) |
| 357 | } |
| 358 | |
| 359 | func computeSupportedEditions(minEdition, maxEdition descriptorpb.Edition) map[string]descriptorpb.Edition { |
| 360 | supportedEditions := map[string]descriptorpb.Edition{} |
| 361 | for editionNum := range descriptorpb.Edition_name { |
| 362 | edition := descriptorpb.Edition(editionNum) |
| 363 | if edition >= minEdition && edition <= maxEdition { |
| 364 | name := strings.TrimPrefix(edition.String(), "EDITION_") |
| 365 | supportedEditions[name] = edition |
| 366 | } |
| 367 | } |
| 368 | return supportedEditions |
| 369 | } |
| 370 | |
| 371 | // actualDescriptor returns the actual field descriptor referenced by msg that |
| 372 | // corresponds to the given ext (i.e. same number). It returns nil if msg has |
| 373 | // no reference, if the actual descriptor is the same as ext, or if ext is |
| 374 | // otherwise safe to use as is. |
| 375 | func actualDescriptor(msg protoreflect.Message, ext protoreflect.ExtensionDescriptor) protoreflect.FieldDescriptor { |
| 376 | if !msg.Has(ext) || ext.Message() == nil { |
| 377 | // nothing to match; safe as is |
| 378 | return nil |
| 379 | } |
| 380 | val := msg.Get(ext) |
| 381 | switch { |
| 382 | case ext.IsMap(): // should not actually be possible |
| 383 | expectedDescriptor := ext.MapValue().Message() |
| 384 | if expectedDescriptor == nil { |
| 385 | return nil // nothing to match |
| 386 | } |
| 387 | // We know msg.Has(field) is true, from above, so there's at least one entry. |
| 388 | var matches bool |
| 389 | val.Map().Range(func(_ protoreflect.MapKey, val protoreflect.Value) bool { |
| 390 | matches = val.Message().Descriptor() == expectedDescriptor |
| 391 | return false |
| 392 | }) |
| 393 | if matches { |
| 394 | return nil |
| 395 | } |
| 396 | case ext.IsList(): |
| 397 | // We know msg.Has(field) is true, from above, so there's at least one entry. |
| 398 | if val.List().Get(0).Message().Descriptor() == ext.Message() { |
| 399 | return nil |
| 400 | } |
| 401 | case !ext.IsMap(): |
| 402 | if val.Message().Descriptor() == ext.Message() { |
| 403 | return nil |
| 404 | } |
| 405 | } |
| 406 | // The underlying message descriptors do not match. So we need to return |
| 407 | // the actual field descriptor. Sadly, protoreflect.Message provides no way |
| 408 | // to query the field descriptor in a message by number. For non-extensions, |
| 409 | // one can query the associated message descriptor. But for extensions, we |
| 410 | // have to do the slow thing, and range through all fields looking for it. |
| 411 | var actualField protoreflect.FieldDescriptor |
| 412 | msg.Range(func(fd protoreflect.FieldDescriptor, _ protoreflect.Value) bool { |
| 413 | if fd.Number() == ext.Number() { |
| 414 | actualField = fd |
| 415 | return false |
| 416 | } |
| 417 | return true |
| 418 | }) |
| 419 | return actualField |
| 420 | } |