generator

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2025 License: MIT Imports: 3 Imported by: 0

README

Generator Module

Go Reference Go Report Card codecov

Overview

The generator module provides essential utilities for protoc plugin development, including descriptor type checking and test utilities for creating descriptor objects.

Descriptor Type Checking

All type checking functions follow the AsFoo/IsFoo pattern where AsFoo returns the typed descriptor and a boolean, while IsFoo is a convenience wrapper that only returns the boolean.

Type Casting Functions
Function Purpose Parameters Returns
AsMessage Cast to DescriptorProto desc proto.Message *descriptorpb.DescriptorProto, bool
AsMessageWithName Cast to DescriptorProto with name check desc proto.Message, name string *descriptorpb.DescriptorProto, bool
AsFieldType Cast to FieldDescriptorProto desc proto.Message *descriptorpb.FieldDescriptorProto, bool
AsEnumType Cast to EnumDescriptorProto desc proto.Message *descriptorpb.EnumDescriptorProto, bool
AsServiceType Cast to ServiceDescriptorProto desc proto.Message *descriptorpb.ServiceDescriptorProto, bool
AsMethodType Cast to MethodDescriptorProto desc proto.Message *descriptorpb.MethodDescriptorProto, bool
AsFileType Cast to FileDescriptorProto desc proto.Message *descriptorpb.FileDescriptorProto, bool
Type Checking Functions
Function Purpose Parameters Returns
IsMessage Check if DescriptorProto desc proto.Message bool
IsMessageWithName Check if DescriptorProto with name desc proto.Message, name string bool
IsFieldType Check if FieldDescriptorProto desc proto.Message bool
IsEnumType Check if EnumDescriptorProto desc proto.Message bool
IsServiceType Check if ServiceDescriptorProto desc proto.Message bool
IsMethodType Check if MethodDescriptorProto desc proto.Message bool
IsFileType Check if FileDescriptorProto desc proto.Message bool
Field Characteristic Functions
Function Purpose Parameters Returns
Cardinality
AsRepeatedField Cast if repeated field field proto.Message *descriptorpb.FieldDescriptorProto, bool
AsMapField Cast if map field (heuristic) field proto.Message *descriptorpb.FieldDescriptorProto, bool
AsMapFieldWithMessage Cast if map field (definitive) field proto.Message, entryMsg proto.Message *descriptorpb.FieldDescriptorProto, bool
AsOneOfField Cast if oneof field field proto.Message *descriptorpb.FieldDescriptorProto, bool
AsOptionalField Cast if optional field field proto.Message *descriptorpb.FieldDescriptorProto, bool
AsRequiredField Cast if required field field proto.Message *descriptorpb.FieldDescriptorProto, bool
IsRepeatedField Check if repeated field proto.Message bool
IsMapField Check if map (heuristic) field proto.Message bool
IsMapFieldWithMessage Check if map (definitive) field proto.Message, entryMsg proto.Message bool
IsOneOfField Check if oneof field proto.Message bool
IsOptionalField Check if optional field proto.Message bool
IsRequiredField Check if required field proto.Message bool
Type Classification
AsScalarField Cast if scalar type field proto.Message *descriptorpb.FieldDescriptorProto, bool
AsMessageField Cast if message type field proto.Message *descriptorpb.FieldDescriptorProto, bool
AsGroupField Cast if group type (deprecated) field proto.Message *descriptorpb.FieldDescriptorProto, bool
AsEnumField Cast if enum type field proto.Message *descriptorpb.FieldDescriptorProto, bool
IsScalarField Check if scalar field proto.Message bool
IsMessageField Check if message field proto.Message bool
IsGroupField Check if group (deprecated) field proto.Message bool
IsEnumField Check if enum field proto.Message bool
Usage Example
// Using the AsFoo pattern for type-safe access
if field, ok := AsFieldType(desc); ok {
    // field is now typed as *descriptorpb.FieldDescriptorProto
    if repeatedField, ok := AsRepeatedField(field); ok {
        fmt.Printf("Field %s is repeated\n", repeatedField.GetName())
    }
}

// Simple boolean check when you don't need the typed object
if IsMessageWithName(desc, "MyMessage") {
    // Process message type
}

// Chaining checks for specific field types
if field, ok := AsFieldType(desc); ok {
    switch {
    case IsScalarField(field):
        // Handle scalar field
    case IsMessageField(field):
        // Handle message field
    case IsEnumField(field):
        // Handle enum field
    }
}

Test Utilities

Helper functions for creating descriptor objects in tests:

Field Creation Functions

Complete field constructors (with name and number):

Function Purpose Parameters Returns
NewField Create optional field name string, number int32, fieldType descriptorpb.FieldDescriptorProto_Type *descriptorpb.FieldDescriptorProto
NewRepeatedField Create repeated field name string, number int32, fieldType descriptorpb.FieldDescriptorProto_Type *descriptorpb.FieldDescriptorProto
NewRequiredField Create required field (proto2) name string, number int32, fieldType descriptorpb.FieldDescriptorProto_Type *descriptorpb.FieldDescriptorProto
NewMessageField Create message type field name string, number int32, typeName string *descriptorpb.FieldDescriptorProto
NewEnumField Create enum type field name string, number int32, typeName string *descriptorpb.FieldDescriptorProto
NewMapField Create map field name string, number int32, entryTypeName string *descriptorpb.FieldDescriptorProto
NewOneOfField Create oneof field name string, number int32, fieldType descriptorpb.FieldDescriptorProto_Type, oneofIndex int32 *descriptorpb.FieldDescriptorProto

Minimal field constructors (for testing specific properties):

Function Purpose Parameters Returns
NewFieldWithType Create field with type only fieldType descriptorpb.FieldDescriptorProto_Type *descriptorpb.FieldDescriptorProto
NewFieldWithLabel Create field with label only label descriptorpb.FieldDescriptorProto_Label *descriptorpb.FieldDescriptorProto
NewRepeatedMessageField Create repeated message field typeName string *descriptorpb.FieldDescriptorProto
Descriptor Creation Functions
Function Purpose Parameters Returns
NewMessage Create message descriptor name string, fields ...*descriptorpb.FieldDescriptorProto *descriptorpb.DescriptorProto
NewMessageWithNested Create message with nested types name string, messages []*descriptorpb.DescriptorProto, enums []*descriptorpb.EnumDescriptorProto *descriptorpb.DescriptorProto
NewEnum Create enum descriptor name string, values ...string *descriptorpb.EnumDescriptorProto
NewEnumValue Create enum value descriptor name string, number int32 *descriptorpb.EnumValueDescriptorProto
NewService Create service descriptor name string, methods ...*descriptorpb.MethodDescriptorProto *descriptorpb.ServiceDescriptorProto
NewMethod Create method descriptor name, inputType, outputType string *descriptorpb.MethodDescriptorProto
NewFile Create file descriptor name, pkg string, messages []*descriptorpb.DescriptorProto *descriptorpb.FileDescriptorProto
NewFileWithTypes Create file with types name, pkg string, messages []*descriptorpb.DescriptorProto, enums []*descriptorpb.EnumDescriptorProto, services []*descriptorpb.ServiceDescriptorProto *descriptorpb.FileDescriptorProto
NewOneOf Create oneof descriptor name string *descriptorpb.OneofDescriptorProto
Type Constants
Constant Type Value
TypeDouble Floating point descriptorpb.FieldDescriptorProto_TYPE_DOUBLE
TypeFloat Floating point descriptorpb.FieldDescriptorProto_TYPE_FLOAT
TypeInt64 Signed integer descriptorpb.FieldDescriptorProto_TYPE_INT64
TypeUInt64 Unsigned integer descriptorpb.FieldDescriptorProto_TYPE_UINT64
TypeInt32 Signed integer descriptorpb.FieldDescriptorProto_TYPE_INT32
TypeFixed64 Fixed size descriptorpb.FieldDescriptorProto_TYPE_FIXED64
TypeFixed32 Fixed size descriptorpb.FieldDescriptorProto_TYPE_FIXED32
TypeBool Boolean descriptorpb.FieldDescriptorProto_TYPE_BOOL
TypeString String descriptorpb.FieldDescriptorProto_TYPE_STRING
TypeBytes Binary descriptorpb.FieldDescriptorProto_TYPE_BYTES
TypeUInt32 Unsigned integer descriptorpb.FieldDescriptorProto_TYPE_UINT32
TypeSFixed32 Signed fixed descriptorpb.FieldDescriptorProto_TYPE_SFIXED32
TypeSFixed64 Signed fixed descriptorpb.FieldDescriptorProto_TYPE_SFIXED64
TypeSInt32 Signed integer descriptorpb.FieldDescriptorProto_TYPE_SINT32
TypeSInt64 Signed integer descriptorpb.FieldDescriptorProto_TYPE_SINT64
TypeGroup Group (deprecated) descriptorpb.FieldDescriptorProto_TYPE_GROUP
TypeMessage Message descriptorpb.FieldDescriptorProto_TYPE_MESSAGE
TypeEnum Enum descriptorpb.FieldDescriptorProto_TYPE_ENUM
Example Usage
import (
    "testing"

    "darvaza.org/core"
    "google.golang.org/protobuf/proto"
    "google.golang.org/protobuf/types/descriptorpb"
    "protomcp.org/common/generator"
)

func TestMyGenerator(t *testing.T) {
    // Creating complete message descriptors for testing
    msg := &descriptorpb.DescriptorProto{
        Name: proto.String("User"),
        Field: []*descriptorpb.FieldDescriptorProto{
            generator.NewField("id", 1, generator.TypeInt64),
            generator.NewRepeatedField("tags", 2, generator.TypeString),
            generator.NewMessageField("profile", 3, ".example.Profile"),
            generator.NewEnumField("status", 4, ".example.Status"),
            generator.NewMapField("metadata", 5, ".MetadataEntry"),
            generator.NewOneOfField("variant", 6, generator.TypeBool, 0),
        },
    }
    // Test generator with the constructed message
    core.AssertNotNil(t, msg, "message")
    core.AssertEqual(t, "User", msg.GetName(), "message name")
}

// Minimal field descriptors for testing specific behaviour
func TestFieldProperties(t *testing.T) {
    // Create fields with only the properties needed for testing
    messageField := generator.NewFieldWithType(generator.TypeMessage)
    repeatedField := generator.NewFieldWithLabel(
        descriptorpb.FieldDescriptorProto_LABEL_REPEATED)
    mapField := generator.NewRepeatedMessageField(".MapEntry")

    // Test field properties using core assertions
    core.AssertEqual(t, descriptorpb.FieldDescriptorProto_TYPE_MESSAGE,
        messageField.GetType(), "field type")
    core.AssertEqual(t, descriptorpb.FieldDescriptorProto_LABEL_REPEATED,
        repeatedField.GetLabel(), "field label")
    core.AssertNotNil(t, mapField.TypeName, "map field type name")
}

Planned Core Functionality

The following sections describe planned functionality that will be added incrementally as the protomcp.org ecosystem develops.

Path and Naming Utilities

Required by options module for selector matching:

// Path construction - needed for option selectors
func GetMessageName(desc proto.Message) string
func GetFieldPath(file, msg, field proto.Message) string
func GetFullName(desc proto.Message) string
func GetPackage(file proto.Message) string
func GetQualifiedName(desc proto.Message) string

// Name manipulation
func ToGoName(name string) string
func ToCamelCase(name string) string
func ToSnakeCase(name string) string
func ToKebabCase(name string) string
Descriptor Traversal

Critical for options module's hook system:

// Finding descriptors - needed for option resolution
func FindMessage(file proto.Message, name string) proto.Message
func FindField(msg proto.Message, name string) proto.Message
func FindEnum(file proto.Message, name string) proto.Message
func FindService(file proto.Message, name string) proto.Message
func FindMethod(service proto.Message, name string) proto.Message

// Iteration helpers - needed for applying option hooks
// Callback returns true to continue, false to stop (Go 1.23 convention)
func ForEachField(msg proto.Message, fn func(field proto.Message) bool) error
func ForEachMessage(file proto.Message, fn func(msg proto.Message) bool) error
func ForEachEnum(file proto.Message, fn func(enum proto.Message) bool) error
func ForEachService(file proto.Message, fn func(svc proto.Message) bool) error
func ForEachMethod(svc proto.Message, fn func(method proto.Message) bool) error

// Nested traversal
func ForEachNestedMessage(
    msg proto.Message, fn func(nested proto.Message) bool) error
func ForEachNestedEnum(
    msg proto.Message, fn func(enum proto.Message) bool) error
Tree Walking

Simple functions for walking descriptor trees:

// Walk descriptor tree with callback functions
func Walk(file proto.Message, onMessage func(proto.Message) error) error
func WalkMessage(msg proto.Message, onField func(proto.Message) error) error

// Path-aware walking
func WalkWithPath(file proto.Message,
    callback func(path []string, desc proto.Message) error) error
Type Resolution

Needed by options for type-safe option handling:

// Type information
func GetFieldType(field proto.Message) descriptorpb.FieldDescriptorProto_Type
func GetFieldTypeName(field proto.Message) string
func IsScalarType(field proto.Message) bool
func IsMessageWithName(field proto.Message, name string) bool
func IsEnumType(field proto.Message) bool

// Type lookup
func ResolveTypeName(file proto.Message, typeName string) proto.Message
func GetTypeDescriptor(field proto.Message) proto.Message
Dependency Analysis

Useful for both options and code generation:

// Dependency tracking
func GetFileDependencies(file proto.Message) []string
func GetMessageDependencies(msg proto.Message) []string
func GetFieldDependencies(field proto.Message) []string

// Import management
func GetRequiredImports(file proto.Message) []string
func ResolveImportPath(file proto.Message, typeName string) string

Comment Access

Helper functions for reading comments from descriptors:

// Comment reading
func GetLeadingComments(desc proto.Message) string
func GetTrailingComments(desc proto.Message) string

Best Practices

  1. Nil Safety: All functions handle nil inputs gracefully.
  2. Error Propagation: Iteration functions stop on first error.
  3. Type Safety: Use type assertions with checks for proto.Message types.
  4. Performance: Cache frequently accessed paths and names.
  5. Compatibility: Support both proto2 and proto3 descriptors.

Future Extensions

  • Additional traversal helpers.
  • Dependency graph visualization.
  • Performance optimizations for large schemas.
  • Parallel traversal support.

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

Field type aliases for common protobuf field types. These provide shorter, more readable names for the descriptorpb constants. cspell:ignore SFIXED SINT

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

func IsEnumField(field proto.Message) bool

IsEnumField checks if the field is an enum type. Returns true if the field is TYPE_ENUM, false otherwise.

func IsEnumType

func IsEnumType(desc proto.Message) bool

IsEnumType checks if the descriptor is an enum descriptor. Returns true if the descriptor is an EnumDescriptorProto, false otherwise.

func IsFieldType

func IsFieldType(desc proto.Message) bool

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

func IsFileType(desc proto.Message) bool

IsFileType checks if the descriptor is a file descriptor. Returns true if the descriptor is a FileDescriptorProto, false otherwise.

func IsGroupField

func IsGroupField(field proto.Message) bool

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

func IsMapField(field proto.Message) bool

IsMapField checks if the field is a map field. Returns true if the field represents a protobuf map, false otherwise.

func IsMapFieldWithMessage

func IsMapFieldWithMessage(field proto.Message, entryMsg proto.Message) bool

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

func IsMessage(desc proto.Message) bool

IsMessage checks if the descriptor is a message descriptor. Returns true if the descriptor is a DescriptorProto, false otherwise.

func IsMessageField

func IsMessageField(field proto.Message) bool

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

func IsMessageWithName(desc proto.Message, name string) bool

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

func IsMethodType(desc proto.Message) bool

IsMethodType checks if the descriptor is a method descriptor. Returns true if the descriptor is a MethodDescriptorProto, false otherwise.

func IsOneOfField

func IsOneOfField(field proto.Message) bool

IsOneOfField checks if the field is part of a oneof. Returns true if the field has a OneofIndex set, false otherwise.

func IsOptionalField

func IsOptionalField(field proto.Message) bool

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

func IsRepeatedField(field proto.Message) bool

IsRepeatedField checks if the field is repeated. Returns true if the field has LABEL_REPEATED, false otherwise.

func IsRequiredField

func IsRequiredField(field proto.Message) bool

IsRequiredField checks if the field is required (proto2 only). Returns true if the field has LABEL_REQUIRED, false otherwise.

func IsScalarField

func IsScalarField(field proto.Message) bool

IsScalarField checks if the field is a scalar type. Returns true if the field is a scalar protobuf type, false otherwise.

func IsServiceType

func IsServiceType(desc proto.Message) bool

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

NewField creates a field descriptor with the given name and type. Returns a FieldDescriptorProto with LABEL_OPTIONAL.

func NewFieldWithLabel

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

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

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

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

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

NewService creates a service descriptor with the given name. Returns a ServiceDescriptorProto with the provided methods.

Types

This section is empty.

Jump to

Keyboard shortcuts

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