Documentation ¶
Overview ¶
Package protoutil wraps proto structs to allow easier creation, protobuf lang is small enough to easily allow this.
Index ¶
- Constants
- func AddAfterPackage(f *proto.Proto, v proto.Visitee) error
- func AddAfterSyntax(f *proto.Proto, v proto.Visitee) error
- func AddImports(f *proto.Proto, fallback bool, imports ...*proto.Import) (err error)
- func Append(n proto.Visitee, elems ...proto.Visitee)
- func Apply(root proto.Visitee, pre, post ApplyFunc) (result proto.Visitee)
- func AttachComment(n proto.Visitee, comment string)
- func GetImportByPath(f *proto.Proto, path string) (node *proto.Import, err error)
- func GetMessageByName(f *proto.Proto, name string) (node *proto.Message, err error)
- func GetServiceByName(f *proto.Proto, name string) (node *proto.Service, err error)
- func HasImport(f *proto.Proto, path string) bool
- func HasMessage(f *proto.Proto, name string) bool
- func HasService(f *proto.Proto, name string) bool
- func NewEnum(name string, opts ...EnumSpecOpts) *proto.Enum
- func NewEnumField(name string, value int, opts ...EnumFieldSpecOptions) *proto.EnumField
- func NewField(name, typename string, sequence int, opts ...FieldSpecOptions) *proto.NormalField
- func NewImport(path string, opts ...ImportSpecOptions) *proto.Import
- func NewLiteral(lit string) *proto.Literal
- func NewMessage(name string, opts ...MessageSpecOptions) *proto.Message
- func NewOneof(name string, opts ...OneofSpecOptions) *proto.Oneof
- func NewOneofField(name, typename string, sequence int, opts ...OneofFieldOptions) *proto.OneOfField
- func NewOption(name, constant string, opts ...OptionSpecOptions) *proto.Option
- func NewPackage(path string) *proto.Package
- func NewRPC(name, inputType, outputType string, opts ...RPCSpecOptions) *proto.RPC
- func NewService(name string, opts ...ServiceSpecOptions) *proto.Service
- func NextUniqueID(m *proto.Message) int
- func ParseProtoFile(r io.Reader) (*proto.Proto, error)
- func ParseProtoPath(path string) (pf *proto.Proto, err error)
- func Print(pf *proto.Proto) string
- type ApplyFunc
- type Cursor
- func (c *Cursor) Index() int
- func (c *Cursor) InsertAfter(n proto.Visitee)
- func (c *Cursor) InsertBefore(n proto.Visitee)
- func (c *Cursor) IsLast() bool
- func (c *Cursor) Name() string
- func (c *Cursor) Next() (proto.Visitee, bool)
- func (c *Cursor) Node() proto.Visitee
- func (c *Cursor) Parent() proto.Visitee
- func (c *Cursor) Replace(n proto.Visitee)
- type EnumFieldSpec
- type EnumFieldSpecOptions
- type EnumSpec
- type EnumSpecOpts
- type FieldSpec
- type FieldSpecOptions
- type ImportSpec
- type ImportSpecOptions
- type MessageSpec
- type MessageSpecOptions
- type OneofFieldOptions
- type OneofFieldSpec
- type OneofSpec
- type OneofSpecOptions
- type OptionSpec
- type OptionSpecOptions
- type RPCSpec
- type RPCSpecOptions
- type ServiceSpec
- type ServiceSpecOptions
Constants ¶
const ( KindWeak = "weak" KindPublic = "public" )
Values for the kind of import.
Variables ¶
This section is empty.
Functions ¶
func AddAfterPackage ¶
AddAfterPackage tries to add the given Visitee after the 'package' statement. If no package statement is found, returns an error.
func AddAfterSyntax ¶
AddAfterSyntax tries to add the given Visitee after the 'syntax' statement. If no syntax statement is found, returns an error.
func AddImports ¶
AddImports attempts to add the given import *after* any other imports in the file.
If fallback is supplied, attempts to add it after the 'package' statement and then the 'syntax' statement are made.
If none of the attempts are successful, returns an error.
func Append ¶
Append appends the elements provided to the node `n`. `n` must be a node that can accept elements, such as a proto.File or a proto.Message.
Append panics if `n` is not a node that can accept elements or if the type of the elements provided is not compatible with the type of the elements contained by `n`. (Basically, this applies to NormalFields and OneOfFields,).
func Apply ¶
Apply traverses a syntax tree recursively, starting with root, and calling pre and post for each node as described below. Apply returns the syntax tree, possibly modified.
If pre is not nil, it is called for each node before the node's children are traversed (pre-order). If pre returns false, no children are traversed, and post is not called for that node.
If post is not nil, and a prior call of pre didn't return false, post is called for each node after its children are traversed (post-order). If post returns false, traversal is terminated and Apply returns immediately.
func AttachComment ¶
AttachComment attaches a comment top level nodes. Currently only supports Messages, RPC's and Services. Silently ignores other nodes though they can easily be added by just appending a new case to the switch statement.
func GetImportByPath ¶
GetImportByPath returns the import with the given path or nil if not found. Only traverses in proto.Proto since it is the only node that contain imports:
f, _ := ParseProtoPath("foo.proto") s := GetImportByPath(f, "other.proto") s.FileName // "other.proto"
func GetMessageByName ¶
GetMessageByName returns the message with the given name or nil if not found. Only traverses in proto.Proto and proto.Message since they are the only nodes that contain messages:
f, _ := ParseProtoPath("foo.proto") m := GetMessageByName(f, "Foo") m.Name // "Foo"
func GetServiceByName ¶
GetServiceByName returns the service with the given name or nil if not found. Only traverses in proto.Proto since it is the only node that contain services:
f, _ := ParseProtoPath("foo.proto") s := GetServiceByName(f, "FooSrv") s.Name // "FooSrv"
func HasImport ¶
HasImport returns true if the given import (by path) is found in the given file.
f, _ := ParseProtoPath("foo.proto") // true if 'foo.proto' contains import "path.to.other.proto" r := HasImport(f, "path.to.other.proto")
func HasMessage ¶
HasMessage returns true if the given message is found in the given file.
f, _ := ParseProtoPath("foo.proto") // true if 'foo.proto' contains message Foo { ... } r := HasMessage(f, "Foo")
func HasService ¶
HasService returns true if the given service is found in the given file.
f, _ := ParseProtoPath("foo.proto") // true if 'foo.proto' contains service FooSrv { ... } r := HasService(f, "FooSrv")
func NewEnum ¶
func NewEnum(name string, opts ...EnumSpecOpts) *proto.Enum
NewEnum creates a new enum statement node:
// enum Foo { // BAR = 1; // } enum := NewEnum("Foo", WithEnumFields(NewEnumField("BAR", 1)))
No options are attached by default, use WithEnumOptions to add them as required:
// enum Foo { // BAR = 1 [option (foo) = 1]; // } enum := NewEnum("Foo", WithEnumOptions(NewOption("foo", "1")), WithEnumFields(NewEnumField("BAR", 1)))
By default, options are added first, then fields.
func NewEnumField ¶
func NewEnumField(name string, value int, opts ...EnumFieldSpecOptions) *proto.EnumField
NewEnumField creates a new enum field statement node:
// BAR = 1; field := NewEnumField("BAR", 1)
No options are attached by default, use WithEnumFieldOptions to add them as required:
// BAR = 1 [option (foo) = 1]; field := NewEnumField("BAR", 1, WithEnumFieldOptions(NewOption("foo", "1")))
func NewField ¶
func NewField(name, typename string, sequence int, opts ...FieldSpecOptions) *proto.NormalField
NewField creates a new field statement node:
// int32 Foo = 1; field := NewField("Foo", "int32", 1)
Fields aren't marked as repeated, required or optional. Use Repeated, Optional and Required to mark the field as such.
// repeated int32 Foo = 1; field := NewField("Foo", "int32", 1, Repeated())
func NewImport ¶
func NewImport(path string, opts ...ImportSpecOptions) *proto.Import
NewImport creates a new import statement node:
// import "myproto.proto"; imp := NewImport("myproto.proto")
By default, no kind is assigned to it, by using Weak or Public, this can be specified:
// import weak "myproto.proto"; imp := NewImport("myproto.proto", Weak())
func NewLiteral ¶
NewLiteral creates a new Literal:
// true l := NewLiteral("true")
// 1 l := NewLiteral("1")
// "foo" l := NewLiteral("foo")
Currently doesn't support creating compound literals (arrays/maps).
func NewMessage ¶
func NewMessage(name string, opts ...MessageSpecOptions) *proto.Message
NewMessage creates a new message statement node:
// message Foo {} message := NewMessage("Foo")
No fields/enums/options are attached by default, use WithMessageFields, WithEnums, and WithMessageOptions to add them as required:
// message Foo { // option (foo) = 1; // int32 Bar = 1; // } opt := NewOption("foo", "1") field := NewField("int32", "Bar", 1) message := NewMessage("Foo", WithMessageOptions(opt), WithFields(field))
By default, options are added first, then fields and then enums.
func NewOneof ¶
func NewOneof(name string, opts ...OneofSpecOptions) *proto.Oneof
NewOneof creates a new oneof statement node:
// oneof Foo { // int32 Foo = 1; // } oneof := NewOneof("Foo", WithOneOfFields(NewOneOfField("Foo", "int32", 1)))
No options are attached by default, use WithOneOfOptions to add them as required.
func NewOneofField ¶
func NewOneofField(name, typename string, sequence int, opts ...OneofFieldOptions) *proto.OneOfField
NewOneofField creates a new oneof field statement node:
// Needs to placed in oneof block. // int32 Foo = 1; field := NewOneofField("Foo", "int32", 1)
Additional options can be created and attached to the field to the field via WithOneOfFieldOptions:
// int32 Foo = 1 [option (foo) = 1]; field := NewOneofField("Foo", "int32", 1, WithOneOfFieldOptions(NewOption("foo", "1")))
func NewOption ¶
func NewOption(name, constant string, opts ...OptionSpecOptions) *proto.Option
NewOption creates a new option statement node:
// option foo = 1; opt := NewOption("foo", "1")
Custom options can be marked as such by using Custom, this wraps the option name in parenthesis:
// option (foo) = 1; opt := NewOption("foo", "1", Custom())
Since option constants can accept a number of types, strings that require quotation should be passed as raw strings:
// option foo = "bar"; opt := NewOption("foo", `bar`)
func NewPackage ¶
NewPackage creates a new package statement node:
// package foo.bar; pkg := NewPackage("foo.bar")
func NewRPC ¶
func NewRPC(name, inputType, outputType string, opts ...RPCSpecOptions) *proto.RPC
NewRPC creates a new RPC statement node:
// rpc Foo(Bar) returns(Bar) {} rpc := NewRPC("Foo", "Bar", "Bar")
No options are attached by default, use WithRPCOptions to add options as required:
// rpc Foo(Bar) returns(Bar) { // option (foo) = 1; // } rpc := NewRPC("Foo", "Bar", "Bar", WithRPCOptions(NewOption("foo", "1")))
func NewService ¶
func NewService(name string, opts ...ServiceSpecOptions) *proto.Service
NewService creates a new service statement node:
// service Foo {} service := NewService("Foo")
No rpcs/options are attached by default, use WithRPCs and WithServiceOptions to add them as required:
// service Foo { // option (foo) = 1; // rpc Bar(Bar) returns (Bar) {} // } opt := NewOption("foo", "1") rpc := NewRPC("Bar", "Bar", "Bar") service := NewService("Foo", WithServiceOptions(opt), WithRPCs(rpc))
By default, options are added first and then the rpcs.
func NextUniqueID ¶
NextUniqueID goes through the fields of the given Message and returns an id > max(fieldIds). It does not try to 'plug the holes' by selecting the least available id.
// In 'example.proto' file syntax = "proto3" message Hello { string g = 1; string foo = 2; int32 bar = 3; int64 baz = 5; } f := ParseProtoPath("example.proto") m := GetMessageByName(f, "Hello") NextUniqueID(m) // 6
func ParseProtoFile ¶
ParseProtoFile parses the given file.
func ParseProtoPath ¶
ParseProtoPath opens the file denoted by path and parses it into a proto file.
Types ¶
type ApplyFunc ¶
An ApplyFunc is invoked by Apply for each Visitee n, even if n is nil, before and/or after the node's children, using a Cursor describing the current node and providing operations on it.
The return value of ApplyFunc controls the syntax tree traversal. See Apply for details.
type Cursor ¶
type Cursor struct {
// contains filtered or unexported fields
}
A Cursor describes a node encountered during Apply. Information about the node and its parent is available from the Node, Parent, Name, and Index methods.
func (*Cursor) Index ¶
Index reports the index >= 0 of the current Visitee in the slice of Visitees that contains it, or a value < 0 if the current Visitee is not part of a slice. The index of the current node changes if InsertBefore is called while processing the current node.
func (*Cursor) InsertAfter ¶
InsertAfter inserts n after the current Node in its containing slice. If the current Node is not part of a slice, InsertAfter panics. Apply does not walk n.
func (*Cursor) InsertBefore ¶
InsertBefore inserts n before the current Node in its containing slice. If the current Node is not part of a slice, InsertBefore panics. Apply will not walk n.
func (*Cursor) IsLast ¶
IsLast returns if the current node being traversed is the final node in the slice of nodes. Can be used to determine if a node is the last one.
func (*Cursor) Name ¶
Name returns the name of the parent Node field that contains the current Node. If the parent is a *ast.Package and the current Node is a *ast.File, Name returns the filename for the current Node.
func (*Cursor) Next ¶
Next returns the next Visitee. Can be used to check the next value before deciding to continue.
type EnumFieldSpec ¶
type EnumFieldSpec struct {
// contains filtered or unexported fields
}
EnumFieldSpec holds information relevant to the enum field statement.
type EnumFieldSpecOptions ¶
type EnumFieldSpecOptions func(f *EnumFieldSpec)
EnumFieldSpecOptions is a type alias for a callable accepting an EnumFieldSpec.
func WithEnumFieldOptions ¶
func WithEnumFieldOptions(options ...*proto.Option) EnumFieldSpecOptions
WithEnumFieldOptions adds options to the enum field.
type EnumSpec ¶
type EnumSpec struct {
// contains filtered or unexported fields
}
EnumSpec holds information relevant to the enum statement.
type EnumSpecOpts ¶
type EnumSpecOpts func(i *EnumSpec)
EnumSpecOpts is a type alias for a callable accepting an EnumSpec.
func WithEnumFields ¶
func WithEnumFields(fields ...*proto.EnumField) EnumSpecOpts
WithEnumFields adds fields to the enum.
func WithEnumOptions ¶
func WithEnumOptions(options ...*proto.Option) EnumSpecOpts
WithEnumOptions adds options to the enum.
type FieldSpec ¶
type FieldSpec struct {
// contains filtered or unexported fields
}
FieldSpec holds information relevant to the field statement.
type FieldSpecOptions ¶
type FieldSpecOptions func(f *FieldSpec)
FieldSpecOptions is a type alias for a callable accepting a FieldSpec.
func WithFieldOptions ¶
func WithFieldOptions(options ...*proto.Option) FieldSpecOptions
WithFieldOptions adds options to the field.
type ImportSpec ¶
type ImportSpec struct {
// contains filtered or unexported fields
}
ImportSpec holds information relevant to the import statement.
type ImportSpecOptions ¶
type ImportSpecOptions func(i *ImportSpec)
ImportSpecOptions is a type alias for a callable accepting an ImportSpec.
func Public ¶
func Public() ImportSpecOptions
Public allows you to set the kind of the import statement to 'public'.
func Weak ¶
func Weak() ImportSpecOptions
Weak allows you to set the kind of the import statement to 'weak'.
type MessageSpec ¶
type MessageSpec struct {
// contains filtered or unexported fields
}
MessageSpec holds information relevant to the message statement.
type MessageSpecOptions ¶
type MessageSpecOptions func(i *MessageSpec)
MessageSpecOptions is a type alias for a callable accepting a MessageSpec.
func Extend ¶
func Extend() MessageSpecOptions
func WithEnums ¶
func WithEnums(enum ...*proto.Enum) MessageSpecOptions
WithEnums adds enums to the message.
func WithFields ¶
func WithFields(fields ...*proto.NormalField) MessageSpecOptions
WithFields adds fields to the message.
func WithMessageOptions ¶
func WithMessageOptions(options ...*proto.Option) MessageSpecOptions
WithMessageOptions adds options to the message.
type OneofFieldOptions ¶
type OneofFieldOptions func(f *OneofFieldSpec)
OneofFieldOptions is a type alias for a callable accepting a OneOfField.
func WithOneofFieldOptions ¶
func WithOneofFieldOptions(options ...*proto.Option) OneofFieldOptions
WithOneofFieldOptions adds options to the oneof field.
type OneofFieldSpec ¶
type OneofFieldSpec struct {
// contains filtered or unexported fields
}
OneofFieldSpec holds information relevant to the oneof field statement.
type OneofSpec ¶
type OneofSpec struct {
// contains filtered or unexported fields
}
OneofSpec holds information relevant to the enum statement.
type OneofSpecOptions ¶
type OneofSpecOptions func(o *OneofSpec)
OneofSpecOptions is a type alias for a callable accepting a OneOfSpec.
func WithOneofFields ¶
func WithOneofFields(fields ...*proto.OneOfField) OneofSpecOptions
WithOneofFields adds fields to the oneof.
func WithOneofOptions ¶
func WithOneofOptions(options ...*proto.Option) OneofSpecOptions
WithOneofOptions adds options to the oneof.
type OptionSpec ¶
type OptionSpec struct {
// contains filtered or unexported fields
}
OptionSpec holds information relevant to the option statement.
type OptionSpecOptions ¶
type OptionSpecOptions func(o *OptionSpec)
OptionSpecOptions is a function that accepts an OptionSpec.
func SetField ¶
func SetField(name string) OptionSpecOptions
SetField allows setting specific fields for a given option that denotes a type with fields.
// option (my_opt).field = "Value"; opt := NewOption("my_opt", "Value", Custom(), Setter("field"))
type RPCSpec ¶
type RPCSpec struct {
// contains filtered or unexported fields
}
RPCSpec holds information relevant to the rpc statement.
type RPCSpecOptions ¶
type RPCSpecOptions func(i *RPCSpec)
RPCSpecOptions is a type alias for a callable accepting an RPCSpec.
func StreamResponse ¶
func StreamResponse() RPCSpecOptions
StreamResponse marks response as streaming.
func WithRPCOptions ¶
func WithRPCOptions(option ...*proto.Option) RPCSpecOptions
WithRPCOptions adds options to the RPC.
type ServiceSpec ¶
type ServiceSpec struct {
// contains filtered or unexported fields
}
ServiceSpec holds information relevant to the service statement.
type ServiceSpecOptions ¶
type ServiceSpecOptions func(i *ServiceSpec)
ServiceSpecOptions is a type alias for a callable accepting a ServiceSpec.
func WithRPCs ¶
func WithRPCs(rpcs ...*proto.RPC) ServiceSpecOptions
WithRPCs adds rpcs to the service.
func WithServiceOptions ¶
func WithServiceOptions(options ...*proto.Option) ServiceSpecOptions
WithServiceOptions adds options to the service.