Documentation
¶
Index ¶
- type Comment
- type ConfigOption
- type Declaration
- type Edition
- type EmptyStatement
- type Enum
- type EnumField
- type EnumValueOption
- type Extend
- type Extensions
- type Field
- type FieldOption
- type GroupField
- type HasInlineCommentSetter
- type Import
- type ImportModifier
- type MapField
- type Message
- type Oneof
- type OneofField
- type Option
- type Package
- type Parser
- func (p *Parser) IsEOF() bool
- func (p *Parser) MaybeScanInlineComment(hasSetter HasInlineCommentSetter)
- func (p *Parser) ParseComments() []*Comment
- func (p *Parser) ParseDeclaration() (*Declaration, error)
- func (p *Parser) ParseEdition() (*Edition, error)
- func (p *Parser) ParseEnum() (*Enum, error)
- func (p *Parser) ParseExtend() (*Extend, error)
- func (p *Parser) ParseExtensions() (*Extensions, error)
- func (p *Parser) ParseField() (*Field, error)
- func (p *Parser) ParseGroupField() (*GroupField, error)
- func (p *Parser) ParseImport() (*Import, error)
- func (p *Parser) ParseMapField() (*MapField, error)
- func (p *Parser) ParseMessage() (*Message, error)
- func (p *Parser) ParseOneof() (*Oneof, error)
- func (p *Parser) ParseOption() (*Option, error)
- func (p *Parser) ParsePackage() (*Package, error)
- func (p *Parser) ParseProto() (*Proto, error)
- func (p *Parser) ParseReserved() (*Reserved, error)
- func (p *Parser) ParseService() (*Service, error)
- func (p *Parser) ParseSyntax() (*Syntax, error)
- type Proto
- type ProtoMeta
- type RPC
- type RPCRequest
- type RPCResponse
- type Range
- type Reserved
- type Service
- type Syntax
- type Visitee
- type Visitor
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Comment ¶
type Comment struct {
// Raw includes a comment syntax like // and /* */.
Raw string
// Meta is the meta information.
Meta meta.Meta
}
Comment is a comment in either C/C++-style // and /* ... */ syntax.
type ConfigOption ¶
type ConfigOption func(*Parser)
ConfigOption is an option for Parser.
func WithBodyIncludingComments ¶
func WithBodyIncludingComments(bodyIncludingComments bool) ConfigOption
WithBodyIncludingComments is an option to allow to include comments into each element's body. The comments are remaining of other elements'Comments and InlineComment.
func WithPermissive ¶
func WithPermissive(permissive bool) ConfigOption
WithPermissive is an option to allow the permissive parsing rather than the just documented spec.
type Declaration ¶ added in v4.14.0
type Declaration struct {
Number string
FullName string
Type string
Reserved bool
Repeated bool
// Comments are the optional ones placed at the beginning.
Comments []*Comment
// InlineComment is the optional one placed at the ending.
InlineComment *Comment
// InlineCommentBehindLeftCurly is the optional one placed behind a left curly.
InlineCommentBehindLeftCurly *Comment
// Meta is the meta information.
Meta meta.Meta
}
Declaration is an option of extension ranges.
func (*Declaration) Accept ¶ added in v4.14.0
func (d *Declaration) Accept(v Visitor)
Accept dispatches the call to the visitor.
func (*Declaration) SetInlineComment ¶ added in v4.14.0
func (d *Declaration) SetInlineComment(comment *Comment)
SetInlineComment implements the HasInlineCommentSetter interface.
type Edition ¶ added in v4.12.0
type Edition struct {
Edition string
// EditionQuote includes quotes
EditionQuote string
// Comments are the optional ones placed at the beginning.
Comments []*Comment
// InlineComment is the optional one placed at the ending.
InlineComment *Comment
// Meta is the meta information.
Meta meta.Meta
}
Edition is used to define the protobuf version.
func (*Edition) SetInlineComment ¶ added in v4.12.0
SetInlineComment implements the HasInlineCommentSetter interface.
type EmptyStatement ¶
type EmptyStatement struct {
// InlineComment is the optional one placed at the ending.
InlineComment *Comment
}
EmptyStatement represents ";".
func (*EmptyStatement) Accept ¶
func (e *EmptyStatement) Accept(v Visitor)
Accept dispatches the call to the visitor.
func (*EmptyStatement) SetInlineComment ¶
func (e *EmptyStatement) SetInlineComment(comment *Comment)
SetInlineComment implements the HasInlineCommentSetter interface.
type Enum ¶
type Enum struct {
EnumName string
// EnumBody can have options and enum fields.
// The element of this is the union of an option, enumField, reserved, and emptyStatement.
EnumBody []Visitee
// Comments are the optional ones placed at the beginning.
Comments []*Comment
// InlineComment is the optional one placed at the ending.
InlineComment *Comment
// InlineCommentBehindLeftCurly is the optional one placed behind a left curly.
InlineCommentBehindLeftCurly *Comment
// Meta is the meta information.
Meta meta.Meta
}
Enum consists of a name and an enum body.
func (*Enum) SetInlineComment ¶
SetInlineComment implements the HasInlineCommentSetter interface.
type EnumField ¶
type EnumField struct {
Ident string
Number string
EnumValueOptions []*EnumValueOption
// Comments are the optional ones placed at the beginning.
Comments []*Comment
// InlineComment is the optional one placed at the ending.
InlineComment *Comment
// Meta is the meta information.
Meta meta.Meta
}
EnumField is a field of enum.
func (*EnumField) SetInlineComment ¶
SetInlineComment implements the HasInlineCommentSetter interface.
type EnumValueOption ¶
EnumValueOption is an option of a enumField.
type Extend ¶
type Extend struct {
MessageType string
// ExtendBody can have fields and emptyStatements
ExtendBody []Visitee
// Comments are the optional ones placed at the beginning.
Comments []*Comment
// InlineComment is the optional one placed at the ending.
InlineComment *Comment
// InlineCommentBehindLeftCurly is the optional one placed behind a left curly.
InlineCommentBehindLeftCurly *Comment
// Meta is the meta information.
Meta meta.Meta
}
Extend consists of a messageType and an extend body.
func (*Extend) SetInlineComment ¶
SetInlineComment implements the HasInlineCommentSetter interface.
type Extensions ¶
type Extensions struct {
Ranges []*Range
Declarations []*Declaration
// Comments are the optional ones placed at the beginning.
Comments []*Comment
// InlineComment is the optional one placed at the ending.
InlineComment *Comment
// InlineCommentBehindLeftSquare is the optional one placed behind a left square.
InlineCommentBehindLeftSquare *Comment
// Meta is the meta information.
Meta meta.Meta
}
Extensions declare that a range of field numbers in a message are available for third-party extensions.
func (*Extensions) Accept ¶
func (e *Extensions) Accept(v Visitor)
Accept dispatches the call to the visitor.
func (*Extensions) SetInlineComment ¶
func (e *Extensions) SetInlineComment(comment *Comment)
SetInlineComment implements the HasInlineCommentSetter interface.
type Field ¶
type Field struct {
IsRepeated bool
IsRequired bool // proto2 only
IsOptional bool // proto2 only
Type string
FieldName string
FieldNumber string
FieldOptions []*FieldOption
// Comments are the optional ones placed at the beginning.
Comments []*Comment
// InlineComment is the optional one placed at the ending.
InlineComment *Comment
// Meta is the meta information.
Meta meta.Meta
}
Field is a normal field that is the basic element of a protocol buffer message.
func (*Field) SetInlineComment ¶
SetInlineComment implements the HasInlineCommentSetter interface.
type FieldOption ¶
FieldOption is an option for the field.
type GroupField ¶
type GroupField struct {
IsRepeated bool
IsRequired bool
IsOptional bool
// GroupName must begin with capital letter.
GroupName string
// MessageBody can have fields, nested enum definitions, nested message definitions,
// options, oneofs, map fields, extends, reserved, and extensions statements.
MessageBody []Visitee
FieldNumber string
// Comments are the optional ones placed at the beginning.
Comments []*Comment
// InlineComment is the optional one placed at the ending.
InlineComment *Comment
// InlineCommentBehindLeftCurly is the optional one placed behind a left curly.
InlineCommentBehindLeftCurly *Comment
// Meta is the meta information.
Meta meta.Meta
}
GroupField is one way to nest information in message definitions. proto2 only.
func (*GroupField) Accept ¶
func (f *GroupField) Accept(v Visitor)
Accept dispatches the call to the visitor.
func (*GroupField) SetInlineComment ¶
func (f *GroupField) SetInlineComment(comment *Comment)
SetInlineComment implements the HasInlineCommentSetter interface.
type HasInlineCommentSetter ¶
type HasInlineCommentSetter interface {
SetInlineComment(comment *Comment)
}
HasInlineCommentSetter requires to have a setter for an InlineComment field.
type Import ¶
type Import struct {
Modifier ImportModifier
Location string
// Comments are the optional ones placed at the beginning.
Comments []*Comment
// InlineComment is the optional one placed at the ending.
InlineComment *Comment
// Meta is the meta information.
Meta meta.Meta
}
Import is used to import another .proto's definitions.
func (*Import) SetInlineComment ¶
SetInlineComment implements the HasInlineCommentSetter interface.
type ImportModifier ¶
type ImportModifier uint
ImportModifier is a modifier enum type for import behavior.
const ( ImportModifierNone ImportModifier = iota ImportModifierPublic ImportModifierWeak )
Optional import modifier value to change a default behavior.
type MapField ¶
type MapField struct {
KeyType string
Type string
MapName string
FieldNumber string
FieldOptions []*FieldOption
// Comments are the optional ones placed at the beginning.
Comments []*Comment
// InlineComment is the optional one placed at the ending.
InlineComment *Comment
// Meta is the meta information.
Meta meta.Meta
}
MapField is an associative map.
func (*MapField) SetInlineComment ¶
SetInlineComment implements the HasInlineCommentSetter interface.
type Message ¶
type Message struct {
MessageName string
// MessageBody can have fields, nested enum definitions, nested message definitions,
// options, oneofs, map fields, group fields(proto2 only), extends, reserved, and extensions(proto2 only) statements.
MessageBody []Visitee
// Comments are the optional ones placed at the beginning.
Comments []*Comment
// InlineComment is the optional one placed at the ending.
InlineComment *Comment
// InlineCommentBehindLeftCurly is the optional one placed behind a left curly.
InlineCommentBehindLeftCurly *Comment
// Meta is the meta information.
Meta meta.Meta
}
Message consists of a message name and a message body.
func (*Message) SetInlineComment ¶
SetInlineComment implements the HasInlineCommentSetter interface.
type Oneof ¶
type Oneof struct {
OneofFields []*OneofField
OneofName string
Options []*Option
// Comments are the optional ones placed at the beginning.
Comments []*Comment
// InlineComment is the optional one placed at the ending.
InlineComment *Comment
// InlineCommentBehindLeftCurly is the optional one placed behind a left curly.
InlineCommentBehindLeftCurly *Comment
// Meta is the meta information.
Meta meta.Meta
}
Oneof consists of oneof fields and a oneof name.
func (*Oneof) SetInlineComment ¶
SetInlineComment implements the HasInlineCommentSetter interface.
type OneofField ¶
type OneofField struct {
Type string
FieldName string
FieldNumber string
FieldOptions []*FieldOption
// Comments are the optional ones placed at the beginning.
Comments []*Comment
// InlineComment is the optional one placed at the ending.
InlineComment *Comment
// Meta is the meta information.
Meta meta.Meta
}
OneofField is a constituent field of oneof.
func (*OneofField) Accept ¶
func (f *OneofField) Accept(v Visitor)
Accept dispatches the call to the visitor.
func (*OneofField) SetInlineComment ¶
func (f *OneofField) SetInlineComment(comment *Comment)
SetInlineComment implements the HasInlineCommentSetter interface.
type Option ¶
type Option struct {
OptionName string
Constant string
// Comments are the optional ones placed at the beginning.
Comments []*Comment
// InlineComment is the optional one placed at the ending.
InlineComment *Comment
// Meta is the meta information.
Meta meta.Meta
}
Option can be used in proto files, messages, enums and services.
func (*Option) SetInlineComment ¶
SetInlineComment implements the HasInlineCommentSetter interface.
type Package ¶
type Package struct {
Name string
// Comments are the optional ones placed at the beginning.
Comments []*Comment
// InlineComment is the optional one placed at the ending.
InlineComment *Comment
// Meta is the meta information.
Meta meta.Meta
}
Package can be used to prevent name clashes between protocol message types.
func (*Package) SetInlineComment ¶
SetInlineComment implements the HasInlineCommentSetter interface.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser is a parser.
func NewParser ¶
func NewParser(lex *lexer.Lexer, opts ...ConfigOption) *Parser
NewParser creates a new Parser.
func (*Parser) MaybeScanInlineComment ¶
func (p *Parser) MaybeScanInlineComment( hasSetter HasInlineCommentSetter, )
MaybeScanInlineComment tries to scan a comment on the current line. If present then set it with setter.
func (*Parser) ParseComments ¶
ParseComments parsers a sequence of comments.
comments = { comment }
See https://developers.google.com/protocol-buffers/docs/proto3#adding-comments
func (*Parser) ParseDeclaration ¶ added in v4.14.0
func (p *Parser) ParseDeclaration() (*Declaration, error)
ParseDeclaration parses a declaration.
declaration = "declaration" "=" "{"
"number" ":" number ","
"full_name" ":" string ","
"type" ":" string ","
"repeated" ":" bool ","
"reserved" ":" bool
"}"
See https://protobuf.dev/programming-guides/extension_declarations/
func (*Parser) ParseEdition ¶ added in v4.12.0
ParseEdition parses the Edition.
edition = "edition" "=" [ ( "'" decimalLit "'" ) | ( '"' decimalLit '"' ) ] ";"
See https://protobuf.dev/reference/protobuf/edition-2023-spec/#edition
func (*Parser) ParseEnum ¶
ParseEnum parses the enum.
enum = "enum" enumName enumBody
See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#enum_definition
func (*Parser) ParseExtend ¶
ParseExtend parses the extend. Note that group is not supported.
extend = "extend" messageType "{" {field | group | emptyStatement} "}"
See https://developers.google.com/protocol-buffers/docs/reference/proto2-spec#extend
func (*Parser) ParseExtensions ¶
func (p *Parser) ParseExtensions() (*Extensions, error)
ParseExtensions parses the extensions.
extensions = "extensions" ranges ";"
See https://developers.google.com/protocol-buffers/docs/reference/proto2-spec#extensions
func (*Parser) ParseField ¶
ParseField parses the field.
field = [ "repeated" ] type fieldName "=" fieldNumber [ "[" fieldOptions "]" ] ";" field = [ "required" | "optional" | "repeated" ] type fieldName "=" fieldNumber [ "[" fieldOptions "]" ] ";"
See
https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#normal_field https://developers.google.com/protocol-buffers/docs/reference/proto2-spec#normal_field
func (*Parser) ParseGroupField ¶
func (p *Parser) ParseGroupField() (*GroupField, error)
ParseGroupField parses the group.
group = label "group" groupName "=" fieldNumber messageBody
See https://developers.google.com/protocol-buffers/docs/reference/proto2-spec#group_field
func (*Parser) ParseImport ¶
ParseImport parses the import.
import = "import" [ "weak" | "public" ] strLit ";"
See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#import_statement
func (*Parser) ParseMapField ¶
ParseMapField parses the mapField.
mapField = "map" "<" keyType "," type ">" mapName "=" fieldNumber [ "[" fieldOptions "]" ] ";"
See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#map_field
func (*Parser) ParseMessage ¶
ParseMessage parses the message.
message = "message" messageName messageBody
See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#message_definition
func (*Parser) ParseOneof ¶
ParseOneof parses the oneof.
oneof = "oneof" oneofName "{" { option | oneofField | emptyStatement } "}"
See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#oneof_and_oneof_field
func (*Parser) ParseOption ¶
ParseOption parses the option.
option = "option" optionName "=" constant ";"
See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#option
func (*Parser) ParsePackage ¶
ParsePackage parses the package.
package = "package" fullIdent ";"
See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#package
func (*Parser) ParseProto ¶
ParseProto parses the proto.
proto = [syntax] [edition] { import | package | option | topLevelDef | emptyStatement }
See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#proto_file See https://protobuf.dev/reference/protobuf/edition-2023-spec/#proto_file
func (*Parser) ParseReserved ¶
ParseReserved parses the reserved.
reserved = "reserved" ( ranges | fieldNames ) ";" reserved = "reserved" ( ranges | reservedIdent ) ";"
See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#reserved See https://protobuf.dev/reference/protobuf/edition-2023-spec/#reserved
func (*Parser) ParseService ¶
ParseService parses the service.
service = "service" serviceName "{" { option | rpc | emptyStatement } "}"
See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#service_definition
func (*Parser) ParseSyntax ¶
ParseSyntax parses the syntax.
syntax = "syntax" "=" quote "proto3" quote ";" syntax = "syntax" "=" quote "proto2" quote ";"
See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#syntax
type Proto ¶
type Proto struct {
Syntax *Syntax
Edition *Edition
// ProtoBody is a slice of sum type consisted of *Import, *Package, *Option, *Message, *Enum, *Service, *Extend and *EmptyStatement.
ProtoBody []Visitee
Meta *ProtoMeta
}
Proto represents a protocol buffer definition.
type ProtoMeta ¶
type ProtoMeta struct {
// Filename is a name of file, if any.
Filename string
}
ProtoMeta represents a meta information about the Proto.
type RPC ¶
type RPC struct {
RPCName string
RPCRequest *RPCRequest
RPCResponse *RPCResponse
Options []*Option
// Comments are the optional ones placed at the beginning.
Comments []*Comment
// InlineComment is the optional one placed at the ending.
InlineComment *Comment
// InlineCommentBehindLeftCurly is the optional one placed behind a left curly.
InlineCommentBehindLeftCurly *Comment
// EmbeddedComments are the optional ones placed between the start position and the position before left curly.
EmbeddedComments []*Comment
// Meta is the meta information.
Meta meta.Meta
}
RPC is a Remote Procedure Call.
func (*RPC) SetInlineComment ¶
SetInlineComment implements the HasInlineCommentSetter interface.
type RPCRequest ¶
type RPCRequest struct {
IsStream bool
MessageType string
// Meta is the meta information.
Meta meta.Meta
}
RPCRequest is a request of RPC.
type RPCResponse ¶
type RPCResponse struct {
IsStream bool
MessageType string
// Meta is the meta information.
Meta meta.Meta
}
RPCResponse is a response of RPC.
type Reserved ¶
type Reserved struct {
Ranges []*Range
FieldNames []string
// Comments are the optional ones placed at the beginning.
Comments []*Comment
// InlineComment is the optional one placed at the ending.
InlineComment *Comment
// Meta is the meta information.
Meta meta.Meta
}
Reserved declares a range of field numbers or field names that cannot be used in this message. These component Ranges and FieldNames are mutually exclusive.
func (*Reserved) SetInlineComment ¶
SetInlineComment implements the HasInlineCommentSetter interface.
type Service ¶
type Service struct {
ServiceName string
// ServiceBody can have options and rpcs.
ServiceBody []Visitee
// Comments are the optional ones placed at the beginning.
Comments []*Comment
// InlineComment is the optional one placed at the ending.
InlineComment *Comment
// InlineCommentBehindLeftCurly is the optional one placed behind a left curly.
InlineCommentBehindLeftCurly *Comment
// Meta is the meta information.
Meta meta.Meta
}
Service consists of RPCs.
func (*Service) SetInlineComment ¶
SetInlineComment implements the HasInlineCommentSetter interface.
type Syntax ¶
type Syntax struct {
ProtobufVersion string
// ProtobufVersionQuote includes quotes
ProtobufVersionQuote string
// Comments are the optional ones placed at the beginning.
Comments []*Comment
// InlineComment is the optional one placed at the ending.
InlineComment *Comment
// Meta is the meta information.
Meta meta.Meta
}
Syntax is used to define the protobuf version.
func (*Syntax) SetInlineComment ¶
SetInlineComment implements the HasInlineCommentSetter interface.
type Visitee ¶
type Visitee interface {
Accept(v Visitor)
}
Visitee is implemented by all Protocol Buffer elements.
type Visitor ¶
type Visitor interface {
VisitComment(*Comment)
VisitDeclaration(*Declaration) (next bool)
VisitEdition(*Edition) (next bool)
VisitEmptyStatement(*EmptyStatement) (next bool)
VisitEnum(*Enum) (next bool)
VisitEnumField(*EnumField) (next bool)
VisitExtend(*Extend) (next bool)
VisitExtensions(*Extensions) (next bool)
VisitField(*Field) (next bool)
VisitGroupField(*GroupField) (next bool)
VisitImport(*Import) (next bool)
VisitMapField(*MapField) (next bool)
VisitMessage(*Message) (next bool)
VisitOneof(*Oneof) (next bool)
VisitOneofField(*OneofField) (next bool)
VisitOption(*Option) (next bool)
VisitPackage(*Package) (next bool)
VisitReserved(*Reserved) (next bool)
VisitRPC(*RPC) (next bool)
VisitService(*Service) (next bool)
VisitSyntax(*Syntax) (next bool)
}
Visitor is for dispatching Protocol Buffer elements.