ProtoParser fidelity PR 1: split leading/trailing comments, fix blank-doc-line rendering#3579
ProtoParser fidelity PR 1: split leading/trailing comments, fix blank-doc-line rendering#3579efirestone wants to merge 1 commit intomasterfrom
Conversation
Adds round-trip fidelity for two ProtoParser gaps that share the same
code path:
1. Leading vs. trailing comment split. FieldElement, EnumConstantElement,
and ReservedElement gain a trailingDocumentation: String = "" field.
The parser populates it; toSchema() emits same-line trailing comments
inline (// ... for single-line, /* ... */ for multi-line block
trailing) instead of merging them into the leading block above the
declaration.
2. Blank line inside a doc paragraph. Util.appendDocumentation now emits
"//\n" instead of "// \n", eliminating the trailing-whitespace
artifact.
The API is additive and non-breaking: the existing documentation field
keeps its current merged semantics ("$leading\n$trailing" when both are
present), so consumers that only read documentation see no change.
Parser-constructed elements always populate the new field; elements
constructed by other means default to "".
SyntaxReader.tryAppendTrailingDocumentation now returns a small
DocumentationWithTrailing(merged, trailing) value. Shared helpers
leadingDocumentation(...) and appendTrailingDocumentation(...) in
Util.kt keep the printer logic uniform across the three elements.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
5306de5 to
814c268
Compare
|
What's the use case? |
We want to be able to programmatically strip out unused fields/types on a .proto file which can then later be converted to whatever codegen'd language we want. It's similar to the existing allow/denylist behavior, but the result is another copy of the .proto file rather than Kotlin/Java/Swift. We can mostly do it today, but the comments, whitespace, and ordering don't come through faithfully, so the goal is to preserve those accurately. |
|
Treeshaking at the proto level to generate another proto schema is already heavily used by all modules/services on the backend of Cash App. Everytime a Since we currently consider the way generated code is formatted not part of the API, I'm curious as to why we would wanna enforce that to |
Summary
Today, if you go .proto file ->
ProtoParser-> print out .proto file the resulting file isn't identical to the original because some metadata isn't preserved withinProtoParser. The goal of this and follow-on PRs is to add that metadata intoProtoParserso that we can faithfully reproduce the original file.This is the first of a 4-part series. This PR covers the two fidelity gaps that share the same code path:
FieldElement,EnumConstantElement, andReservedElementgain a newtrailingDocumentation: String = ""field. The parser populates it, andtoSchema()emits same-line trailing comments inline (// ..., or/* ... */for multi-line block trailing) instead of merging them into the leading block above the declaration.Util.appendDocumentationnow emits//\ninstead of// \nfor empty lines, eliminating the trailing-whitespace artifact.API shape
Additive and non-breaking. The existing
documentation: Stringfield keeps its current merged semantics ("$leading\n$trailing"when both are present), so consumers that only readdocumentationsee no change. Parser-constructed elements always populate the new field; elements constructed by other means default to"".Plumbing:
SyntaxReader.tryAppendTrailingDocumentationnow returns a smallDocumentationWithTrailing(merged, trailing)value (and isinternal, since the new return type isinternal).leadingDocumentation(...)andappendTrailingDocumentation(...)inUtil.ktare reused across the three elements to keep printer logic uniform.Test plan
./gradlew :wire-schema:jvmTest— all pass (including new unit + round-trip tests):wire-compiler,:wire-java-generator,:wire-kotlin-generator,:wire-swift-generator,:wire-runtime,:wire-golden-files,:wire-gradle-plugin,:wire-moshi-adapter,:wire-gson-support,:wire-reflector) — all pass, no golden file driftdocumentation(messageFieldLeadingAndTrailingCommentAreCombined,trailingCommentNotAssignedToFollowingField,trailingCommentNotCombinedWhenEmpty) remain greenOut of scope (follow-up PRs in the series)
Commits
10 commits, each small and self-contained. Reviewable top-to-bottom.
🤖 Generated with Claude Code