Categorygithub.com/emicklei/proto
modulepackage
1.14.0
Repository: https://github.com/emicklei/proto.git
Documentation: pkg.go.dev

# README

proto

Go Go Report Card GoDoc codecov

Package in Go for parsing Google Protocol Buffers .proto files version 2 + 3

install

go get -u -v github.com/emicklei/proto

usage

package main

import (
	"fmt"
	"os"

	"github.com/emicklei/proto"
)

func main() {
	reader, _ := os.Open("test.proto")
	defer reader.Close()

	parser := proto.NewParser(reader)
	definition, _ := parser.Parse()

	proto.Walk(definition,
		proto.WithService(handleService),
		proto.WithMessage(handleMessage))
}

func handleService(s *proto.Service) {
	fmt.Println(s.Name)
}

func handleMessage(m *proto.Message) {
	lister := new(optionLister)
	for _, each := range m.Elements {
		each.Accept(lister)
	}
	fmt.Println(m.Name)
}

type optionLister struct {
	proto.NoopVisitor
}

func (l optionLister) VisitOption(o *proto.Option) {
	fmt.Println(o.Name)
}

validation

Current parser implementation is not completely validating .proto definitions. In many but not all cases, the parser will report syntax errors when reading unexpected charaters or tokens. Use some linting tools (e.g. https://github.com/uber/prototool) or protoc for full validation.

contributions

See proto-contrib for other contributions on top of this package such as protofmt, proto2xsd and proto2gql. protobuf2map is a small package for inspecting serialized protobuf messages using its .proto definition.

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

# Functions

NewParser returns a new instance of Parser.
Walk recursively pays a visit to all Visitees of a Proto and calls each handler with it.
WithEnum returns a Handler that will call the apply function when the Visitee is a Enum.
WithImport returns a Handler that will call the apply function when the Visitee is an Import.
WithMessage returns a Handler that will call the apply function when the Visitee is a Message.
WithNormalField returns a Handler that will call the apply function when the Visitee is a NormalField.
WithOneof returns a Handler that will call the apply function when the Visitee is a Oneof.
WithOption returns a Handler that will call the apply function when the Visitee is a Option.
WithPackage returns a Handler that will call the apply function when the Visitee is a Package.
WithRPC returns a Handler that will call the apply function when the Visitee is a RPC.
WithService returns a Handler that will call the apply function when the Visitee is a Service.

# Structs

Comment one or more comment text lines, either in c- or c++ style.
No description provided by the author
Enum definition consists of a name and an enum body.
EnumField is part of the body of an Enum.
Extensions declare that a range of field numbers in a message are available for third-party extensions.
Field is an abstract message field.
Group represents a (proto2 only) group.
Import holds a filename to another .proto definition.
Literal represents intLit,floatLit,strLit or boolLit or a nested structure thereof.
MapField represents a map entry in a message.
Message consists of a message name and a message body.
NamedLiteral associates a name with a Literal.
NoopVisitor is a no-operation visitor that can be used when creating your own visitor that is interested in only one or a few types.
NormalField represents a field in a Message.
Oneof is a field alternate.
OneOfField is part of Oneof.
Option is a protoc compiler option.
Package specifies the namespace for all proto elements.
Parser represents a parser.
Proto represents a .proto definition.
Range is to specify number intervals (with special end value "max").
Reserved statements declare a range of field numbers or field names that cannot be used in a message.
RPC represents an rpc entry in a message.
Service defines a set of RPC calls.
Syntax should have value "proto".

# Interfaces

Documented is for types that may have an associated comment (not inlined).
Visitee is implemented by all Proto elements.
Visitor is for dispatching Proto elements.

# Type aliases

Handler is a type of function that accepts a Visitee.
LiteralMap is like a map of *Literal but preserved the ordering.