protoreflect

module
v1.14.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2022 License: Apache-2.0

README

Protocol Buffer and gRPC Reflection

Build Status Go Report Card

This repo provides reflection APIs for protocol buffers (also known as "protobufs" for short) and gRPC. The core of reflection in protobufs is the descriptor. A descriptor is itself a protobuf message that describes a .proto source file or any element therein. So a collection of descriptors can describe an entire schema of protobuf types, including RPC services.

GoDoc


⚠️ Note

This repo was built to work with the original "V1" API of the Protobuf runtime for Go: github.com/golang/protobuf.

Since the creation of this repo, a new runtime for Go has been release, a "V2" of the API in google.golang.org/protobuf. Most protobuf users have likely upgraded to that newer runtime and thus likely encounter some interop issues using this repo. In the meantime, you can often work around these issues using the proto.MessageV1 and proto.MessageV2 converter functions (both are defined in the V1 proto package).

We eventually want this repo to have much better interop with the V2 API. Here are the current goals:

  1. By EOY 2022: Update the desc.Descriptor type in this repo to be a wrapper around the protoreflect.Descriptor types in the V2 API. Also provide simple conversion functions to go from one to the other. This will likely resolve most of the issues that people encounter.
  2. H1 2023: Create a new V2 of this whole repo. The V2 protobuf API now includes support for descriptors and dynamic messages, and it also exposes numerous low-level aspects like the binary wire format. So a lot of what's in this repo is no longer necessary. But some features still are, such as the desc/builder, desc/protoprint, dynamic/grpcdynamic, dynamic/msgregistry, and grpcreflect packages. These will remain in the V2 form of the repo (though possibly rearranged a little). Noticeably absent from the list above is desc/protoparse, whose V2 API will be in a brand new module named protocompile. It will be a much improved API, provide better performance, and directly use the reflection APIs in the v2 Protobuf runtime.

Descriptors: The Language Model of Protocol Buffers

import "github.com/jhump/protoreflect/desc"

The desc package herein introduces a Descriptor interface and implementations of it that correspond to each of the descriptor types. These new types are effectively smart wrappers around the generated protobuf types that make them much more useful and easier to use.

You can construct descriptors from file descriptor sets (which can be generated by protoc), and you can also load descriptors for messages and services that are linked into the current binary. "What does it mean for messages and services to be linked in?" you may ask. It means your binary imports a package that was generated by protoc. When you generate Go code from your .proto sources, the resulting package has descriptor information embedded in it. The desc package allows you to easily extract those embedded descriptors.

Descriptors can also be acquired directly from .proto source files (using the protoparse sub-package) or by programmatically constructing them (using the builder sub-package).

Read more ≫

import "github.com/jhump/protoreflect/desc/protoparse"

The protoparse package allows for parsing of .proto source files into rich descriptors. Without this package, you must invoke protoc to either generate a file descriptor set file or to generate Go code (which has descriptor information embedded in it). This package allows reading the source directly without having to invoke protoc.

Read more ≫

import "github.com/jhump/protoreflect/desc/protoprint"

The protoprint package allows for printing of descriptors to .proto source files. This is effectively the inverse of the protoparse package. Combined with the builder package, this is a useful tool for programmatically generating protocol buffer sources.

Read more ≫

import "github.com/jhump/protoreflect/desc/builder"

The builder package allows for programmatic construction of rich descriptors. Descriptors can be constructed programmatically by creating trees of descriptor protos and using the desc package to link those into rich descriptors. But constructing a valid tree of descriptor protos is far from trivial.

So this package provides generous API to greatly simplify that task. It also allows for converting rich descriptors into builders, which means you can programmatically modify/tweak existing descriptors.

Read more ≫


Dynamic Messages and Stubs

import "github.com/jhump/protoreflect/dynamic"

The dynamic package provides a dynamic message implementation. It implements proto.Message but is backed by a message descriptor and a map of fields->values, instead of a generated struct. This is useful for acting generically with protocol buffer messages, without having to generate and link in Go code for every kind of message. This is particularly useful for general-purpose tools that need to operate on arbitrary protocol buffer schemas. This is made possible by having the tools load descriptors at runtime.

Read more ≫

import "github.com/jhump/protoreflect/dynamic/grpcdynamic"

There is also sub-package named grpcdynamic, which provides a dynamic stub implementation. The stub can be used to issue RPC methods using method descriptors instead of generated client interfaces.

Read more ≫


gRPC Server Reflection

import "github.com/jhump/protoreflect/grpcreflect"

The grpcreflect package provides an easy-to-use client for the gRPC reflection service, making it much easier to query for and work with the schemas of remote services.

It also provides some helper methods for querying for rich service descriptors for the services registered in a gRPC server.

Read more ≫

Directories

Path Synopsis
Package codec contains a reader/write type that assists with encoding and decoding protobuf's binary representation.
Package codec contains a reader/write type that assists with encoding and decoding protobuf's binary representation.
Package desc contains "rich descriptors" for protocol buffers.
Package desc contains "rich descriptors" for protocol buffers.
builder
Package builder contains a means of building and modifying proto descriptors programmatically.
Package builder contains a means of building and modifying proto descriptors programmatically.
protoparse
Package protoparse provides functionality for parsing *.proto source files into descriptors that can be used with other protoreflect packages, like dynamic messages and dynamic GRPC clients.
Package protoparse provides functionality for parsing *.proto source files into descriptors that can be used with other protoreflect packages, like dynamic messages and dynamic GRPC clients.
protoparse/ast
Package ast defines types for modeling the AST (Abstract Syntax Tree) for the protocol buffers source language.
Package ast defines types for modeling the AST (Abstract Syntax Tree) for the protocol buffers source language.
protoprint
Package protoprint provides a mechanism to generate protobuf source code from descriptors.
Package protoprint provides a mechanism to generate protobuf source code from descriptors.
sourceinfo
Package sourceinfo provides the ability to register and query source code info for file descriptors that are compiled into the binary.
Package sourceinfo provides the ability to register and query source code info for file descriptors that are compiled into the binary.
sourceinfo/cmd/protoc-gen-gosrcinfo
Command protoc-gen-gosrcinfo is a protoc plugin.
Command protoc-gen-gosrcinfo is a protoc plugin.
Package dynamic provides an implementation for a dynamic protobuf message.
Package dynamic provides an implementation for a dynamic protobuf message.
grpcdynamic
Package grpcdynamic provides a dynamic RPC stub.
Package grpcdynamic provides a dynamic RPC stub.
msgregistry
Package msgregistry contains a registry of known message and enum types.
Package msgregistry contains a registry of known message and enum types.
Package grpcreflect provides GRPC-specific extensions to protobuf reflection.
Package grpcreflect provides GRPC-specific extensions to protobuf reflection.
Package internal contains some code that should not be exported but needs to be shared across more than one of the protoreflect sub-packages.
Package internal contains some code that should not be exported but needs to be shared across more than one of the protoreflect sub-packages.

Jump to

Keyboard shortcuts

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