-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add sourceinfo plugin #22
Conversation
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.
Just one remark about fixing up doc comments, otherwise LGTM
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.
Seems fine, only question is whether we want this available as a Buf-managed plugin over on https://github.com/bufbuild/plugins?
We also have https://github.com/bufbuild/tools which is a collection of public tools/plugins we use. But this plugin does appear to be closer to what this (bufbuild/protoschema-plugins) repository aims to be.
tl;dr - is this plugin something the rest of the community would want, or something we're primarily interested in? I'm not sure.
The target use case is to enable runtime, reflection-based 'plugins'. For example, using a protobuf schema as the basis for pflags requires access to the documentation, but does not require code gen otherwise. This plugin is a building block for |
Probably worth a mention in the README.md, even if just a brief description of what this plugin does? |
fileName := pluginsourceinfo.GetSourceInfoPath(testDesc) | ||
filePath := filepath.Join(outputDir, fileName) | ||
// Create any missing directories | ||
if err = os.MkdirAll(filepath.Dir(filePath), 0755); err != nil { |
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.
err :=
, always (this err
should be scoped to this if 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.
this is a lambda that is reporting the error to the outter scope, it cannot use :=
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.
Then it shouldn't be set inside of an if
statement - that's confusing to readers. I'm actually surprised that there isn't a linter that bans this in golangci-lint
sourceinfo/sourceinfo.go
Outdated
var ( | ||
// GlobalFiles is a replacement for protoregistry.GlobalFiles that includes | ||
// all registered source info. | ||
GlobalFiles protodesc.Resolver = sourceinfo.GlobalFiles |
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.
Global variables are banned - Make this API not use global variables
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.
This was not resolved - you just changed this from a global variable to a static function that wraps a global variable.
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.
This is how the sourceinfo package exposes the functionality. Are you suggesting that package can't be used? Or do you have another solution in mind?
README.md
Outdated
## SourceInfo | ||
|
||
The sourceinfo plugin provides a protoc-gen-sourceinfo command that generates an `.sourceinfo.binpb` file, for every `.proto` file. | ||
Each generated file contains the associated serialized `google.protobuf.SourceInfo` value. |
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.
There is no type called google.protobuf.SourceInfo
. Do you mean google.protobuf.SourceCodeInfo
? If so, this calls into question the naming of a lot of things here (package, plugin, etc).
Additionally, a SourceCodeInfo
(if this is the intent) does not make a ton sense without an associated FileDescriptorProto
- why not make the plugin provide the entire FileDescriptorProto/FileDescriptorSet
?
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.
Nice typo find! I was following @jhump's naming scheme, I am happy to switch it to source code info every where if you think that would provide more clarity.
The normal code gen already comes packed with the Descriptors for normal reflection, this plugin provides just the part that is stripped out. @jhump's sourceinfo library nicely glues everything together at runtime, they same could be done in other languages.
|
||
The following helpers are provided for go via `import "github.com/bufbuild/protoschema-plugins/sourceinfo"`: | ||
|
||
- `sourceinfo.GlobalFiles()`: A drop-in replacement for protoregistry.GlobalFiles |
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.
How can this be a drop-in if you just are serializing the google.protobuf.SourceCodeInfo
file?
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.
This is hooking into @jhump's library to provide a drop in replacement, backed by normal protoregistry values.
return fmt.Sprintf("%s%s", path, FileExtension) | ||
} | ||
|
||
// GenFileContents generates the source info file contents for the given file descriptor. |
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 don't know what this means - what is the "source info file contents"? Can you explain in the documentation what this is? All I am getting back is a []byte
.
"google.golang.org/protobuf/types/descriptorpb" | ||
) | ||
|
||
// GlobalFiles is a replacement for protoregistry.GlobalFiles that includes |
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.
Needs much more extensive documentation. As a reader, I don't know what "all registered source info" is, and re: comment above, I don't know how this replaces protoregistry.GlobalFiles
(and I should not have to read the code to find out if it actually does or how it does)
sourceinfo/sourceinfo.go
Outdated
var ( | ||
// GlobalFiles is a replacement for protoregistry.GlobalFiles that includes | ||
// all registered source info. | ||
GlobalFiles protodesc.Resolver = sourceinfo.GlobalFiles |
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.
This was not resolved - you just changed this from a global variable to a static function that wraps a global variable.
The sourceinfo plugin provides a protoc-gen-sourceinfo cmd that generates a *.sourceinfo.binpb file, for every .proto file. Each generated file contains the associated serialized
google.protobuf.SourceInfo
value. These files can then be loaded directly or embedded in a binary.The following helpers are provided for go: