-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve comments & add procedure consts to generated code #480
Conversation
If the source schema has leading or leading detached comments on the protobuf syntax or package declarations, propagate them to the generated code. This matches the behavior of protoc-gen-go and protoc-gen-go-grpc, with one small change: we keep protobuf package comments adjacent to the Go package declaration, so that they appear as the package-level documentation in GoDoc. Fixes #470.
Generate constants for each RPC's fully-qualified name. This is particularly useful in Connect interceptors and net/http middleware, where users often want their code to behave differently depending on the RPC being called. (For example, they may have a subset of procedures exempted from authentication.) Without this PR, the best approach we can offer is either hard-coding a string literal (fragile if the package or RPC is moved, renamed, or deleted) or reflection using the descriptor embedded in protoc-gen-go's output. protoc-gen-go-grpc recently began doing something very similar to this, so it seems reasonable for us to do the same. Of the open issues, this fixes #326.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes look great - I'm not great at naming so will let y'all figure out the best name for the constants.
wrapComments(g, "Note that these are different from the fully-qualified method names used by ", | ||
"google.golang.org/protobuf/reflect/protoreflect. To convert from these constants to ", | ||
"reflection-formatted method names, remove the leading slash and convert the ", | ||
"remaining slash to a period.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of documenting this in the generated code, is it worth adding a utility function to convert a procedure to a protoreflect.FullName? That seems like a useful method and we could simplify the generated code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm hoping to minimize the number of places that the connect-go runtime couples closely to the protobuf runtime. I suspect that the most common reason people would make this translation is to get a method descriptor, so I'm planning to just expose that directly in a subsequent PR.
// Code generated by protoc-gen-connect-go. DO NOT EDIT. | ||
// | ||
// Source: connect/ping/v1/ping.proto | ||
|
||
// The connect.ping.v1 package contains an echo service designed to test the | ||
// connect-go implementation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like the go code adds a trailing space here before the package statement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. I'm not sure why - the commits over there don't clarify. It looks like this is perhaps an artifact of reusing the same function for the comments on the protobuf package and the comments on the protobuf syntax. For us, it seems nice to produce Go code with package docs.
@@ -12,10 +12,15 @@ | |||
// See the License for the specific language governing permissions and | |||
// limitations under the License. | |||
|
|||
// The canonical location for this file is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is one benefit now for people not using our license header tools is that the license in the .proto file will be included?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does look like that is the result - here's a run from the plugins repo from two images exported w/ buf export
: https://gist.github.com/pkwarren/0628b6e2412d7f6483e59c23344d9079
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep! This seemed like a good thing to me - do you think it's ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't see anything of interest that hadn't already been pointed out, so LGTM.
Any estimates as to when this change will land in a release? |
Likely Monday! |
[](https://renovatebot.com) This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [github.com/bufbuild/connect-go](https://togithub.com/bufbuild/connect-go) | require | minor | `v1.5.2` -> `v1.6.0` | --- ### Release Notes <details> <summary>bufbuild/connect-go</summary> ### [`v1.6.0`](https://togithub.com/bufbuild/connect-go/releases/tag/v1.6.0) [Compare Source](https://togithub.com/bufbuild/connect-go/compare/v1.5.2...v1.6.0) <!-- Release notes generated using configuration in .github/release.yml at main --> #### What's Changed ##### Enhancements - Improve comments & add procedure consts to generated code by [@​akshayjshah](https://togithub.com/akshayjshah) in [https://github.com/bufbuild/connect-go/pull/480](https://togithub.com/bufbuild/connect-go/pull/480) - Reduce per-call URL parsing cost by [@​mattrobenolt](https://togithub.com/mattrobenolt) in [https://github.com/bufbuild/connect-go/pull/467](https://togithub.com/bufbuild/connect-go/pull/467) - Improve errors for outdated protobuf runtimes by [@​akshayjshah](https://togithub.com/akshayjshah) in [https://github.com/bufbuild/connect-go/pull/465](https://togithub.com/bufbuild/connect-go/pull/465) - Switch README to use `buf curl` by [@​akshayjshah](https://togithub.com/akshayjshah) in [https://github.com/bufbuild/connect-go/pull/474](https://togithub.com/bufbuild/connect-go/pull/474) ##### Bugfixes - Clarify purpose of handler_stream_test.go by [@​Hirochon](https://togithub.com/Hirochon) in [https://github.com/bufbuild/connect-go/pull/472](https://togithub.com/bufbuild/connect-go/pull/472) - Make StreamType constants typed numerics by [@​jhump](https://togithub.com/jhump) in [https://github.com/bufbuild/connect-go/pull/486](https://togithub.com/bufbuild/connect-go/pull/486) - Populate Spec and Peer in Client.CallServerStream by [@​jhump](https://togithub.com/jhump) in [https://github.com/bufbuild/connect-go/pull/487](https://togithub.com/bufbuild/connect-go/pull/487) #### New Contributors - [@​Hirochon](https://togithub.com/Hirochon) made their first contribution in [https://github.com/bufbuild/connect-go/pull/472](https://togithub.com/bufbuild/connect-go/pull/472) **Full Changelog**: bufbuild/connect-go@v1.5.2...v1.6.0 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://app.renovatebot.com/dashboard#github/open-feature/flagd). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS4zMS40IiwidXBkYXRlZEluVmVyIjoiMzUuMzEuNCJ9--> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
This PR improves
protoc-gen-connect-go
's generated code in two ways:the generated code. This matches protoc-gen-go's behavior, with one small
improvement: comments attached to the protobuf package statement are also
attached to the Go package declaration (vs. leaving an intervening blank
line). This lets package-level protobuf comments also serve as package-level
GoDoc.
users easily customize the behavior of interceptors or
net/http
middlewareon a per-procedure basis. This has come up repeatedly, beginning shortly
after release, so we ought to provide a satisfactory answer.
protoc-gen-go-grpc
began doing something similar in the most recentrelease.
Fixes #326, #470, and #471.