Documentation
¶
Overview ¶
Package generator provides utilities for protoc plugin development.
This package includes:
Descriptor type checking utilities:
- AsMessage, IsMessage - for DescriptorProto
- AsMessageWithName, IsMessageWithName - for DescriptorProto with name check
- AsFieldType, IsFieldType - for FieldDescriptorProto
- AsEnumType, IsEnumType - for EnumDescriptorProto
- AsServiceType, IsServiceType - for ServiceDescriptorProto
- AsMethodType, IsMethodType - for MethodDescriptorProto
- AsFileType, IsFileType - for FileDescriptorProto
Field classification utilities:
- AsScalarField, IsScalarField - for scalar type fields
- AsMessageField, IsMessageField - for message type fields (TYPE_MESSAGE only)
- AsGroupField, IsGroupField - for group type fields (TYPE_GROUP, deprecated)
- AsEnumField, IsEnumField - for enum type fields
- AsRepeatedField, IsRepeatedField - for repeated fields
- AsMapField, IsMapField - for map fields (heuristic check)
- AsMapFieldWithMessage, IsMapFieldWithMessage - for map fields (definitive check)
- AsOneOfField, IsOneOfField - for oneof fields
- AsOptionalField, IsOptionalField - for optional fields
- AsRequiredField, IsRequiredField - for required fields
Test utilities for creating descriptor objects:
- NewField - create optional field with scalar type.
- NewRepeatedField - create repeated field.
- NewRequiredField - create required field (proto2).
- NewMessageField - create message type field.
- NewEnumField - create enum type field.
- NewMapField - create map field.
- NewOneOfField - create oneof field.
- NewFieldWithType - create minimal field with only type set.
- NewFieldWithLabel - create minimal field with only label set.
- NewRepeatedMessageField - create repeated message field.
- NewMessage - create message descriptor.
- NewMessageWithNested - create message with nested types.
- NewEnum - create enum descriptor.
- NewEnumValue - create enum value descriptor.
- NewService - create service descriptor.
- NewMethod - create method descriptor.
- NewFile - create file descriptor.
- NewFileWithTypes - create file with messages, enums, and services.
- NewOneOf - create oneof descriptor.
- Type constants (TypeString, TypeInt32, etc.) for field types.
Future releases will add:
- Descriptor traversal utilities.
- Path construction and naming helpers.
- Visitor patterns for walking descriptor trees.
- Code generation output management.
- Context management for build environments.
Index ¶
- Constants
- func AsEnumField(field proto.Message) (*descriptorpb.FieldDescriptorProto, bool)
- func AsEnumType(desc proto.Message) (*descriptorpb.EnumDescriptorProto, bool)
- func AsFieldType(desc proto.Message) (*descriptorpb.FieldDescriptorProto, bool)
- func AsFileType(desc proto.Message) (*descriptorpb.FileDescriptorProto, bool)
- func AsGroupField(field proto.Message) (*descriptorpb.FieldDescriptorProto, bool)
- func AsMapField(field proto.Message) (*descriptorpb.FieldDescriptorProto, bool)
- func AsMapFieldWithMessage(field proto.Message, entryMsg proto.Message) (*descriptorpb.FieldDescriptorProto, bool)
- func AsMessage(desc proto.Message) (*descriptorpb.DescriptorProto, bool)
- func AsMessageField(field proto.Message) (*descriptorpb.FieldDescriptorProto, bool)
- func AsMessageWithName(desc proto.Message, name string) (*descriptorpb.DescriptorProto, bool)
- func AsMethodType(desc proto.Message) (*descriptorpb.MethodDescriptorProto, bool)
- func AsOneOfField(field proto.Message) (*descriptorpb.FieldDescriptorProto, bool)
- func AsOptionalField(field proto.Message) (*descriptorpb.FieldDescriptorProto, bool)
- func AsRepeatedField(field proto.Message) (*descriptorpb.FieldDescriptorProto, bool)
- func AsRequiredField(field proto.Message) (*descriptorpb.FieldDescriptorProto, bool)
- func AsScalarField(field proto.Message) (*descriptorpb.FieldDescriptorProto, bool)
- func AsServiceType(desc proto.Message) (*descriptorpb.ServiceDescriptorProto, bool)
- func IsEnumField(field proto.Message) bool
- func IsEnumType(desc proto.Message) bool
- func IsFieldType(desc proto.Message) bool
- func IsFileType(desc proto.Message) bool
- func IsGroupField(field proto.Message) bool
- func IsMapField(field proto.Message) bool
- func IsMapFieldWithMessage(field proto.Message, entryMsg proto.Message) bool
- func IsMessage(desc proto.Message) bool
- func IsMessageField(field proto.Message) bool
- func IsMessageWithName(desc proto.Message, name string) bool
- func IsMethodType(desc proto.Message) bool
- func IsOneOfField(field proto.Message) bool
- func IsOptionalField(field proto.Message) bool
- func IsRepeatedField(field proto.Message) bool
- func IsRequiredField(field proto.Message) bool
- func IsScalarField(field proto.Message) bool
- func IsServiceType(desc proto.Message) bool
- func NewEnum(name string, values ...string) *descriptorpb.EnumDescriptorProto
- func NewEnumField(name string, number int32, typeName string) *descriptorpb.FieldDescriptorProto
- func NewEnumValue(name string, number int32) *descriptorpb.EnumValueDescriptorProto
- func NewField(name string, number int32, fieldType descriptorpb.FieldDescriptorProto_Type) *descriptorpb.FieldDescriptorProto
- func NewFieldWithLabel(label descriptorpb.FieldDescriptorProto_Label) *descriptorpb.FieldDescriptorProto
- func NewFieldWithType(fieldType descriptorpb.FieldDescriptorProto_Type) *descriptorpb.FieldDescriptorProto
- func NewFile(name, pkg string) *descriptorpb.FileDescriptorProto
- func NewFileWithTypes(name, pkg string, messages []*descriptorpb.DescriptorProto, ...) *descriptorpb.FileDescriptorProto
- func NewMapField(name string, number int32, entryTypeName string) *descriptorpb.FieldDescriptorProto
- func NewMessage(name string, fields ...*descriptorpb.FieldDescriptorProto) *descriptorpb.DescriptorProto
- func NewMessageField(name string, number int32, typeName string) *descriptorpb.FieldDescriptorProto
- func NewMessageWithNested(name string, fields []*descriptorpb.FieldDescriptorProto, ...) *descriptorpb.DescriptorProto
- func NewMethod(name, inputType, outputType string) *descriptorpb.MethodDescriptorProto
- func NewOneOf(name string) *descriptorpb.OneofDescriptorProto
- func NewOneOfField(name string, number int32, fieldType descriptorpb.FieldDescriptorProto_Type, ...) *descriptorpb.FieldDescriptorProto
- func NewRepeatedField(name string, number int32, fieldType descriptorpb.FieldDescriptorProto_Type) *descriptorpb.FieldDescriptorProto
- func NewRepeatedMessageField(typeName string) *descriptorpb.FieldDescriptorProto
- func NewRequiredField(name string, number int32, fieldType descriptorpb.FieldDescriptorProto_Type) *descriptorpb.FieldDescriptorProto
- func NewService(name string, methods ...*descriptorpb.MethodDescriptorProto) *descriptorpb.ServiceDescriptorProto
Constants ¶
const ( // Numeric types TypeDouble = descriptorpb.FieldDescriptorProto_TYPE_DOUBLE TypeFloat = descriptorpb.FieldDescriptorProto_TYPE_FLOAT TypeInt64 = descriptorpb.FieldDescriptorProto_TYPE_INT64 TypeUInt64 = descriptorpb.FieldDescriptorProto_TYPE_UINT64 TypeInt32 = descriptorpb.FieldDescriptorProto_TYPE_INT32 TypeFixed64 = descriptorpb.FieldDescriptorProto_TYPE_FIXED64 TypeFixed32 = descriptorpb.FieldDescriptorProto_TYPE_FIXED32 TypeSFixed32 = descriptorpb.FieldDescriptorProto_TYPE_SFIXED32 TypeSFixed64 = descriptorpb.FieldDescriptorProto_TYPE_SFIXED64 TypeSInt32 = descriptorpb.FieldDescriptorProto_TYPE_SINT32 TypeSInt64 = descriptorpb.FieldDescriptorProto_TYPE_SINT64 TypeUInt32 = descriptorpb.FieldDescriptorProto_TYPE_UINT32 // Boolean and string types TypeBool = descriptorpb.FieldDescriptorProto_TYPE_BOOL TypeString = descriptorpb.FieldDescriptorProto_TYPE_STRING TypeBytes = descriptorpb.FieldDescriptorProto_TYPE_BYTES // Complex types TypeGroup = descriptorpb.FieldDescriptorProto_TYPE_GROUP TypeMessage = descriptorpb.FieldDescriptorProto_TYPE_MESSAGE TypeEnum = descriptorpb.FieldDescriptorProto_TYPE_ENUM )
Field type aliases for common protobuf field types. These provide shorter, more readable names for the descriptorpb constants. cspell:ignore SFIXED SINT
const ( LabelOptional = descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL LabelRequired = descriptorpb.FieldDescriptorProto_LABEL_REQUIRED LabelRepeated = descriptorpb.FieldDescriptorProto_LABEL_REPEATED )
Label type aliases for field cardinality
Variables ¶
This section is empty.
Functions ¶
func AsEnumField ¶
func AsEnumField(field proto.Message) (*descriptorpb.FieldDescriptorProto, bool)
AsEnumField checks if the field is an enum type and returns it as a field descriptor. Returns the field descriptor and true if it's an enum field, nil and false otherwise.
func AsEnumType ¶
func AsEnumType(desc proto.Message) (*descriptorpb.EnumDescriptorProto, bool)
AsEnumType attempts to cast the descriptor to an enum descriptor and validates it has a Name. Returns the enum descriptor and true if successful and Name is not nil/empty, nil and false otherwise. An EnumDescriptorProto without a Name is considered invalid as every enum must have a name.
func AsFieldType ¶
func AsFieldType(desc proto.Message) (*descriptorpb.FieldDescriptorProto, bool)
AsFieldType attempts to cast the descriptor to a field descriptor and validates it has a Type. Returns the field descriptor and true if successful and Type is not nil, nil and false otherwise. A FieldDescriptorProto without a Type is considered invalid as every field must have a type.
func AsFileType ¶
func AsFileType(desc proto.Message) (*descriptorpb.FileDescriptorProto, bool)
AsFileType attempts to cast the descriptor to a file descriptor and validates it has a Name. Returns the file descriptor and true if successful and Name is not nil/empty, nil and false otherwise. A FileDescriptorProto without a Name is considered invalid as every file must have a name.
func AsGroupField ¶
func AsGroupField(field proto.Message) (*descriptorpb.FieldDescriptorProto, bool)
AsGroupField checks if the field is a group type and returns it as a field descriptor. Returns the field descriptor and true if it's a TYPE_GROUP field, nil and false otherwise. Note: Groups are deprecated in proto2 and not supported in proto3. Use message types instead.
func AsMapField ¶
func AsMapField(field proto.Message) (*descriptorpb.FieldDescriptorProto, bool)
AsMapField checks if the field is a map field and returns it as a field descriptor. In protobuf, map fields are represented as repeated message fields where the message type is a special map entry type. This function uses a heuristic: type names ending with "Entry". For definitive checking, use AsMapFieldWithMessage. Returns the field descriptor and true if it's a map field, nil and false otherwise.
func AsMapFieldWithMessage ¶
func AsMapFieldWithMessage(field proto.Message, entryMsg proto.Message) (*descriptorpb.FieldDescriptorProto, bool)
AsMapFieldWithMessage checks if the field is a map field by examining the map entry's message descriptor. This is the definitive check as it verifies the map_entry=true option. Returns the field descriptor and true if it's a map field, nil and false otherwise.
func AsMessage ¶
func AsMessage(desc proto.Message) (*descriptorpb.DescriptorProto, bool)
AsMessage attempts to cast the descriptor to a message descriptor and validates it has a Name. Returns the message descriptor and true if successful and Name is not nil/empty, nil and false otherwise. A DescriptorProto without a Name is considered invalid as every message must have a name.
func AsMessageField ¶
func AsMessageField(field proto.Message) (*descriptorpb.FieldDescriptorProto, bool)
AsMessageField checks if the field is a message type and returns it as a field descriptor. Returns the field descriptor and true if it's a TYPE_MESSAGE field, nil and false otherwise. Note: This does not include TYPE_GROUP fields. Use AsGroupField for groups.
func AsMessageWithName ¶
func AsMessageWithName(desc proto.Message, name string) (*descriptorpb.DescriptorProto, bool)
AsMessageWithName attempts to cast the descriptor to a message descriptor and checks if it has the given name. Returns the message descriptor and true if successful and name matches (empty name matches any).
func AsMethodType ¶
func AsMethodType(desc proto.Message) (*descriptorpb.MethodDescriptorProto, bool)
AsMethodType attempts to cast the descriptor to a method descriptor and validates essential fields. Returns the method descriptor and true if successful with valid Name, InputType and OutputType. A MethodDescriptorProto without these fields is considered invalid.
func AsOneOfField ¶
func AsOneOfField(field proto.Message) (*descriptorpb.FieldDescriptorProto, bool)
AsOneOfField checks if the field is part of a oneof and returns it as a field descriptor. Returns the field descriptor and true if it's part of a oneof, nil and false otherwise.
func AsOptionalField ¶
func AsOptionalField(field proto.Message) (*descriptorpb.FieldDescriptorProto, bool)
AsOptionalField checks if the field is optional and returns it as a field descriptor. Returns the field descriptor and true if it has LABEL_OPTIONAL, nil and false otherwise.
Note: This returns true for all fields with LABEL_OPTIONAL, which includes:
- Proto2 optional fields
- Proto3 optional fields (with 'optional' keyword, also have Proto3Optional=true)
- Proto3 singular fields (without 'optional' keyword, have Proto3Optional=false/nil)
To distinguish proto3 'optional' fields specifically, check the Proto3Optional field.
func AsRepeatedField ¶
func AsRepeatedField(field proto.Message) (*descriptorpb.FieldDescriptorProto, bool)
AsRepeatedField checks if the field is repeated and returns it as a field descriptor. Returns the field descriptor and true if it's a repeated field, nil and false otherwise.
func AsRequiredField ¶
func AsRequiredField(field proto.Message) (*descriptorpb.FieldDescriptorProto, bool)
AsRequiredField checks if the field is required and returns it as a field descriptor. Returns the field descriptor and true if it's required, nil and false otherwise (proto2 only).
func AsScalarField ¶
func AsScalarField(field proto.Message) (*descriptorpb.FieldDescriptorProto, bool)
AsScalarField checks if the field is a scalar type and returns it as a field descriptor. Returns the field descriptor and true if it's a scalar field, nil and false otherwise.
func AsServiceType ¶
func AsServiceType(desc proto.Message) (*descriptorpb.ServiceDescriptorProto, bool)
AsServiceType attempts to cast the descriptor to a service descriptor and validates it has a Name. Returns the service descriptor and true if successful and Name is not nil/empty, nil and false otherwise. A ServiceDescriptorProto without a Name is considered invalid as every service must have a name.
func IsEnumField ¶
IsEnumField checks if the field is an enum type. Returns true if the field is TYPE_ENUM, false otherwise.
func IsEnumType ¶
IsEnumType checks if the descriptor is an enum descriptor. Returns true if the descriptor is an EnumDescriptorProto, false otherwise.
func IsFieldType ¶
IsFieldType checks if the descriptor is a valid field descriptor. Returns true if the descriptor is a FieldDescriptorProto with Type set, false otherwise.
func IsFileType ¶
IsFileType checks if the descriptor is a file descriptor. Returns true if the descriptor is a FileDescriptorProto, false otherwise.
func IsGroupField ¶
IsGroupField checks if the field is a group type. Returns true if the field is TYPE_GROUP, false otherwise. Note: Groups are deprecated in proto2 and not supported in proto3. Use message types instead.
func IsMapField ¶
IsMapField checks if the field is a map field. Returns true if the field represents a protobuf map, false otherwise.
func IsMapFieldWithMessage ¶
IsMapFieldWithMessage checks if the field is a map field with definitive message descriptor check. Returns true if the field represents a protobuf map with map_entry=true, false otherwise.
func IsMessage ¶
IsMessage checks if the descriptor is a message descriptor. Returns true if the descriptor is a DescriptorProto, false otherwise.
func IsMessageField ¶
IsMessageField checks if the field is a message type. Returns true if the field is TYPE_MESSAGE, false otherwise. Note: This does not include TYPE_GROUP fields. Use IsGroupField for groups.
func IsMessageWithName ¶
IsMessageWithName checks if the descriptor is a message type with the given name. Returns true if the descriptor is a DescriptorProto with matching name, false otherwise.
func IsMethodType ¶
IsMethodType checks if the descriptor is a method descriptor. Returns true if the descriptor is a MethodDescriptorProto, false otherwise.
func IsOneOfField ¶
IsOneOfField checks if the field is part of a oneof. Returns true if the field has a OneofIndex set, false otherwise.
func IsOptionalField ¶
IsOptionalField checks if the field is optional. Returns true if the field has LABEL_OPTIONAL, false otherwise.
Note: This returns true for all fields with LABEL_OPTIONAL, which includes:
- Proto2 optional fields
- Proto3 optional fields (with 'optional' keyword, also have Proto3Optional=true)
- Proto3 singular fields (without 'optional' keyword, have Proto3Optional=false/nil)
To distinguish proto3 'optional' fields specifically, check the Proto3Optional field.
func IsRepeatedField ¶
IsRepeatedField checks if the field is repeated. Returns true if the field has LABEL_REPEATED, false otherwise.
func IsRequiredField ¶
IsRequiredField checks if the field is required (proto2 only). Returns true if the field has LABEL_REQUIRED, false otherwise.
func IsScalarField ¶
IsScalarField checks if the field is a scalar type. Returns true if the field is a scalar protobuf type, false otherwise.
func IsServiceType ¶
IsServiceType checks if the descriptor is a service descriptor. Returns true if the descriptor is a ServiceDescriptorProto, false otherwise.
func NewEnum ¶
func NewEnum(name string, values ...string) *descriptorpb.EnumDescriptorProto
NewEnum creates an enum descriptor with the given name and values. Returns an EnumDescriptorProto with values numbered from 0.
func NewEnumField ¶
func NewEnumField(name string, number int32, typeName string) *descriptorpb.FieldDescriptorProto
NewEnumField creates an enum type field descriptor. Returns a FieldDescriptorProto with TYPE_ENUM and the given type name.
func NewEnumValue ¶
func NewEnumValue(name string, number int32) *descriptorpb.EnumValueDescriptorProto
NewEnumValue creates an enum value descriptor. Returns an EnumValueDescriptorProto with the given name and number.
func NewField ¶
func NewField(name string, number int32, fieldType descriptorpb.FieldDescriptorProto_Type) *descriptorpb.FieldDescriptorProto
NewField creates a field descriptor with the given name and type. Returns a FieldDescriptorProto with LABEL_OPTIONAL.
func NewFieldWithLabel ¶
func NewFieldWithLabel(label descriptorpb.FieldDescriptorProto_Label) *descriptorpb.FieldDescriptorProto
NewFieldWithLabel creates a minimal field descriptor with label and a default type Useful for testing cardinality functions. Uses TYPE_STRING as default type.
func NewFieldWithType ¶
func NewFieldWithType(fieldType descriptorpb.FieldDescriptorProto_Type) *descriptorpb.FieldDescriptorProto
NewFieldWithType creates a minimal field descriptor with only type set Useful for testing type-checking functions
func NewFile ¶
func NewFile(name, pkg string) *descriptorpb.FileDescriptorProto
NewFile creates a file descriptor with the given name and package. Returns a FileDescriptorProto with basic metadata.
func NewFileWithTypes ¶
func NewFileWithTypes(name, pkg string, messages []*descriptorpb.DescriptorProto, enums []*descriptorpb.EnumDescriptorProto, services []*descriptorpb.ServiceDescriptorProto) *descriptorpb.FileDescriptorProto
NewFileWithTypes creates a file descriptor with various types. Returns a FileDescriptorProto with messages, enums, and services.
func NewMapField ¶
func NewMapField(name string, number int32, entryTypeName string) *descriptorpb.FieldDescriptorProto
NewMapField creates a map field descriptor. Note: This creates a repeated message field with the given entry type name. The actual map entry message with map_entry option should be defined separately.
func NewMessage ¶
func NewMessage(name string, fields ...*descriptorpb.FieldDescriptorProto) *descriptorpb.DescriptorProto
NewMessage creates a message descriptor with the given name. Returns a DescriptorProto with the provided fields.
func NewMessageField ¶
func NewMessageField(name string, number int32, typeName string) *descriptorpb.FieldDescriptorProto
NewMessageField creates a message type field descriptor. Returns a FieldDescriptorProto with TYPE_MESSAGE and the given type name.
func NewMessageWithNested ¶
func NewMessageWithNested(name string, fields []*descriptorpb.FieldDescriptorProto, nestedMessages []*descriptorpb.DescriptorProto, nestedEnums []*descriptorpb.EnumDescriptorProto) *descriptorpb.DescriptorProto
NewMessageWithNested creates a message with nested types. Returns a DescriptorProto with nested messages and enums.
func NewMethod ¶
func NewMethod(name, inputType, outputType string) *descriptorpb.MethodDescriptorProto
NewMethod creates a method descriptor. Returns a MethodDescriptorProto with the given input and output types.
func NewOneOf ¶
func NewOneOf(name string) *descriptorpb.OneofDescriptorProto
NewOneOf creates a oneof descriptor. Returns a OneofDescriptorProto with the given name.
func NewOneOfField ¶
func NewOneOfField(name string, number int32, fieldType descriptorpb.FieldDescriptorProto_Type, oneOfIndex int32) *descriptorpb.FieldDescriptorProto
NewOneOfField creates a field that's part of a oneof. Returns a FieldDescriptorProto with the given oneof index.
func NewRepeatedField ¶
func NewRepeatedField(name string, number int32, fieldType descriptorpb.FieldDescriptorProto_Type) *descriptorpb.FieldDescriptorProto
NewRepeatedField creates a repeated field descriptor. Returns a FieldDescriptorProto with LABEL_REPEATED.
func NewRepeatedMessageField ¶
func NewRepeatedMessageField(typeName string) *descriptorpb.FieldDescriptorProto
NewRepeatedMessageField creates a repeated message field with optional type name Useful for map field testing and other repeated message scenarios
func NewRequiredField ¶
func NewRequiredField(name string, number int32, fieldType descriptorpb.FieldDescriptorProto_Type) *descriptorpb.FieldDescriptorProto
NewRequiredField creates a required field descriptor (proto2). Returns a FieldDescriptorProto with LABEL_REQUIRED.
func NewService ¶
func NewService(name string, methods ...*descriptorpb.MethodDescriptorProto) *descriptorpb.ServiceDescriptorProto
NewService creates a service descriptor with the given name. Returns a ServiceDescriptorProto with the provided methods.
Types ¶
This section is empty.