Package descutil contains utility functions for protobuf descriptor messages.



View Source
const (
	// FileDescriptorProtoPackageTag is the number of package field
	// in FileDescriptorProto message.
	FileDescriptorProtoPackageTag = 2
	// FileDescriptorProtoMessageTag is the number of message field
	// in FileDescriptorProto message.
	FileDescriptorProtoMessageTag = 4
	// FileDescriptorProtoEnumTag is the number of enum field
	// in FileDescriptorProto message.
	FileDescriptorProtoEnumTag = 5
	// FileDescriptorProtoServiceTag is the number of service field
	// in FileDescriptorProto message.
	FileDescriptorProtoServiceTag = 6

	// ServiceDescriptorProtoMethodTag is the number of method field
	// in ServiceDescriptorProto message.
	ServiceDescriptorProtoMethodTag = 2

	// DescriptorProtoFieldTag is the number of field field
	// in DescriptorProto message.
	DescriptorProtoFieldTag = 2
	// DescriptorProtoNestedTypeTag is the number of nested_type field
	// in DescriptorProto message.
	DescriptorProtoNestedTypeTag = 3
	// DescriptorProtoEnumTypeTag is the number of enum_type field
	// in DescriptorProto message.
	DescriptorProtoEnumTypeTag = 4
	// DescriptorProtoOneOfTag is the number of oneof_decl field
	// in DescriptorProto message.
	DescriptorProtoOneOfTag = 8

	// EnumDescriptorProtoValueTag is the number of value field
	// in EnumDescriptorProto message.
	EnumDescriptorProtoValueTag = 2

These constnats correspond to tag values in the respective "descriptor.proto" message types. These constants' tag matches are asserted in the "TestTagsMatchProto" unit test.


This section is empty.


func At

func At(descProto proto.Message, path []int32) (interface{}, error)

At returns a descriptor proto or its field value at the given path. The path has same semantics as descriptor.SourceCodeInfo_Location.Path. See its comment for explanation.

For example, given a FileDescriptorProto and path [4, 2], At will return the 2nd top-level message DescriptorProto because 4 is FileDescriptorProto.MessageType field tag.

Does not supported uninterpreted options, returns (nil, nil).

func FindEnum

func FindEnum(d *pb.DescriptorProto, name string) int

FindEnum searches for a nested EnumDescriptorProto by name.

func FindEnumForFile

func FindEnumForFile(f *pb.FileDescriptorProto, name string) int

FindEnumForFile searches for an EnumDescriptorProto by name.

func FindEnumValue

func FindEnumValue(e *pb.EnumDescriptorProto, name string) int

FindEnumValue searches for an EnumValueDescriptorProto by name.

func FindField

func FindField(d *pb.DescriptorProto, name string) int

FindField searches for a FieldDescriptorProto by name.

func FindFile

func FindFile(s *pb.FileDescriptorSet, name string) int

FindFile searches for a FileDescriptorProto by name.

func FindMessage

func FindMessage(d *pb.DescriptorProto, name string) int

FindMessage searches for a nested DescriptorProto by name.

func FindMessageForFile

func FindMessageForFile(f *pb.FileDescriptorProto, name string) int

FindMessageForFile searches for a DescriptorProto by name.

func FindMethodForService

func FindMethodForService(s *pb.ServiceDescriptorProto, name string) int

FindMethodForService searches for a MethodDescriptorProto by name.

func FindOneOf

func FindOneOf(d *pb.DescriptorProto, name string) int

FindOneOf searches for a nested OneofDescriptorProto by name.

func FindService

func FindService(s *pb.FileDescriptorSet, fullName string) (file *pb.FileDescriptorProto, serviceIndex int)

FindService searches for a service by full name.

func FindServiceForFile

func FindServiceForFile(f *pb.FileDescriptorProto, name string) int

FindServiceForFile searches for a FileDescriptorProto by name.

func FindValueByNumber

func FindValueByNumber(e *pb.EnumDescriptorProto, number int32) int

FindValueByNumber searches for an EnumValueDescriptorProto by number.

func IndexSourceCodeInfo

func IndexSourceCodeInfo(f *pb.FileDescriptorProto) (map[interface{}]*pb.SourceCodeInfo_Location, error)

IndexSourceCodeInfo returns a map that maps a pointer to the associated source code info, where the pointer points to a descriptor proto or its field, e.g. &myFieldDescriptorProto.Name.

IndexSourceCodeInfo can be used to retrieve comments.

Does not support whole-slice locations.

func Repeated

func Repeated(f *pb.FieldDescriptorProto) bool

Repeated returns true if the field is repeated.

func Required

func Required(f *pb.FieldDescriptorProto) bool

Required returns true if the field is required.

func Resolve

func Resolve(s *pb.FileDescriptorSet, fullName string) (file *pb.FileDescriptorProto, obj interface{}, path []int)

Resolve searches for an object by full name. obj can be one of *ServiceDescriptorProto, *MethodDescriptorProto, *DescriptorProto, *FieldDescriptorProto, *DescriptorProto, *EnumDescriptorProto, *EnumValueDescriptorProto or nil

For path, see comment in SourceCodeInfo message.


type Printer

type Printer struct {
	Out indented.Writer

	// Err is not nil if writing to Out failed.
	Err error
	// contains filtered or unexported fields

Printer prints a proto3 definition from a description.

func NewPrinter

func NewPrinter(out io.Writer) *Printer

NewPrinter creates a new Printer which will output protobuf definition text (i.e. ".proto" file) to the given writer.

func (*Printer) AppendLeadingComments

func (p *Printer) AppendLeadingComments(ptr interface{}, lines []string)

AppendLeadingComments allows adding additional leading comments to any printable descriptorpb object associated with this printer.

Each line will be prepended with " " and appended with "\n".


p := NewPrinter(os.Stdout)
p.AppendLeadingComments(protodesc.ToDescriptorProto(myMsg.ProtoReflect()), []string{
  "This is a line.",
  "This is the next line.",

func (*Printer) Enum

func (p *Printer) Enum(enum *descriptorpb.EnumDescriptorProto)

Enum prints an enum definition.

func (*Printer) EnumValue

EnumValue prints an enum value definition.

func (*Printer) Field

func (p *Printer) Field(field *descriptorpb.FieldDescriptorProto)

Field prints a field definition.

func (*Printer) MaybeLeadingComments

func (p *Printer) MaybeLeadingComments(ptr interface{})

MaybeLeadingComments prints leading comments of the descriptorpb proto if found.

func (*Printer) Message

func (p *Printer) Message(msg *descriptorpb.DescriptorProto)

Message prints a message definition.

func (*Printer) Method

func (p *Printer) Method(method *descriptorpb.MethodDescriptorProto)

Method prints a service method definition.

func (*Printer) OneOf

func (p *Printer) OneOf(msg *descriptorpb.DescriptorProto, oneOfIndex int)

OneOf prints a oneof definition.

func (*Printer) Package

func (p *Printer) Package(name string)

Package prints package declaration.

func (*Printer) Printf

func (p *Printer) Printf(format string, a ...interface{})

Printf prints to p.Out unless there was an error.

func (*Printer) Service

func (p *Printer) Service(service *descriptorpb.ServiceDescriptorProto, methodIndex int)

Service prints a service definition. If methodIndex != -1, only one method is printed. If serviceIndex != -1, leading comments are printed if found.

func (*Printer) SetFile

SetFile specifies the file containing the descriptors being printed. Used to relativize names and print comments.