proto

package module
Version: v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2017 License: MIT Imports: 8 Imported by: 0

README

proto

Build Status Go Report Card GoDoc

Package in Go for parsing Google Protocol Buffers [.proto files version 2 + 3] (https://developers.google.com/protocol-buffers/docs/reference/proto3-spec)

This repository also includes 3 commands. The protofmt tool is for formatting .proto files. The proto2xsd tool is for generating XSD files from .proto version 3 files. The proto2gql tool is for generating the GraphQL Schema.

usage as package
package main

import (
	"os"

	"github.com/emicklei/proto"
)

func main() {
	reader, _ := os.Open("test.proto")
	defer reader.Close()
	parser := proto.NewParser(reader)
	definition, _ := parser.Parse()
	formatter := proto.NewFormatter(os.Stdout, " ")
	formatter.Format(definition)
}
usage of proto2xsd command
> proto2xsd -help
	Usage of proto2xsd [flags] [path ...]
	-ns string
        namespace of the target types (default "http://your.company.com/domain/version")		
	-w	write result to an XSD files instead of stdout

See folder cmd/proto2xsd/README.md for more details.

usage of proto2gql command
> proto2gql -help
    Usage of proto2gql [flags] [path ...]

    -std_out
        Writes transformed files to stdout
    -txt_out string
        Writes transformed files to .graphql file
    -go_out string
        Writes transformed files to .go file
    -js_out string
        Writes transformed files to .js file
    -package_alias value
        Renames packages using given aliases
    -resolve_import value
        Resolves given external packages
    -no_prefix
        Disables package prefix for type names

See folder cmd/proto2gql/README.md for more details.

usage of protofmt command
> protofmt -help
	Usage of protofmt [flags] [path ...]
	-w	write result to (source) files instead of stdout

See folder cmd/protofmt/README.md for more details.

how to install
go get -u -v github.com/emicklei/proto
known issues
  • the proto2 test file in protofmt folder contains character escape sequences that are currently not accepted by the scanner. See line 537 and 573.

© 2017, ernestmicklei.com. MIT License. Contributions welcome.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Comment

type Comment struct {
	Position   scanner.Position
	Lines      []string
	Cstyle     bool // refers to /* ... */,  C++ style is using //
	ExtraSlash bool
}

Comment holds a message.

func (*Comment) Accept

func (c *Comment) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Comment) Merge

func (c *Comment) Merge(other *Comment)

Merge appends all lines from the argument comment.

func (Comment) Message

func (c Comment) Message() string

Message returns the first line or empty if no lines.

func (*Comment) String added in v1.1.0

func (c *Comment) String() string

type Documented

type Documented interface {
	Doc() *Comment
}

Documented is for types that may have an associated comment (not inlined).

type Enum

type Enum struct {
	Position scanner.Position
	Comment  *Comment
	Name     string
	Elements []Visitee
}

Enum definition consists of a name and an enum body.

func (*Enum) Accept

func (e *Enum) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Enum) Doc

func (e *Enum) Doc() *Comment

Doc is part of Documented

type EnumField

type EnumField struct {
	Position      scanner.Position
	Comment       *Comment
	Name          string
	Integer       int
	ValueOption   *Option
	InlineComment *Comment
}

EnumField is part of the body of an Enum.

func (*EnumField) Accept

func (f *EnumField) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*EnumField) Doc

func (f *EnumField) Doc() *Comment

Doc is part of Documented

type Extensions

type Extensions struct {
	Position      scanner.Position
	Comment       *Comment
	Ranges        []Range
	InlineComment *Comment
}

Extensions declare that a range of field numbers in a message are available for third-party extensions. proto2 only

func (*Extensions) Accept

func (e *Extensions) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Extensions) String added in v1.1.0

func (e *Extensions) String() string

type Field

type Field struct {
	Position      scanner.Position
	Comment       *Comment
	Name          string
	Type          string
	Sequence      int
	Options       []*Option
	InlineComment *Comment
}

Field is an abstract message field.

type Formatter

type Formatter struct {
	// contains filtered or unexported fields
}

Formatter visits a Proto and writes formatted source.

func NewFormatter

func NewFormatter(writer io.Writer, indentSeparator string) *Formatter

NewFormatter returns a new Formatter. Only the indentation separator is configurable.

func (*Formatter) Format

func (f *Formatter) Format(p *Proto)

Format visits all proto elements and writes formatted source.

func (*Formatter) VisitComment

func (f *Formatter) VisitComment(c *Comment)

VisitComment formats a Comment and writes a newline.

func (*Formatter) VisitEnum

func (f *Formatter) VisitEnum(e *Enum)

VisitEnum formats a Enum.

func (*Formatter) VisitEnumField

func (f *Formatter) VisitEnumField(e *EnumField)

VisitEnumField formats a EnumField.

func (*Formatter) VisitExtensions

func (f *Formatter) VisitExtensions(e *Extensions)

VisitExtensions formats a proto2 Extensions.

func (*Formatter) VisitGroup

func (f *Formatter) VisitGroup(g *Group)

VisitGroup formats a proto2 Group.

func (*Formatter) VisitImport

func (f *Formatter) VisitImport(i *Import)

VisitImport formats a Import.

func (*Formatter) VisitMapField

func (f *Formatter) VisitMapField(m *MapField)

VisitMapField formats a MapField.

func (*Formatter) VisitMessage

func (f *Formatter) VisitMessage(m *Message)

VisitMessage formats a Message.

func (*Formatter) VisitNormalField

func (f *Formatter) VisitNormalField(f1 *NormalField)

VisitNormalField formats a NormalField.

func (*Formatter) VisitOneof

func (f *Formatter) VisitOneof(o *Oneof)

VisitOneof formats a Oneof.

func (*Formatter) VisitOneofField

func (f *Formatter) VisitOneofField(o *OneOfField)

VisitOneofField formats a OneofField.

func (*Formatter) VisitOption

func (f *Formatter) VisitOption(o *Option)

VisitOption formats a Option.

func (*Formatter) VisitPackage

func (f *Formatter) VisitPackage(p *Package)

VisitPackage formats a Package.

func (*Formatter) VisitRPC

func (f *Formatter) VisitRPC(r *RPC)

VisitRPC formats a RPC.

func (*Formatter) VisitReserved

func (f *Formatter) VisitReserved(r *Reserved)

VisitReserved formats a Reserved.

func (*Formatter) VisitService

func (f *Formatter) VisitService(s *Service)

VisitService formats a Service.

func (*Formatter) VisitSyntax

func (f *Formatter) VisitSyntax(s *Syntax)

VisitSyntax formats a Syntax.

type Group

type Group struct {
	Position scanner.Position
	Comment  *Comment
	Name     string
	Optional bool
	Sequence int
	Elements []Visitee
}

Group represents a (proto2 only) group. https://developers.google.com/protocol-buffers/docs/reference/proto2-spec#group_field

func (*Group) Accept

func (g *Group) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Group) Doc

func (g *Group) Doc() *Comment

Doc is part of Documented

func (*Group) String added in v1.1.0

func (g *Group) String() string

type Import

type Import struct {
	Position      scanner.Position
	Comment       *Comment
	Filename      string
	Kind          string // weak, public, <empty>
	InlineComment *Comment
}

Import holds a filename to another .proto definition.

func (*Import) Accept

func (i *Import) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Import) Doc

func (i *Import) Doc() *Comment

Doc is part of Documented

type Literal

type Literal struct {
	Position scanner.Position
	Source   string
	IsString bool
}

Literal represents intLit,floatLit,strLit or boolLit

func (Literal) String

func (l Literal) String() string

String returns the source (if quoted then use double quote).

type MapField

type MapField struct {
	*Field
	KeyType string
}

MapField represents a map entry in a message.

func (*MapField) Accept

func (f *MapField) Accept(v Visitor)

Accept dispatches the call to the visitor.

type Message

type Message struct {
	Position scanner.Position
	Comment  *Comment
	Name     string
	IsExtend bool
	Elements []Visitee
}

Message consists of a message name and a message body.

func (*Message) Accept

func (m *Message) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Message) Doc

func (m *Message) Doc() *Comment

Doc is part of Documented

func (*Message) String added in v1.1.0

func (m *Message) String() string

type NamedLiteral

type NamedLiteral struct {
	*Literal
	Name string
}

NamedLiteral associates a name with a Literal

type NormalField

type NormalField struct {
	*Field
	Repeated bool
	Optional bool // proto2
	Required bool // proto2
}

NormalField represents a field in a Message.

func (*NormalField) Accept

func (f *NormalField) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*NormalField) Doc

func (f *NormalField) Doc() *Comment

Doc is part of Documented

func (*NormalField) String added in v1.1.0

func (n *NormalField) String() string

type OneOfField

type OneOfField struct {
	*Field
}

OneOfField is part of Oneof.

func (*OneOfField) Accept

func (o *OneOfField) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*OneOfField) Doc

func (o *OneOfField) Doc() *Comment

Doc is part of Documented Note: although Doc() is defined on Field, it must be implemented here as well.

type Oneof

type Oneof struct {
	Position scanner.Position
	Comment  *Comment
	Name     string
	Elements []Visitee
}

Oneof is a field alternate.

func (*Oneof) Accept

func (o *Oneof) Accept(v Visitor)

Accept dispatches the call to the visitor.

type Option

type Option struct {
	Position            scanner.Position
	Comment             *Comment
	Name                string
	Constant            Literal
	IsEmbedded          bool
	AggregatedConstants []*NamedLiteral
	InlineComment       *Comment
}

Option is a protoc compiler option

func (*Option) Accept

func (o *Option) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Option) Doc

func (o *Option) Doc() *Comment

Doc is part of Documented

type Package

type Package struct {
	Position      scanner.Position
	Comment       *Comment
	Name          string
	InlineComment *Comment
}

Package specifies the namespace for all proto elements.

func (*Package) Accept

func (p *Package) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Package) Doc

func (p *Package) Doc() *Comment

Doc is part of Documented

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser represents a parser.

func NewParser

func NewParser(r io.Reader) *Parser

NewParser returns a new instance of Parser.

func (*Parser) Filename added in v1.1.0

func (p *Parser) Filename(f string)

Filename is for reporting. Optional.

func (*Parser) Parse

func (p *Parser) Parse() (*Proto, error)

Parse parses a proto definition. May return a parse or scanner error.

type Proto

type Proto struct {
	Elements []Visitee
}

Proto represents a .proto definition

type RPC

type RPC struct {
	Position       scanner.Position
	Comment        *Comment
	Name           string
	RequestType    string
	StreamsRequest bool
	ReturnsType    string
	StreamsReturns bool
	Options        []*Option
	InlineComment  *Comment
}

RPC represents an rpc entry in a message.

func (*RPC) Accept

func (r *RPC) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*RPC) Doc

func (r *RPC) Doc() *Comment

Doc is part of Documented

type Range

type Range struct {
	From, To int
	Max      bool
}

Range is to specify number intervals (with special end value "max")

func (Range) String

func (r Range) String() string

String return a single number if from = to. Returns <from> to <to> otherwise unless Max then return <from> to max.

type Reserved

type Reserved struct {
	Position      scanner.Position
	Comment       *Comment
	Ranges        []Range
	FieldNames    []string
	InlineComment *Comment
}

Reserved statements declare a range of field numbers or field names that cannot be used in a message.

func (*Reserved) Accept

func (r *Reserved) Accept(v Visitor)

Accept dispatches the call to the visitor.

type Service

type Service struct {
	Position scanner.Position
	Comment  *Comment
	Name     string
	Elements []Visitee
}

Service defines a set of RPC calls.

func (*Service) Accept

func (s *Service) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Service) Doc

func (s *Service) Doc() *Comment

Doc is part of Documented

type Syntax

type Syntax struct {
	Position      scanner.Position
	Comment       *Comment
	Value         string
	InlineComment *Comment
}

Syntax should have value "proto"

func (*Syntax) Accept

func (s *Syntax) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Syntax) Doc

func (s *Syntax) Doc() *Comment

Doc is part of Documented

type Visitee

type Visitee interface {
	Accept(v Visitor)
}

Visitee is implemented by all Proto elements.

type Visitor

type Visitor interface {
	VisitMessage(m *Message)
	VisitService(v *Service)
	VisitSyntax(s *Syntax)
	VisitPackage(p *Package)
	VisitOption(o *Option)
	VisitImport(i *Import)
	VisitNormalField(i *NormalField)
	VisitEnumField(i *EnumField)
	VisitEnum(e *Enum)
	VisitComment(e *Comment)
	VisitOneof(o *Oneof)
	VisitOneofField(o *OneOfField)
	VisitReserved(r *Reserved)
	VisitRPC(r *RPC)
	VisitMapField(f *MapField)
	// proto2
	VisitGroup(g *Group)
	VisitExtensions(e *Extensions)
}

Visitor is for dispatching Proto elements.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL