protoutil

package
v0.27.2 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package protoutil wraps proto structs to allow easier creation, protobuf lang is small enough to easily allow this.

Index

Constants

View Source
const (
	KindWeak   = "weak"
	KindPublic = "public"
)

Values for the kind of import.

Variables

This section is empty.

Functions

func AddAfterPackage

func AddAfterPackage(f *proto.Proto, v proto.Visitee) error

AddAfterPackage tries to add the given Visitee after the 'package' statement. If no package statement is found, returns an error.

func AddAfterSyntax

func AddAfterSyntax(f *proto.Proto, v proto.Visitee) error

AddAfterSyntax tries to add the given Visitee after the 'syntax' statement. If no syntax statement is found, returns an error.

func AddImports

func AddImports(f *proto.Proto, fallback bool, imports ...*proto.Import) (err error)

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

func Append(n proto.Visitee, elems ...proto.Visitee)

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

func Apply(root proto.Visitee, pre, post ApplyFunc) (result proto.Visitee)

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

func AttachComment(n proto.Visitee, comment string)

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

func GetImportByPath(f *proto.Proto, path string) (node *proto.Import, err error)

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

func GetMessageByName(f *proto.Proto, name string) (node *proto.Message, err error)

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

func GetServiceByName(f *proto.Proto, name string) (node *proto.Service, err error)

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

func HasImport(f *proto.Proto, path string) bool

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

func HasMessage(f *proto.Proto, name string) bool

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

func HasService(f *proto.Proto, name string) bool

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

func NewLiteral(lit string) *proto.Literal

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

func NewPackage(path string) *proto.Package

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

func NextUniqueID(m *proto.Message) int

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

func ParseProtoFile(r io.Reader) (*proto.Proto, error)

ParseProtoFile parses the given file.

func ParseProtoPath

func ParseProtoPath(path string) (pf *proto.Proto, err error)

ParseProtoPath opens the file denoted by path and parses it into a proto file.

func Print

func Print(pf *proto.Proto) string

Print formats the proto file using proto-contrib/pkg/protofmt. This does have certain opinions on how formatting is done.

Types

type ApplyFunc

type ApplyFunc func(*Cursor) bool

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

func (c *Cursor) Index() int

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

func (c *Cursor) InsertAfter(n proto.Visitee)

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

func (c *Cursor) InsertBefore(n proto.Visitee)

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

func (c *Cursor) IsLast() bool

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

func (c *Cursor) Name() string

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

func (c *Cursor) Next() (proto.Visitee, bool)

Next returns the next Visitee. Can be used to check the next value before deciding to continue.

func (*Cursor) Node

func (c *Cursor) Node() proto.Visitee

Node returns the current Node.

func (*Cursor) Parent

func (c *Cursor) Parent() proto.Visitee

Parent returns the parent of the current Node.

func (*Cursor) Replace

func (c *Cursor) Replace(n proto.Visitee)

Replace replaces the current Node with n. The replacement node is not walked by Apply.

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 Optional

func Optional() FieldSpecOptions

Optional marks the field as optional.

func Repeated

func Repeated() FieldSpecOptions

Repeated marks the field as repeated.

func Required

func Required() FieldSpecOptions

Required marks the field as required.

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 Custom

func Custom() OptionSpecOptions

Custom denotes the option as being a custom option.

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 StreamRequest

func StreamRequest() RPCSpecOptions

StreamRequest marks request as streaming.

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.

Jump to

Keyboard shortcuts

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