github.com/bakjos/protoreflect@v1.9.2/README.md (about) 1 # Protocol Buffer and gRPC Reflection 2 3 [![Build Status](https://circleci.com/gh/jhump/protoreflect/tree/master.svg?style=svg)](https://circleci.com/gh/jhump/protoreflect/tree/master) 4 [![Go Report Card](https://goreportcard.com/badge/github.com/bakjos/protoreflect)](https://goreportcard.com/report/github.com/bakjos/protoreflect) 5 6 This repo provides reflection APIs for [protocol buffers](https://developers.google.com/protocol-buffers/) (also known as "protobufs" for short) 7 and [gRPC](https://grpc.io/). The core of reflection in protobufs is the 8 [descriptor](https://github.com/google/protobuf/blob/199d82fde1734ab5bc931cd0de93309e50cd7ab9/src/google/protobuf/descriptor.proto). 9 A descriptor is itself a protobuf message that describes a `.proto` source file or any element 10 therein. So a collection of descriptors can describe an entire schema of protobuf types, including 11 RPC services. 12 13 [![GoDoc](https://godoc.org/github.com/bakjos/protoreflect?status.svg)](https://godoc.org/github.com/bakjos/protoreflect) 14 15 --- 16 17 ## Descriptors: The Language Model of Protocol Buffers 18 19 ```go 20 import "github.com/bakjos/protoreflect/desc" 21 ``` 22 23 The `desc` package herein introduces a `Descriptor` interface and implementations of it that 24 correspond to each of the descriptor types. These new types are effectively smart wrappers around 25 the [generated protobuf types](https://github.com/golang/protobuf/blob/master/protoc-gen-go/descriptor/descriptor.pb.go) 26 that make them _much_ more useful and easier to use. 27 28 You can construct descriptors from file descriptor sets (which can be generated by `protoc`), and 29 you can also load descriptors for messages and services that are linked into the current binary. 30 "What does it mean for messages and services to be linked in?" you may ask. It means your binary 31 imports a package that was generated by `protoc`. When you generate Go code from your `.proto` 32 sources, the resulting package has descriptor information embedded in it. The `desc` package allows 33 you to easily extract those embedded descriptors. 34 35 Descriptors can also be acquired directly from `.proto` source files (using the `protoparse` sub-package) 36 or by programmatically constructing them (using the `builder` sub-package). 37 38 _[Read more ≫](https://godoc.org/github.com/bakjos/protoreflect/desc)_ 39 40 ```go 41 import "github.com/bakjos/protoreflect/desc/protoparse" 42 ``` 43 44 The `protoparse` package allows for parsing of `.proto` source files into rich descriptors. Without 45 this package, you must invoke `protoc` to either generate a file descriptor set file or to generate 46 Go code (which has descriptor information embedded in it). This package allows reading the source 47 directly without having to invoke `protoc`. 48 49 _[Read more ≫](https://godoc.org/github.com/bakjos/protoreflect/desc/protoparse)_ 50 51 ```go 52 import "github.com/bakjos/protoreflect/desc/protoprint" 53 ``` 54 55 The `protoprint` package allows for printing of descriptors to `.proto` source files. This is 56 effectively the inverse of the `protoparse` package. Combined with the `builder` package, this 57 is a useful tool for programmatically generating protocol buffer sources. 58 59 _[Read more ≫](https://godoc.org/github.com/bakjos/protoreflect/desc/protoprint)_ 60 61 ```go 62 import "github.com/bakjos/protoreflect/desc/builder" 63 ``` 64 65 The `builder` package allows for programmatic construction of rich descriptors. Descriptors can 66 be constructed programmatically by creating trees of descriptor protos and using the `desc` package 67 to link those into rich descriptors. But constructing a valid tree of descriptor protos is far from 68 trivial. 69 70 So this package provides generous API to greatly simplify that task. It also allows for converting 71 rich descriptors into builders, which means you can programmatically modify/tweak existing 72 descriptors. 73 74 _[Read more ≫](https://godoc.org/github.com/bakjos/protoreflect/desc/builder)_ 75 76 --- 77 78 ## Dynamic Messages and Stubs 79 80 ```go 81 import "github.com/bakjos/protoreflect/dynamic" 82 ``` 83 84 The `dynamic` package provides a dynamic message implementation. It implements `proto.Message` but 85 is backed by a message descriptor and a map of fields->values, instead of a generated struct. This 86 is useful for acting generically with protocol buffer messages, without having to generate and link 87 in Go code for every kind of message. This is particularly useful for general-purpose tools that 88 need to operate on arbitrary protocol buffer schemas. This is made possible by having the tools load 89 descriptors at runtime. 90 91 _[Read more ≫](https://godoc.org/github.com/bakjos/protoreflect/dynamic)_ 92 93 ```go 94 import "github.com/bakjos/protoreflect/dynamic/grpcdynamic" 95 ``` 96 97 There is also sub-package named `grpcdynamic`, which provides a dynamic stub implementation. The stub can 98 be used to issue RPC methods using method descriptors instead of generated client interfaces. 99 100 _[Read more ≫](https://godoc.org/github.com/bakjos/protoreflect/dynamic/grpcdynamic)_ 101 102 --- 103 104 ## gRPC Server Reflection 105 106 ```go 107 import "github.com/bakjos/protoreflect/grpcreflect" 108 ``` 109 110 The `grpcreflect` package provides an easy-to-use client for the 111 [gRPC reflection service](https://github.com/grpc/grpc-go/blob/6bd4f6eb1ea9d81d1209494242554dcde44429a4/reflection/grpc_reflection_v1alpha/reflection.proto#L36), 112 making it much easier to query for and work with the schemas of remote services. 113 114 It also provides some helper methods for querying for rich service descriptors for the 115 services registered in a gRPC server. 116 117 _[Read more ≫](https://godoc.org/github.com/bakjos/protoreflect/grpcreflect)_