Documentation ¶
Overview ¶
Package protokit is a library that makes it easy to create your own protoc plugins. It has excellent test coverage, and saves you so much time!
There are two main things this library provides; a parser for parsing protobuf files into some well-defined structs, and an abstraction to make it simple to write your own protoc plugins.
Getting Started ¶
For a quick view of how to get started, see https://godoc.org/github.com/pseudomuto/protokit#example-RunPlugin
If you want see/try a working example, check out the examples in https://github.com/pseudomuto/protokit/tree/master/examples.
Index ¶
- Constants
- func ContextWithDescriptor(ctx context.Context, d *Descriptor) context.Context
- func ContextWithEnumDescriptor(ctx context.Context, d *EnumDescriptor) context.Context
- func ContextWithFileDescriptor(ctx context.Context, fd *FileDescriptor) context.Context
- func ContextWithServiceDescriptor(ctx context.Context, service *ServiceDescriptor) context.Context
- func RunPlugin(p Plugin) error
- func RunPluginWithIO(p Plugin, r io.Reader, w io.Writer) error
- type Comment
- type Comments
- type Descriptor
- func (m *Descriptor) GetComments() *Comment
- func (m *Descriptor) GetEnum(name string) *EnumDescriptor
- func (m *Descriptor) GetEnums() []*EnumDescriptor
- func (m *Descriptor) GetExtensions() []*ExtensionDescriptor
- func (c *Descriptor) GetFile() *FileDescriptor
- func (c *Descriptor) GetFullName() string
- func (c *Descriptor) GetLongName() string
- func (m *Descriptor) GetMessage(name string) *Descriptor
- func (m *Descriptor) GetMessageField(name string) *FieldDescriptor
- func (m *Descriptor) GetMessageFields() []*FieldDescriptor
- func (m *Descriptor) GetMessages() []*Descriptor
- func (c *Descriptor) GetPackage() string
- func (m *Descriptor) GetParent() *Descriptor
- func (c *Descriptor) IsProto3() bool
- type EnumDescriptor
- func (e *EnumDescriptor) GetComments() *Comment
- func (c *EnumDescriptor) GetFile() *FileDescriptor
- func (c *EnumDescriptor) GetFullName() string
- func (c *EnumDescriptor) GetLongName() string
- func (e *EnumDescriptor) GetNamedValue(name string) *EnumValueDescriptor
- func (c *EnumDescriptor) GetPackage() string
- func (e *EnumDescriptor) GetParent() *Descriptor
- func (e *EnumDescriptor) GetValues() []*EnumValueDescriptor
- func (c *EnumDescriptor) IsProto3() bool
- type EnumValueDescriptor
- func (v *EnumValueDescriptor) GetComments() *Comment
- func (v *EnumValueDescriptor) GetEnum() *EnumDescriptor
- func (c *EnumValueDescriptor) GetFile() *FileDescriptor
- func (c *EnumValueDescriptor) GetFullName() string
- func (c *EnumValueDescriptor) GetLongName() string
- func (c *EnumValueDescriptor) GetPackage() string
- func (c *EnumValueDescriptor) IsProto3() bool
- type ExtensionDescriptor
- func (e *ExtensionDescriptor) GetComments() *Comment
- func (c *ExtensionDescriptor) GetFile() *FileDescriptor
- func (c *ExtensionDescriptor) GetFullName() string
- func (c *ExtensionDescriptor) GetLongName() string
- func (c *ExtensionDescriptor) GetPackage() string
- func (e *ExtensionDescriptor) GetParent() *Descriptor
- func (c *ExtensionDescriptor) IsProto3() bool
- type FieldDescriptor
- func (mf *FieldDescriptor) GetComments() *Comment
- func (c *FieldDescriptor) GetFile() *FileDescriptor
- func (c *FieldDescriptor) GetFullName() string
- func (c *FieldDescriptor) GetLongName() string
- func (mf *FieldDescriptor) GetMessage() *Descriptor
- func (c *FieldDescriptor) GetPackage() string
- func (c *FieldDescriptor) IsProto3() bool
- type FileDescriptor
- func (f *FileDescriptor) GetComments() *Commentdeprecated
- func (f *FileDescriptor) GetEnum(name string) *EnumDescriptor
- func (f *FileDescriptor) GetEnums() []*EnumDescriptor
- func (f *FileDescriptor) GetExtensions() []*ExtensionDescriptor
- func (f *FileDescriptor) GetImports() []*ImportedDescriptor
- func (f *FileDescriptor) GetMessage(name string) *Descriptor
- func (f *FileDescriptor) GetMessages() []*Descriptor
- func (f *FileDescriptor) GetPackageComments() *Comment
- func (f *FileDescriptor) GetService(name string) *ServiceDescriptor
- func (f *FileDescriptor) GetServices() []*ServiceDescriptor
- func (f *FileDescriptor) GetSyntaxComments() *Comment
- func (f *FileDescriptor) IsProto3() bool
- type ImportedDescriptor
- type MethodDescriptor
- func (m *MethodDescriptor) GetComments() *Comment
- func (c *MethodDescriptor) GetFile() *FileDescriptor
- func (c *MethodDescriptor) GetFullName() string
- func (c *MethodDescriptor) GetLongName() string
- func (c *MethodDescriptor) GetPackage() string
- func (m *MethodDescriptor) GetService() *ServiceDescriptor
- func (c *MethodDescriptor) IsProto3() bool
- type Plugin
- type ServiceDescriptor
- func (s *ServiceDescriptor) GetComments() *Comment
- func (c *ServiceDescriptor) GetFile() *FileDescriptor
- func (c *ServiceDescriptor) GetFullName() string
- func (c *ServiceDescriptor) GetLongName() string
- func (s *ServiceDescriptor) GetMethods() []*MethodDescriptor
- func (s *ServiceDescriptor) GetNamedMethod(name string) *MethodDescriptor
- func (c *ServiceDescriptor) GetPackage() string
- func (c *ServiceDescriptor) IsProto3() bool
Examples ¶
Constants ¶
const Version = "0.2.1"
Version describes the current version of protokit being used
Variables ¶
This section is empty.
Functions ¶
func ContextWithDescriptor ¶
func ContextWithDescriptor(ctx context.Context, d *Descriptor) context.Context
ContextWithDescriptor returns a new context with the specified `Descriptor`
func ContextWithEnumDescriptor ¶
func ContextWithEnumDescriptor(ctx context.Context, d *EnumDescriptor) context.Context
ContextWithEnumDescriptor returns a new context with the specified `EnumDescriptor`
func ContextWithFileDescriptor ¶
func ContextWithFileDescriptor(ctx context.Context, fd *FileDescriptor) context.Context
ContextWithFileDescriptor returns a new context with the attached `FileDescriptor`
func ContextWithServiceDescriptor ¶
func ContextWithServiceDescriptor(ctx context.Context, service *ServiceDescriptor) context.Context
ContextWithServiceDescriptor returns a new context with `service`
func RunPlugin ¶
RunPlugin runs the supplied plugin by reading input from stdin and generating output to stdout.
Example ¶
An example of running a custom plugin. This would be in your main.go file.
package main import ( "github.com/golang/protobuf/proto" "github.com/golang/protobuf/protoc-gen-go/plugin" "github.com/pseudomuto/protokit" "log" ) type plugin struct{} func (p *plugin) Generate(r *plugin_go.CodeGeneratorRequest) (*plugin_go.CodeGeneratorResponse, error) { descriptors := protokit.ParseCodeGenRequest(r) resp := new(plugin_go.CodeGeneratorResponse) for _, desc := range descriptors { resp.File = append(resp.File, &plugin_go.CodeGeneratorResponse_File{ Name: proto.String(desc.GetName() + ".out"), Content: proto.String("Some relevant output"), }) } return resp, nil } // An example of running a custom plugin. This would be in your main.go file. func main() { // in func main() {} if err := protokit.RunPlugin(new(plugin)); err != nil { log.Fatal(err) } }
Output:
Types ¶
type Comment ¶
A Comment describes the leading, trailing, and detached comments for a proto object. See `SourceCodeInfo_Location` in descriptor.proto for details on what those terms mean
func (*Comment) GetDetached ¶
GetDetached returns the detached leading comments
func (*Comment) GetLeading ¶
GetLeading returns the leading comments
func (*Comment) GetTrailing ¶
GetTrailing returns the leading comments
func (*Comment) String ¶
String returns the leading and trailing comments joined by 2 line breaks (`\n\n`). If either are empty, the line breaks are removed.
Example ¶
Join the leading and trailing comments together
package main import ( "fmt" "github.com/pseudomuto/protokit" ) func main() { c := &protokit.Comment{Leading: "Some leading comment", Trailing: "Some trailing comment"} fmt.Println(c.String()) }
Output: Some leading comment Some trailing comment
type Comments ¶
Comments is a map of source location paths to values.
func ParseComments ¶
func ParseComments(fd *descriptor.FileDescriptorProto) Comments
ParseComments parses all comments within a proto file. The locations are encoded into the map by joining the paths with a "." character. E.g. `4.2.3.0`.
Leading/trailing spaces are trimmed for each comment type (leading, trailing, detached)
type Descriptor ¶
type Descriptor struct { *descriptor.DescriptorProto Parent *Descriptor Comments *Comment Enums []*EnumDescriptor Extensions []*ExtensionDescriptor Fields []*FieldDescriptor Messages []*Descriptor // contains filtered or unexported fields }
A Descriptor describes a message
func DescriptorFromContext ¶
func DescriptorFromContext(ctx context.Context) (*Descriptor, bool)
DescriptorFromContext returns the associated `Descriptor` for the context and whether or not it was found
func (*Descriptor) GetComments ¶
func (m *Descriptor) GetComments() *Comment
GetComments returns a description of the message
func (*Descriptor) GetEnum ¶
func (m *Descriptor) GetEnum(name string) *EnumDescriptor
GetEnum returns the enum with the specified name. The name can be either simple, or fully qualified (returns `nil` if not found)
func (*Descriptor) GetEnums ¶
func (m *Descriptor) GetEnums() []*EnumDescriptor
GetEnums returns the nested enumerations within the message
func (*Descriptor) GetExtensions ¶
func (m *Descriptor) GetExtensions() []*ExtensionDescriptor
GetExtensions returns the message-level extensions defined by this message
func (*Descriptor) GetFile ¶
func (c *Descriptor) GetFile() *FileDescriptor
GetFile returns the FileDescriptor that contains this object
func (*Descriptor) GetFullName ¶
func (c *Descriptor) GetFullName() string
GetFullName returns the `LongName` prefixed with the package this object is in
func (*Descriptor) GetLongName ¶
func (c *Descriptor) GetLongName() string
GetLongName returns the name prefixed with the dot-separated parent descriptor's name (if any)
func (*Descriptor) GetMessage ¶
func (m *Descriptor) GetMessage(name string) *Descriptor
GetMessage returns the nested message with the specified name. The name can be simple or fully qualified (returns `nil` if not found)
func (*Descriptor) GetMessageField ¶
func (m *Descriptor) GetMessageField(name string) *FieldDescriptor
GetMessageField returns the field with the specified name (returns `nil` if not found)
func (*Descriptor) GetMessageFields ¶
func (m *Descriptor) GetMessageFields() []*FieldDescriptor
GetMessageFields returns the message fields
func (*Descriptor) GetMessages ¶
func (m *Descriptor) GetMessages() []*Descriptor
GetMessages returns the nested messages within the message
func (*Descriptor) GetPackage ¶
func (c *Descriptor) GetPackage() string
GetPackage returns the package this object is in
func (*Descriptor) GetParent ¶
func (m *Descriptor) GetParent() *Descriptor
GetParent returns the parent descriptor (if any) that defines this descriptor
type EnumDescriptor ¶
type EnumDescriptor struct { *descriptor.EnumDescriptorProto Parent *Descriptor Values []*EnumValueDescriptor Comments *Comment // contains filtered or unexported fields }
An EnumDescriptor describe an enum type
func EnumDescriptorFromContext ¶
func EnumDescriptorFromContext(ctx context.Context) (*EnumDescriptor, bool)
EnumDescriptorFromContext returns the associated `EnumDescriptor` for the context and whether or not it was found
func (*EnumDescriptor) GetComments ¶
func (e *EnumDescriptor) GetComments() *Comment
GetComments returns a description of this enum
func (*EnumDescriptor) GetFile ¶
func (c *EnumDescriptor) GetFile() *FileDescriptor
GetFile returns the FileDescriptor that contains this object
func (*EnumDescriptor) GetFullName ¶
func (c *EnumDescriptor) GetFullName() string
GetFullName returns the `LongName` prefixed with the package this object is in
func (*EnumDescriptor) GetLongName ¶
func (c *EnumDescriptor) GetLongName() string
GetLongName returns the name prefixed with the dot-separated parent descriptor's name (if any)
func (*EnumDescriptor) GetNamedValue ¶
func (e *EnumDescriptor) GetNamedValue(name string) *EnumValueDescriptor
GetNamedValue returns the value with the specified name (returns `nil` if not found)
func (*EnumDescriptor) GetPackage ¶
func (c *EnumDescriptor) GetPackage() string
GetPackage returns the package this object is in
func (*EnumDescriptor) GetParent ¶
func (e *EnumDescriptor) GetParent() *Descriptor
GetParent returns the parent message (if any) that contains this enum
func (*EnumDescriptor) GetValues ¶
func (e *EnumDescriptor) GetValues() []*EnumValueDescriptor
GetValues returns the available values for this enum
type EnumValueDescriptor ¶
type EnumValueDescriptor struct { *descriptor.EnumValueDescriptorProto Enum *EnumDescriptor Comments *Comment // contains filtered or unexported fields }
An EnumValueDescriptor describes an enum value
func (*EnumValueDescriptor) GetComments ¶
func (v *EnumValueDescriptor) GetComments() *Comment
GetComments returns a description of the value
func (*EnumValueDescriptor) GetEnum ¶
func (v *EnumValueDescriptor) GetEnum() *EnumDescriptor
GetEnum returns the parent enumeration that contains this value
func (*EnumValueDescriptor) GetFile ¶
func (c *EnumValueDescriptor) GetFile() *FileDescriptor
GetFile returns the FileDescriptor that contains this object
func (*EnumValueDescriptor) GetFullName ¶
func (c *EnumValueDescriptor) GetFullName() string
GetFullName returns the `LongName` prefixed with the package this object is in
func (*EnumValueDescriptor) GetLongName ¶
func (c *EnumValueDescriptor) GetLongName() string
GetLongName returns the name prefixed with the dot-separated parent descriptor's name (if any)
func (*EnumValueDescriptor) GetPackage ¶
func (c *EnumValueDescriptor) GetPackage() string
GetPackage returns the package this object is in
type ExtensionDescriptor ¶
type ExtensionDescriptor struct { *descriptor.FieldDescriptorProto Parent *Descriptor Comments *Comment // contains filtered or unexported fields }
An ExtensionDescriptor describes a protobuf extension. If it's a top-level extension it's parent will be `nil`
func (*ExtensionDescriptor) GetComments ¶
func (e *ExtensionDescriptor) GetComments() *Comment
GetComments returns a description of the extension
func (*ExtensionDescriptor) GetFile ¶
func (c *ExtensionDescriptor) GetFile() *FileDescriptor
GetFile returns the FileDescriptor that contains this object
func (*ExtensionDescriptor) GetFullName ¶
func (c *ExtensionDescriptor) GetFullName() string
GetFullName returns the `LongName` prefixed with the package this object is in
func (*ExtensionDescriptor) GetLongName ¶
func (c *ExtensionDescriptor) GetLongName() string
GetLongName returns the name prefixed with the dot-separated parent descriptor's name (if any)
func (*ExtensionDescriptor) GetPackage ¶
func (c *ExtensionDescriptor) GetPackage() string
GetPackage returns the package this object is in
func (*ExtensionDescriptor) GetParent ¶
func (e *ExtensionDescriptor) GetParent() *Descriptor
GetParent returns the descriptor that defined this extension (if any)
type FieldDescriptor ¶
type FieldDescriptor struct { *descriptor.FieldDescriptorProto Comments *Comment Message *Descriptor // contains filtered or unexported fields }
A FieldDescriptor describes a message field
func (*FieldDescriptor) GetComments ¶
func (mf *FieldDescriptor) GetComments() *Comment
GetComments returns a description of the field
func (*FieldDescriptor) GetFile ¶
func (c *FieldDescriptor) GetFile() *FileDescriptor
GetFile returns the FileDescriptor that contains this object
func (*FieldDescriptor) GetFullName ¶
func (c *FieldDescriptor) GetFullName() string
GetFullName returns the `LongName` prefixed with the package this object is in
func (*FieldDescriptor) GetLongName ¶
func (c *FieldDescriptor) GetLongName() string
GetLongName returns the name prefixed with the dot-separated parent descriptor's name (if any)
func (*FieldDescriptor) GetMessage ¶
func (mf *FieldDescriptor) GetMessage() *Descriptor
GetMessage returns the descriptor that defines this field
func (*FieldDescriptor) GetPackage ¶
func (c *FieldDescriptor) GetPackage() string
GetPackage returns the package this object is in
type FileDescriptor ¶
type FileDescriptor struct { *descriptor.FileDescriptorProto Comments *Comment // Deprecated: see PackageComments PackageComments *Comment SyntaxComments *Comment Enums []*EnumDescriptor Extensions []*ExtensionDescriptor Imports []*ImportedDescriptor Messages []*Descriptor Services []*ServiceDescriptor OptionExtensions map[string]interface{} // contains filtered or unexported fields }
A FileDescriptor describes a single proto file with all of its messages, enums, services, etc.
func FileDescriptorFromContext ¶
func FileDescriptorFromContext(ctx context.Context) (*FileDescriptor, bool)
FileDescriptorFromContext returns the `FileDescriptor` from the context and whether or not the key was found.
func ParseCodeGenRequest ¶
func ParseCodeGenRequest(req *plugin_go.CodeGeneratorRequest) []*FileDescriptor
ParseCodeGenRequest parses the given request into `FileDescriptor` objects. Only the `req.FilesToGenerate` will be returned.
For example, given the following invocation, only booking.proto will be returned even if it imports other protos:
protoc --plugin=protoc-gen-test=./test -I. protos/booking.proto
func (*FileDescriptor) GetComments
deprecated
func (f *FileDescriptor) GetComments() *Comment
GetComments returns the file's package comments.
Deprecated: please see GetPackageComments
func (*FileDescriptor) GetEnum ¶
func (f *FileDescriptor) GetEnum(name string) *EnumDescriptor
GetEnum returns the enumeration with the specified name (returns `nil` if not found)
func (*FileDescriptor) GetEnums ¶
func (f *FileDescriptor) GetEnums() []*EnumDescriptor
GetEnums returns the top-level enumerations defined in this file
func (*FileDescriptor) GetExtensions ¶
func (f *FileDescriptor) GetExtensions() []*ExtensionDescriptor
GetExtensions returns the top-level (file) extensions defined in this file
func (*FileDescriptor) GetImports ¶
func (f *FileDescriptor) GetImports() []*ImportedDescriptor
GetImports returns the proto files imported by this file
func (*FileDescriptor) GetMessage ¶
func (f *FileDescriptor) GetMessage(name string) *Descriptor
GetMessage returns the message with the specified name (returns `nil` if not found)
func (*FileDescriptor) GetMessages ¶
func (f *FileDescriptor) GetMessages() []*Descriptor
GetMessages returns the top-level messages defined in this file
func (*FileDescriptor) GetPackageComments ¶
func (f *FileDescriptor) GetPackageComments() *Comment
GetPackageComments returns the file's package comments
func (*FileDescriptor) GetService ¶
func (f *FileDescriptor) GetService(name string) *ServiceDescriptor
GetService returns the service with the specified name (returns `nil` if not found)
func (*FileDescriptor) GetServices ¶
func (f *FileDescriptor) GetServices() []*ServiceDescriptor
GetServices returns the services defined in this file
func (*FileDescriptor) GetSyntaxComments ¶
func (f *FileDescriptor) GetSyntaxComments() *Comment
GetSyntaxComments returns the file's syntax comments
func (*FileDescriptor) IsProto3 ¶
func (f *FileDescriptor) IsProto3() bool
IsProto3 returns whether or not this file is a proto3 file
type ImportedDescriptor ¶
type ImportedDescriptor struct {
// contains filtered or unexported fields
}
An ImportedDescriptor describes a type that was imported by a FileDescriptor.
func (*ImportedDescriptor) GetFile ¶
func (c *ImportedDescriptor) GetFile() *FileDescriptor
GetFile returns the FileDescriptor that contains this object
func (*ImportedDescriptor) GetFullName ¶
func (c *ImportedDescriptor) GetFullName() string
GetFullName returns the `LongName` prefixed with the package this object is in
func (*ImportedDescriptor) GetLongName ¶
func (c *ImportedDescriptor) GetLongName() string
GetLongName returns the name prefixed with the dot-separated parent descriptor's name (if any)
func (*ImportedDescriptor) GetPackage ¶
func (c *ImportedDescriptor) GetPackage() string
GetPackage returns the package this object is in
type MethodDescriptor ¶
type MethodDescriptor struct { *descriptor.MethodDescriptorProto Comments *Comment Service *ServiceDescriptor // contains filtered or unexported fields }
A MethodDescriptor describes a method in a service
func (*MethodDescriptor) GetComments ¶
func (m *MethodDescriptor) GetComments() *Comment
GetComments returns a description of the method
func (*MethodDescriptor) GetFile ¶
func (c *MethodDescriptor) GetFile() *FileDescriptor
GetFile returns the FileDescriptor that contains this object
func (*MethodDescriptor) GetFullName ¶
func (c *MethodDescriptor) GetFullName() string
GetFullName returns the `LongName` prefixed with the package this object is in
func (*MethodDescriptor) GetLongName ¶
func (c *MethodDescriptor) GetLongName() string
GetLongName returns the name prefixed with the dot-separated parent descriptor's name (if any)
func (*MethodDescriptor) GetPackage ¶
func (c *MethodDescriptor) GetPackage() string
GetPackage returns the package this object is in
func (*MethodDescriptor) GetService ¶
func (m *MethodDescriptor) GetService() *ServiceDescriptor
GetService returns the service descriptor that defines this method
type Plugin ¶
type Plugin interface {
Generate(req *plugin_go.CodeGeneratorRequest) (*plugin_go.CodeGeneratorResponse, error)
}
Plugin describes an interface for running protoc code generator plugins
type ServiceDescriptor ¶
type ServiceDescriptor struct { *descriptor.ServiceDescriptorProto Comments *Comment Methods []*MethodDescriptor // contains filtered or unexported fields }
A ServiceDescriptor describes a service
func ServiceDescriptorFromContext ¶
func ServiceDescriptorFromContext(ctx context.Context) (*ServiceDescriptor, bool)
ServiceDescriptorFromContext returns the `Service` from the context and whether or not the key was found.
func (*ServiceDescriptor) GetComments ¶
func (s *ServiceDescriptor) GetComments() *Comment
GetComments returns a description of the service
func (*ServiceDescriptor) GetFile ¶
func (c *ServiceDescriptor) GetFile() *FileDescriptor
GetFile returns the FileDescriptor that contains this object
func (*ServiceDescriptor) GetFullName ¶
func (c *ServiceDescriptor) GetFullName() string
GetFullName returns the `LongName` prefixed with the package this object is in
func (*ServiceDescriptor) GetLongName ¶
func (c *ServiceDescriptor) GetLongName() string
GetLongName returns the name prefixed with the dot-separated parent descriptor's name (if any)
func (*ServiceDescriptor) GetMethods ¶
func (s *ServiceDescriptor) GetMethods() []*MethodDescriptor
GetMethods returns the methods for the service
func (*ServiceDescriptor) GetNamedMethod ¶
func (s *ServiceDescriptor) GetNamedMethod(name string) *MethodDescriptor
GetNamedMethod returns the method with the specified name (if found)
func (*ServiceDescriptor) GetPackage ¶
func (c *ServiceDescriptor) GetPackage() string
GetPackage returns the package this object is in