Documentation ¶
Overview ¶
Package bufprotosource defines minimal interfaces for Protobuf descriptor types.
This is done so that the backing package can be swapped out easily.
All values that return SourceLocation can be nil.
Testing is currently implicitly done through the bufcheck packages, however if this were to be split out into a separate library, it would need a separate testing suite.
Index ¶
- Constants
- func DirPathToFiles(files ...File) (map[string][]File, error)
- func EnumIsSubset(supersetEnum Enum, subsetEnum Enum) (bool, error)
- func FilePathToFile(files ...File) (map[string]File, error)
- func ForEachEnum(f func(Enum) error, containerDescriptor ContainerDescriptor) error
- func ForEachExtension(f func(Field) error, containerDescriptor ContainerDescriptor) error
- func ForEachMessage(f func(Message) error, containerDescriptor ContainerDescriptor) error
- func FullNameToEnum(files ...File) (map[string]Enum, error)
- func FullNameToMessage(files ...File) (map[string]Message, error)
- func FullNameToMethod(files ...File) (map[string]Method, error)
- func FullNameToService(files ...File) (map[string]Service, error)
- func NameInReservedNames(name string, reservedNames ...ReservedName) bool
- func NameToEnumValue(enum Enum) (map[string]EnumValue, error)
- func NameToMessageOneof(message Message) (map[string]Oneof, error)
- func NameToMethod(service Service) (map[string]Method, error)
- func NameToService(file File) (map[string]Service, error)
- func NestedNameToEnum(containerDescriptor ContainerDescriptor) (map[string]Enum, error)
- func NestedNameToExtension(containerDescriptor ContainerDescriptor) (map[string]Field, error)
- func NestedNameToMessage(containerDescriptor ContainerDescriptor) (map[string]Message, error)
- func NumberInReservedRanges(number int, reservedRanges ...TagRange) bool
- func NumberToMessageField(message Message) (map[int]Field, error)
- func NumberToMessageFieldForLabel(message Message, label descriptorpb.FieldDescriptorProto_Label) (map[int]Field, error)
- func NumberToNameToEnumValue(enum Enum) (map[int]map[string]EnumValue, error)
- func PackageToDirectlyImportedPackageToFileImports(files ...File) (map[string]map[string][]FileImport, error)
- func PackageToFiles(files ...File) (map[string][]File, error)
- func PackageToNameToService(files ...File) (map[string]map[string]Service, error)
- func PackageToNestedNameToEnum(files ...File) (map[string]map[string]Enum, error)
- func PackageToNestedNameToExtension(files ...File) (map[string]map[string]Field, error)
- func PackageToNestedNameToMessage(files ...File) (map[string]map[string]Message, error)
- func SortFiles(files []File)
- func StringToExtensionMessageRange(message Message) map[string]MessageRange
- func StringToReservedTagRange(reservedDescriptor ReservedDescriptor) map[string]TagRange
- func TagRangeString(tagRange TagRange) string
- func ValueToReservedName(reservedDescriptor ReservedDescriptor) map[string]ReservedName
- type ContainerDescriptor
- type Descriptor
- type Enum
- type EnumRange
- type EnumValue
- type ExtensionRange
- type FeaturesDescriptor
- type Field
- type File
- type FileImport
- type FileInfo
- type Location
- type LocationDescriptor
- type Message
- type MessageRange
- type Method
- type ModuleFullName
- type NamedDescriptor
- type Oneof
- type OptionExtensionDescriptor
- type ReservedDescriptor
- type ReservedName
- type Service
- type Syntax
- type TagRange
Constants ¶
const ( // MessageRangeInclusiveMax is the maximum allowed tag for a message field. MessageRangeInclusiveMax = 536870911 // 2^29 - 1 // MessageSetRangeInclusiveMax is the maximum allowed tag for a message // field for a message that uses the message-set wire format. MessageSetRangeInclusiveMax = math.MaxInt32 - 1 )
const ( // EnumRangeInclusiveMax is the maximum allowed number for an enum value. EnumRangeInclusiveMax = math.MaxInt32 )
Variables ¶
This section is empty.
Functions ¶
func DirPathToFiles ¶
DirPathToFiles maps the Files to a map from directory to the slice of Files in that directory.
Returns error if file paths are not unique. Directories are normalized.
Files will be sorted by FilePath.
func EnumIsSubset ¶
EnumIsSubset checks if subsetEnum is a subset of supersetEnum.
func FilePathToFile ¶
FilePathToFile maps the Files to a map from Path() to File.
Returns error if file paths are not unique.
func ForEachEnum ¶
func ForEachEnum(f func(Enum) error, containerDescriptor ContainerDescriptor) error
ForEachEnum calls f on each Enum in the given ContainerDescriptor, including nested Enums.
Returns error and stops iterating if f returns error. Never returns error unless f returns error.
func ForEachExtension ¶
func ForEachExtension(f func(Field) error, containerDescriptor ContainerDescriptor) error
ForEachExtension calls f on each extension Field in the given ContainerDescriptor, including nested extensions.
Returns error and stops iterating if f returns error. Never returns error unless f returns error.
func ForEachMessage ¶
func ForEachMessage(f func(Message) error, containerDescriptor ContainerDescriptor) error
ForEachMessage calls f on each Message in the given ContainerDescriptor, including nested Messages.
Returns error and stops iterating if f returns error Never returns error unless f returns error.
func FullNameToEnum ¶
FullNameToEnum maps the Enums in the Files to a map from full name to enum.
Returns error if the Enums do not have unique full names within the Files, which should generally never happen for properly-formed Files.
func FullNameToMessage ¶
FullNameToMessage maps the Messages in the Files to a map from full name to message.
Returns error if the Messages do not have unique full names within the Files, which should generally never happen for properly-formed Files.
func FullNameToMethod ¶
FullNameToMethod maps the Methods in the Files to a map from full name to Method.
Returns error if Methods do not have unique full names within the Files, which should generally never happen for properly-formed Files.
func FullNameToService ¶
FullNameToService maps the Services in the Files to a map from full name to Service.
Returns error if Services do not have unique full names within the Files, which should generally never happen for properly-formed Files.
func NameInReservedNames ¶
func NameInReservedNames(name string, reservedNames ...ReservedName) bool
NameInReservedNames returns true if the name is in one of the ReservedNames.
func NameToEnumValue ¶
NameToEnumValue maps the EnumValues in the Enum to a map from name to EnumValue.
Returns error if the EnumValues do not have unique names within the Enum, which should generally never happen for properly-formed Enums.
func NameToMessageOneof ¶
NameToMessageOneof maps the Oneofs in the Message to a map from name to Oneof.
Returns error if the Oneofs do not have unique names within the Message, which should generally never happen for properly-formed Messages.
func NameToMethod ¶
NameToMethod maps the Methods in the Service to a map from name to Method.
Returns error if Methods do not have unique names within the Service, which should generally never happen for properly-formed Services.
func NameToService ¶
NameToService maps the Services in the File to a map from name to Service.
Returns error if Services do not have unique names within the File, which should generally never happen for properly-formed Files.
func NestedNameToEnum ¶
func NestedNameToEnum(containerDescriptor ContainerDescriptor) (map[string]Enum, error)
NestedNameToEnum maps the Enums in the ContainerDescriptor to a map from nested name to Enum.
Returns error if Enums do not have unique nested names within the ContainerDescriptor, which should generally never happen for properly-formed ContainerDescriptors.
func NestedNameToExtension ¶
func NestedNameToExtension(containerDescriptor ContainerDescriptor) (map[string]Field, error)
NestedNameToExtension maps the Enums in the ContainerDescriptor to a map from nested name to extension Field.
Returns error if extensions do not have unique nested names within the ContainerDescriptor, which should generally never happen for properly-formed ContainerDescriptors.
func NestedNameToMessage ¶
func NestedNameToMessage(containerDescriptor ContainerDescriptor) (map[string]Message, error)
NestedNameToMessage maps the Messages in the ContainerDescriptor to a map from nested name to Message.
Returns error if Messages do not have unique nested names within the ContainerDescriptor, which should generally never happen for properly-formed files.
func NumberInReservedRanges ¶
NumberInReservedRanges returns true if the number is in one of the Ranges.
func NumberToMessageField ¶
NumberToMessageField maps the Fields in the Message to a map from number to Field.
Does not includes extensions.
Returns error if the Fields do not have unique numbers within the Message, which should generally never happen for properly-formed Messages.
func NumberToMessageFieldForLabel ¶
func NumberToMessageFieldForLabel(message Message, label descriptorpb.FieldDescriptorProto_Label) (map[int]Field, error)
NumberToMessageFieldForLabel maps the Fields with the given label in the message to a map from number to Field.
Does not includes extensions.
Returns error if the Fields do not have unique numbers within the Message, which should generally never happen for properly-formed Messages.
func NumberToNameToEnumValue ¶
NumberToNameToEnumValue maps the EnumValues in the Enum to a map from number to name to EnumValue.
Duplicates by number may occur if allow_alias = true.
Returns error if the EnumValues do not have unique names within the Enum for a given number, which should generally never happen for properly-formed Enums.
func PackageToDirectlyImportedPackageToFileImports ¶
func PackageToDirectlyImportedPackageToFileImports(files ...File) (map[string]map[string][]FileImport, error)
PackageToDirectlyImportedPackageToFileImports maps packages to directly imported packages to the FileImports that import this package.
For example, if package a imports package b via c/d.proto and c/e.proto, this will have a -> b -> [c/d.proto, c/e.proto].
A directly imported package will not be equal to the package, i.e. there will be no a -> a.
Files with no packages are included with key "" to be consistent with other functions.
func PackageToFiles ¶
PackageToFiles maps the Files to a map from Protobuf package to the slice of Files in that package.
Returns error if file paths are not unique.
Files will be sorted by Path.
func PackageToNameToService ¶
PackageToNameToService maps the Services in the Files to a map from package to name to Service.
Returns error if the Services do not have unique names within the packages, which should generally never happen for properly-formed Files.
func PackageToNestedNameToEnum ¶
PackageToNestedNameToEnum maps the Enums in the Files to a map from package to nested name to Enum.
Returns error if the Enums do not have unique nested names within the packages, which should generally never happen for properly-formed Files.
func PackageToNestedNameToExtension ¶
PackageToNestedNameToExtension maps the extension Fields in the Files to a map from package to nested name to Field.
Returns error if the extension do not have unique nested names within the packages, which should generally never happen for properly-formed Files.
func PackageToNestedNameToMessage ¶
PackageToNestedNameToMessage maps the Messages in the Files to a map from package to nested name to Message.
Returns error if the Messages do not have unique nested names within the packages, which should generally never happen for properly-formed Files.
func StringToExtensionMessageRange ¶
func StringToExtensionMessageRange(message Message) map[string]MessageRange
StringToExtensionMessageRange maps the ExtensionMessageRanges in the Message to a map from string string to ExtensionMessageRange.
Ignores duplicates.
func StringToReservedTagRange ¶
func StringToReservedTagRange(reservedDescriptor ReservedDescriptor) map[string]TagRange
StringToReservedTagRange maps the ReservedTagRanges in the ReservedDescriptor to a map from string string to reserved TagRange.
Ignores duplicates.
func TagRangeString ¶
TagRangeString returns the string representation of the range.
func ValueToReservedName ¶
func ValueToReservedName(reservedDescriptor ReservedDescriptor) map[string]ReservedName
ValueToReservedName maps the ReservedNames in the ReservedDescriptor to a map from string value to ReservedName.
Ignores duplicates.
Types ¶
type ContainerDescriptor ¶
ContainerDescriptor contains Enums and Messages.
type Descriptor ¶
type Descriptor interface { // File returns the associated File. // // Always non-nil. File() File }
Descriptor is the base interface for a descriptor type.
type Enum ¶
type Enum interface { NamedDescriptor ReservedDescriptor OptionExtensionDescriptor Values() []EnumValue ReservedEnumRanges() []EnumRange AllowAlias() bool DeprecatedLegacyJSONFieldConflicts() bool Deprecated() bool AllowAliasLocation() Location // Will return nil if this is a top-level Enum Parent() Message // AsDescriptor returns a [protoreflect.Descriptor] that // corresponds to this message. This should only be needed // for reflection usages. AsDescriptor() (protoreflect.EnumDescriptor, error) }
Enum is an enum descriptor.
type EnumValue ¶
type EnumValue interface { NamedDescriptor OptionExtensionDescriptor Enum() Enum Number() int Deprecated() bool NumberLocation() Location }
EnumValue is an enum value descriptor.
type ExtensionRange ¶
type ExtensionRange interface { MessageRange OptionExtensionDescriptor }
ExtensionRange represents an extension range in Messages.
type FeaturesDescriptor ¶
type FeaturesDescriptor interface { // FieldPresenceLocation returns the location for the field_presence // feature, if it is present in the options of the containing element. FieldPresenceLocation() Location // EnumTypeLocation returns the location for the enum_type // feature, if it is present in the options of the containing element. EnumTypeLocation() Location // RepeatedFieldEncodingLocation returns the location for the repeated_field_encoding // feature, if it is present in the options of the containing element. RepeatedFieldEncodingLocation() Location // UTF8ValidationLocation returns the location for the utf8_validation // feature, if it is present in the options of the containing element. UTF8ValidationLocation() Location // MessageEncodingLocation returns the location for the message_encoding // feature, if it is present in the options of the containing element. MessageEncodingLocation() Location // JSONFormatLocation returns the location for the json_format // feature, if it is present in the options of the containing element. JSONFormatLocation() Location }
FeaturesDescriptor contains information about features, which are special options in Protobuf Editions.
type Field ¶
type Field interface { NamedDescriptor OptionExtensionDescriptor // May be nil if this is attached to a file. ParentMessage() Message Number() int Label() descriptorpb.FieldDescriptorProto_Label Type() descriptorpb.FieldDescriptorProto_Type TypeName() string // may be nil Oneof() Oneof Proto3Optional() bool JSONName() string JSType() descriptorpb.FieldOptions_JSType CType() descriptorpb.FieldOptions_CType Retention() descriptorpb.FieldOptions_OptionRetention Targets() []descriptorpb.FieldOptions_OptionTargetType DebugRedact() bool // Set vs unset matters for packed // See the comments on descriptor.proto Packed() *bool Deprecated() bool // Default is the field's default value, encoded as a string. // Instead of trying to interpret or decode this string, it is // typically better to instead use AsDescriptor() and query the // Default() method of the resulting protoreflect.FieldDescriptor. // If empty, the default value is a zero value for the field's // type. Defaults cannot be set for repeated or message fields // (which also means it cannot be set for map fields). Default() string // Empty string unless the field is part of an extension Extendee() string NumberLocation() Location TypeLocation() Location TypeNameLocation() Location JSONNameLocation() Location JSTypeLocation() Location CTypeLocation() Location PackedLocation() Location DefaultLocation() Location ExtendeeLocation() Location // AsDescriptor returns a [protoreflect.Descriptor] that // corresponds to this message. This should only be needed // for reflection usages. AsDescriptor() (protoreflect.FieldDescriptor, error) }
Field is a field descriptor.
type File ¶
type File interface { Descriptor FileInfo // Top-level only. ContainerDescriptor OptionExtensionDescriptor Syntax() Syntax Package() string FileImports() []FileImport Services() []Service Extensions() []Field Edition() descriptorpb.Edition CsharpNamespace() string GoPackage() string JavaMultipleFiles() bool JavaOuterClassname() string JavaPackage() string JavaStringCheckUtf8() bool ObjcClassPrefix() string PhpClassPrefix() string PhpNamespace() string PhpMetadataNamespace() string RubyPackage() string SwiftPrefix() string Deprecated() bool OptimizeFor() descriptorpb.FileOptions_OptimizeMode CcGenericServices() bool JavaGenericServices() bool PyGenericServices() bool CcEnableArenas() bool SyntaxLocation() Location PackageLocation() Location CsharpNamespaceLocation() Location GoPackageLocation() Location JavaMultipleFilesLocation() Location JavaOuterClassnameLocation() Location JavaPackageLocation() Location JavaStringCheckUtf8Location() Location ObjcClassPrefixLocation() Location PhpClassPrefixLocation() Location PhpNamespaceLocation() Location PhpMetadataNamespaceLocation() Location RubyPackageLocation() Location SwiftPrefixLocation() Location OptimizeForLocation() Location CcGenericServicesLocation() Location JavaGenericServicesLocation() Location PyGenericServicesLocation() Location CcEnableArenasLocation() Location // FileDescriptor returns the backing FileDescriptor for this File. // // Users should prefer to use the core protosource API to read properties of the File as opposed // to using the FileDescriptor directly, however we needed to add this to be able to build // Resolvers, and did not want to rewrite the whole API. FileDescriptor() protodescriptor.FileDescriptor }
File is a file descriptor.
type FileImport ¶
type FileImport interface { LocationDescriptor Import() string IsPublic() bool IsWeak() bool IsUnused() bool }
FileImport is a file import descriptor.
type FileInfo ¶
type FileInfo interface { // Path is the path of the file relative to the root it is contained within. // This will be normalized, validated and never empty, // This will be unique within a given Image. Path() string // Path: one/one.proto // RootDirPath: proto // ExternalPath: /foo/bar/proto/one/one.proto ExternalPath() string // ModuleFullName is the module that this file came from. // // Note this *can* be nil if we did not build from a named module. // All code must assume this can be nil. // Note that nil checking should work since the backing type is always a pointer. ModuleFullName() bufmodule.ModuleFullName // CommitID is the commit for the module that this file came from. // // This will only be set if ModuleFullName is set, but may not be set // even if ModuleFullName is set, that is commit is optional information // even if we know what module this file came from. CommitID() uuid.UUID // IsImport returns true if this file is an import. IsImport() bool }
FileInfo contains Protobuf file info.
type Location ¶
type Location interface { StartLine() int StartColumn() int EndLine() int EndColumn() int LeadingComments() string TrailingComments() string // NOT a copy. Do not modify. LeadingDetachedComments() []string }
Location defines source code info location information.
May be extended in the future to include comments.
Note that unlike SourceCodeInfo_Location, these are not zero-indexed.
type LocationDescriptor ¶
type LocationDescriptor interface { Descriptor // Location returns the location of the entire descriptor. // // Can return nil, although will generally not be nil. Location() Location }
LocationDescriptor is the base interface for a descriptor type with a location.
type Message ¶
type Message interface { NamedDescriptor // Only those directly nested under this message. ContainerDescriptor ReservedDescriptor OptionExtensionDescriptor // Includes fields in oneofs. Fields() []Field Extensions() []Field Oneofs() []Oneof ExtensionRanges() []ExtensionRange ExtensionMessageRanges() []MessageRange ReservedMessageRanges() []MessageRange // Will return nil if this is a top-level message Parent() Message IsMapEntry() bool MessageSetWireFormat() bool NoStandardDescriptorAccessor() bool DeprecatedLegacyJSONFieldConflicts() bool Deprecated() bool MessageSetWireFormatLocation() Location NoStandardDescriptorAccessorLocation() Location // AsDescriptor returns a [protoreflect.Descriptor] that // corresponds to this message. This should only be needed // for reflection usages. AsDescriptor() (protoreflect.MessageDescriptor, error) }
Message is a message descriptor.
type MessageRange ¶
MessageRange is a TagRange for Messages.
type Method ¶
type Method interface { NamedDescriptor OptionExtensionDescriptor Service() Service InputTypeName() string OutputTypeName() string ClientStreaming() bool ServerStreaming() bool InputTypeLocation() Location OutputTypeLocation() Location Deprecated() bool IdempotencyLevel() descriptorpb.MethodOptions_IdempotencyLevel IdempotencyLevelLocation() Location }
Method is a method descriptor.
type ModuleFullName ¶
ModuleFullName is a module full name.
type NamedDescriptor ¶
type NamedDescriptor interface { LocationDescriptor // FullName returns the fully-qualified name, i.e. some.pkg.Nested.ParentMessage.FooEnum.ENUM_VALUE. // // Always non-empty. FullName() string // NestedName returns the full nested name without the package, i.e. Nested.ParentMessage.FooEnum // or Nested.ParentMessage.FooEnum.ENUM_VALUE. // // Always non-empty. NestedName() string // Name returns the short name, or the name of a value or field, i.e. FooEnum or ENUM_VALUE. // // Always non-empty. Name() string // NameLocation returns the location of the name of the descriptor. // // If the backing descriptor does not have name-level resolution, this will // attempt to return a location of the entire descriptor. // // If the backing descriptor has comments for the entire descriptor, these // will be added to the named location. // // Can return nil. NameLocation() Location }
NamedDescriptor is the base interface for a named descriptor type.
type Oneof ¶
type Oneof interface { NamedDescriptor OptionExtensionDescriptor Message() Message Fields() []Field // AsDescriptor returns a [protoreflect.Descriptor] that // corresponds to this message. This should only be needed // for reflection usages. AsDescriptor() (protoreflect.OneofDescriptor, error) }
Oneof is a oneof descriptor.
type OptionExtensionDescriptor ¶
type OptionExtensionDescriptor interface { // Features returns information about any features present in the // options. It only provides information about standard features, // not custom features (i.e. extensions of google.protobuf.FeatureSet). // // Never returns nil. Features() FeaturesDescriptor // OptionExtension returns the value for an options extension field. // // Returns false if the extension is not set. // // See https://pkg.go.dev/google.golang.org/protobuf/proto#HasExtension // See https://pkg.go.dev/google.golang.org/protobuf/proto#GetExtension OptionExtension(extensionType protoreflect.ExtensionType) (interface{}, bool) // OptionExtensionLocation returns the source location where the given extension // field value is defined. This is the same as OptionLocation, but specific to // extension fields. OptionExtensionLocation(extensionType protoreflect.ExtensionType, extraPath ...int32) Location // OptionLocation returns the source location where the given option field // value is defined. The extra path can be additional path elements, for getting // getting the location of specific elements inside the field, for message // and repeated values. // // If a precise location cannot be found, but a general one can be, the general // location will be returned. For example, if a specific field inside a message // field is requested but the source code info only includes information // about the message itself (and not that particular field), the location of the // message value is returned. Conversely, if a message location is requested but // the source code info only has information about specific fields inside that // message, the first such location is returned. Similarly, if multiple locations // are in source code info for the requested value, the first one is returned. // // If no relevant location is found in source code info, this returns nil. OptionLocation(field protoreflect.FieldDescriptor, extraPath ...int32) Location // PresentExtensionNumbers returns field numbers for all extensions/custom options // that have a set value on this descriptor. PresentExtensionNumbers() []int32 // ForEachPresentOption iterates through all options that have a set value on this // descriptor, invoking fn for each present option. // // If fn returns false, the iteration is terminated and ForEachPresentOption // immediately returns. ForEachPresentOption(fn func(protoreflect.FieldDescriptor, protoreflect.Value) bool) }
OptionExtensionDescriptor contains options and option extensions.
type ReservedDescriptor ¶
type ReservedDescriptor interface { ReservedTagRanges() []TagRange ReservedNames() []ReservedName }
ReservedDescriptor has reserved ranges and names.
type ReservedName ¶
type ReservedName interface { LocationDescriptor Value() string }
ReservedName is a reserved name for an enum or message.
type Service ¶
type Service interface { NamedDescriptor OptionExtensionDescriptor Methods() []Method Deprecated() bool }
Service is a service descriptor.
type Syntax ¶
type Syntax int
Syntax is the syntax of a file.
const ( // SyntaxUnspecified represents no syntax being specified. // // This is functionally equivalent to SyntaxProto2. SyntaxUnspecified Syntax = iota + 1 // SyntaxProto2 represents the proto2 syntax. SyntaxProto2 // SyntaxProto3 represents the proto3 syntax. SyntaxProto3 // SyntaxEditions represents the editions syntax. SyntaxEditions )
type TagRange ¶
type TagRange interface { LocationDescriptor // Start is the start of the range. Start() int // End is the end of the range. // Inclusive. End() int // Max says that the End is the max. Max() bool }
TagRange is a tag range from start to end.
func CheckTagRangeIsSubset ¶
CheckTagRangeIsSubset checks if supersetRanges is a superset of subsetRanges. If so, it returns true and nil. If not, it returns false with a slice of failing ranges from subsetRanges.
Source Files ¶
- bufprotosource.go
- descriptor.go
- enum.go
- enum_range.go
- enum_value.go
- field.go
- file.go
- file_import.go
- files.go
- location.go
- location_descriptor.go
- location_store.go
- merge_comment_location.go
- message.go
- message_range.go
- method.go
- named_descriptor.go
- oneof.go
- option_extension_descriptor.go
- paths.go
- reserved_name.go
- service.go
- usage.gen.go