6 ms·
Parsing Protobuf Definitions with Tree-sitter
- cyberax 2y agoI don't get it. Why not just use a better Protobuf model? Go's serialization format for protobufs is not the most brilliant one, but it's reasonable. E.g. just use `string` instead of `StringValue`.
- ahtihn 2y agoStringValue let's you differentiate empty string vs no value.
- cyberax 2y agoBut do you actually need that? Like, really? If you absolutely do need it, then in addition to "name" you can also add a field "hasName". If you feel fancy, you can even do that in a code generator.
- parthdesai 2y agoYou need it when an empty string is a valid value in your domain. A better example is Int32Value. By default, protobuf will serialize int32 to 0 if it's not present.
- anytime5704 2y agoI think the other commenter’s point is you can use 2 fields to distinguish between the first field being specified as empty vs absent (or whatever terms you prefer). E.g. - type.specified => “” - type.unspecified => empty The same technique can be used to disambiguate between 0 and empty.
- jahewson 2y agoWorth noting that it’s not quite equivalent due to allowing for a malformed message that includes foo = value and hasFoo = false, opening the door to varied client interpretation.
- adrianmsmith 2y agoBut why would you do that over using the built-in StringValue, that just works and does everything you need?
- tuetuopay 2y agoThat’s an extra field, with no consistency validation, and does not play well with JSON. A field can be either "" or null, and that’s two different cases. Also the wrappers are known to most protobuf code generators, that can then use language features for better ergonomics. E.g. in Rust all wrappers to allow nullability of values will be turned in Option. Like the StringValue will turn into Option<String>. As for the need, yes, it is needed. This carries intent, which is a good thing in customer facing APIs. An update endpoint will have all fields optional to distinguish between a field needing to be overridden or not, without the need to re-send the value when it does not need to be changed (à la PUT). Having additional boolean fields gets unwieldy quickly.
- arccy 2y agoSee protobuf field presence: https://github.com/protocolbuffers/protobuf/blob/main/docs/field_presence.md https://github.com/protocolbuffers/protobuf/blob/main/docs/f...
- relistan 2y agoYou do need it for e.g. database records that are nullable or anywhere else that null/nil is a valid state that differs from zero value. Your alternative approach requires manual work on your part rather than being built in to the bindings, and allows for invalid states as pointed out by another commenter.
- MathMonkeyMan 2y agoI need to get around to playing with tree-sitter. The approach in this article is neat. Here's another approach. The AST of a .proto file is itself a protobuf. That's how the codegen plugins work. Protobuf also has a canonical mapping to JSON, so... What you can do is use protoc to parse the .proto file, spit it out as JSON, and then process that data using your favorite pattern matching language. I wrote a [tool][1] that helps with that. For example, here's some [js code][2] that translates protobuf message definitions into "types" for use in an ORM. [1]: https://github.com/dgoffredo/protojson https://github.com/dgoffredo/protojson [2]: https://github.com/dgoffredo/okra/blob/master/lib/proto2types.js https://github.com/dgoffredo/okra/blob/master/lib/proto2type...
- superb_dev 2y agoOh my god… this might’ve made some tools I’m developing a lot easier
- fizx 2y agoWriting a protoc plugin would have been 5x easier, but its harder to get a blog article out of it. Also, this reads like they might not have seen the newer proto3 optional keyword, or know about the well-known wrapper types.
- danielvaughn 2y agoI highly recommend it. The docs describe it as a zen-like experience, and I fully agree. Once you get the hang of it, it makes it so easy to tweak the syntax of whatever language you’re building. I love it.
- lelandbatey 2y agoI found that some parts of a protobuf aren't captured well by protoc; specifically annotations were not well exposed to Go libraries for writing protoc plugins in 2016. I ended up having to write my own basic protobuf parser to reliably extract annotations and comments for code and documentation generation: https://github.com/metaverse/truss/blame/master/svcdef/svcdef.go https://github.com/metaverse/truss/blame/master/svcdef/svcde...
- grumbles 2y agoHuh. tree-sitter seems neat, but I don’t really get why the author thinks processing the descriptor set is so hard. Seems equally difficult to learn a bunch of new abstractions in the form of tree-sitter vs just learning protobuf’s own ones. Also, if you’re parsing .proto files directly, you have to deal with a bunch of annoying issues like include paths, how you package sets of them to move around, etc. descriptor sets seem like a better solution to me.
- kstrauser 2y agoOTOH, if you can get treesitter to work, all the best editors will gain Protobuf support.
- oefrha 2y agoNo, they’re not inventing the tree-sitter grammar for protobuf here, they’re using the existing grammar provided by someone else. So it’s not like there’s additional reusable work being done here.
- parentheses 2y agoCame to the comments to say this. FileDescriptors have been so simple to work with that I am so surprised the author is touting TS for this use case. The other nice thing about all the descriptors is their schemas are protobufs, so language agnostic and easily serialized/deserialized.
- pcj-github 2y agoFrom the docs "The protocol compiler can output a FileDescriptorSet containing the .proto files it parses." (https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/descriptor.proto https://github.com/protocolbuffers/protobuf/blob/main/src/go...) I don't understand the point of using tree-sitter to repeat that work (almost certainly having bugs doing so). Am I missing something?
- Arainach 2y agoLike others, I don't understand the author's issues getting the stock proto reflection behavior to extract this information. I'm not as familiar with the Go reflection tools, but getting the information the author wants is trivial in Java reflection.
- relistan 2y agoIt’s fairly unpleasant in Go (opinion). Especially for e.g. custom options. YMMV
- danenania 2y agotree-sitter is an incredible tool. I wonder if there's been a dedicated discussion for it on the HN front page at some point—will have to check on the HN algolia search. I'm using it for syntax checking across 30+ languages in Plandex[1], an LLM coding tool. tree-sitter runs in single digit milliseconds on typical files and is highly accurate. When it encounters a syntax problem, it can pinpoint the exact location/expression in the file, and it's fault-tolerant so it can keep going and identify multiple issues rather than stopping on the first one. These results can be sent back to the LLM, which can then often fix its own errors. I was able to reduce syntax issues by roughly 90% with gpt-4o using this approach. Afaik, there's no other viable option for a use case like this. You'd need a menagerie of language specific linters, compilers, and/or language servers to get anywhere close, and many of those are way too slow to run inline. 1 - https://github.com/plandex-ai/plandex https://github.com/plandex-ai/plandex
- danenania 2y agoFound this discussion from 2021: https://news.ycombinator.com/item?id=26225298 https://news.ycombinator.com/item?id=26225298 This looks good too, from a year ago: https://news.ycombinator.com/item?id=36106421 https://news.ycombinator.com/item?id=36106421
- Cloudef 2y agoI used to write proto parser using ragel <https://www.colm.net/open-source/ragel/ https://www.colm.net/open-source/ragel/> for work way back as well, it was surprisingly painless. Think this was way back when protobuf was transitioning to proto3.