protoutil

package
v0.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2024 License: Apache-2.0 Imports: 7 Imported by: 12

Documentation

Overview

Package protoutil contains useful functions for interacting with descriptors. For now these include only functions for efficiently converting descriptors produced by the compiler to descriptor protos and functions for resolving "features" (a core concept of Protobuf Editions).

Despite the fact that descriptor protos are mutable, calling code should NOT mutate any of the protos returned from this package. For efficiency, some values returned from this package may reference internal state of a compiler result, and mutating the proto could corrupt or invalidate parts of that result.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetCustomFeatureDefault added in v0.11.0

func GetCustomFeatureDefault(edition descriptorpb.Edition, extension protoreflect.ExtensionType, feature protoreflect.FieldDescriptor) (protoreflect.Value, error)

GetCustomFeatureDefault gets the default value for the given custom feature and given edition. A custom feature is a field whose containing message is the type of an extension field of google.protobuf.FeatureSet. The given extension describes that extension field and message type. The given feature must be a field of that extension's message type.

func GetFeatureDefault added in v0.11.0

func GetFeatureDefault(edition descriptorpb.Edition, feature protoreflect.FieldDescriptor) (protoreflect.Value, error)

GetFeatureDefault gets the default value for the given feature and the given edition. The given feature must represent a field of the google.protobuf.FeatureSet message and must not be an extension.

If the given field is from a dynamically built descriptor (i.e. it's containing message descriptor is different from the linked-in descriptor for *descriptorpb.FeatureSet), the returned value may be a dynamic value. In such cases, the value may not be directly usable using protoreflect.Message.Set with an instance of *descriptorpb.FeatureSet and must instead be used with a *dynamicpb.Message.

To get the default value of a custom feature, use GetCustomFeatureDefault instead.

func ProtoFromDescriptor

func ProtoFromDescriptor(d protoreflect.Descriptor) proto.Message

ProtoFromDescriptor extracts a descriptor proto from the given "rich" descriptor. For descriptors generated by the compiler, this is an inexpensive and non-lossy operation. Descriptors from other sources however may be expensive (to re-create a proto) and even lossy.

func ProtoFromEnumDescriptor

func ProtoFromEnumDescriptor(d protoreflect.EnumDescriptor) *descriptorpb.EnumDescriptorProto

ProtoFromEnumDescriptor extracts a descriptor proto from the given "rich" descriptor. For enum descriptors generated by the compiler, this is an inexpensive and non-lossy operation. Enum descriptors from other sources however may be expensive (to re-create a proto) and even lossy.

func ProtoFromEnumValueDescriptor

ProtoFromEnumValueDescriptor extracts a descriptor proto from the given "rich" descriptor. For enum value descriptors generated by the compiler, this is an inexpensive and non-lossy operation. Enum value descriptors from other sources however may be expensive (to re-create a proto) and even lossy.

func ProtoFromFieldDescriptor

ProtoFromFieldDescriptor extracts a descriptor proto from the given "rich" descriptor. For field descriptors generated by the compiler, this is an inexpensive and non-lossy operation. Field descriptors from other sources however may be expensive (to re-create a proto) and even lossy.

func ProtoFromFileDescriptor

func ProtoFromFileDescriptor(d protoreflect.FileDescriptor) *descriptorpb.FileDescriptorProto

ProtoFromFileDescriptor extracts a descriptor proto from the given "rich" descriptor. For file descriptors generated by the compiler, this is an inexpensive and non-lossy operation. File descriptors from other sources however may be expensive (to re-create a proto) and even lossy.

func ProtoFromMessageDescriptor

func ProtoFromMessageDescriptor(d protoreflect.MessageDescriptor) *descriptorpb.DescriptorProto

ProtoFromMessageDescriptor extracts a descriptor proto from the given "rich" descriptor. For message descriptors generated by the compiler, this is an inexpensive and non-lossy operation. Message descriptors from other sources however may be expensive (to re-create a proto) and even lossy.

func ProtoFromMethodDescriptor

ProtoFromMethodDescriptor extracts a descriptor proto from the given "rich" descriptor. For method descriptors generated by the compiler, this is an inexpensive and non-lossy operation. Method descriptors from other sources however may be expensive (to re-create a proto) and even lossy.

func ProtoFromOneofDescriptor

ProtoFromOneofDescriptor extracts a descriptor proto from the given "rich" descriptor. For oneof descriptors generated by the compiler, this is an inexpensive and non-lossy operation. Oneof descriptors from other sources however may be expensive (to re-create a proto) and even lossy.

func ProtoFromServiceDescriptor

ProtoFromServiceDescriptor extracts a descriptor proto from the given "rich" descriptor. For service descriptors generated by the compiler, this is an inexpensive and non-lossy operation. Service descriptors from other sources however may be expensive (to re-create a proto) and even lossy.

func ResolveCustomFeature added in v0.11.0

func ResolveCustomFeature(element protoreflect.Descriptor, extension protoreflect.ExtensionType, feature protoreflect.FieldDescriptor) (protoreflect.Value, error)

ResolveCustomFeature resolves a custom feature for the given extension and field descriptor.

The given extension must be an extension of google.protobuf.FeatureSet that represents a non-repeated message value. The given feature is a field in that extension's message type.

If the given element is in a proto2 or proto3 syntax file, this skips resolution and just returns the relevant default (since such files are not allowed to override features). If neither the given element nor any of its ancestors override the given feature, the relevant default is returned.

func ResolveFeature added in v0.11.0

func ResolveFeature(element protoreflect.Descriptor, feature protoreflect.FieldDescriptor) (protoreflect.Value, error)

ResolveFeature resolves a feature for the given descriptor.

If the given element is in a proto2 or proto3 syntax file, this skips resolution and just returns the relevant default (since such files are not allowed to override features). If neither the given element nor any of its ancestors override the given feature, the relevant default is returned.

This has the same caveat as GetFeatureDefault if the given feature is from a dynamically built descriptor.

Types

type DescriptorProtoWrapper

type DescriptorProtoWrapper interface {
	protoreflect.Descriptor
	// AsProto returns the underlying descriptor proto. The concrete
	// type of the proto message depends on the type of this
	// descriptor:
	//    Descriptor Type        Proto Message Type
	//   ---------------------+------------------------------------
	//    FileDescriptor      |  *descriptorpb.FileDescriptorProto
	//    MessageDescriptor   |  *descriptorpb.DescriptorProto
	//    FieldDescriptor     |  *descriptorpb.FieldDescriptorProto
	//    OneofDescriptor     |  *descriptorpb.OneofDescriptorProto
	//    EnumDescriptor      |  *descriptorpb.EnumDescriptorProto
	//    EnumValueDescriptor |  *descriptorpb.EnumValueDescriptorProto
	//    ServiceDescriptor   |  *descriptorpb.ServiceDescriptorProto
	//    MethodDescriptor    |  *descriptorpb.MethodDescriptorProto
	AsProto() proto.Message
}

DescriptorProtoWrapper is a protoreflect.Descriptor that wraps an underlying descriptor proto. It provides the same interface as Descriptor but with one extra operation, to efficiently query for the underlying descriptor proto.

Descriptors that implement this will also implement another method whose specified return type is the concrete type returned by the AsProto method. The name of this method varies by the type of this descriptor:

 Descriptor Type        Other Method Name
---------------------+------------------------------------
 FileDescriptor      |  FileDescriptorProto()
 MessageDescriptor   |  MessageDescriptorProto()
 FieldDescriptor     |  FieldDescriptorProto()
 OneofDescriptor     |  OneofDescriptorProto()
 EnumDescriptor      |  EnumDescriptorProto()
 EnumValueDescriptor |  EnumValueDescriptorProto()
 ServiceDescriptor   |  ServiceDescriptorProto()
 MethodDescriptor    |  MethodDescriptorProto()

For example, a DescriptorProtoWrapper that implements FileDescriptor returns a *descriptorpb.FileDescriptorProto value from its AsProto method and also provides a method with the following signature:

FileDescriptorProto() *descriptorpb.FileDescriptorProto

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL