Skip to content

protoc-gen-buffa: add bytes_type and bytes_type_in options - #821

Open
g0itb4 wants to merge 1 commit into
anthropics:mainfrom
g0itb4:plugin-bytes-type
Open

g0itb4 wants to merge 1 commit into
anthropics:mainfrom
g0itb4:plugin-bytes-type

Conversation

@g0itb4

@g0itb4 g0itb4 commented Nov 12, 2026

Copy link
Copy Markdown

buffa-build can generate bytes fields as bytes::Bytes (bytes_type, bytes_type_in, use_bytes_type), which decode from a Bytes-backed buffer without a copy. The plugin has no option that sets CodeGenConfig::bytes_fields, so a buf generate or protoc workflow always gets Vec<u8> and copies every bytes payload on decode. The guide notes the gap ("there is no protoc-gen-buffa plugin equivalent yet").

Our case: a service whose responses carry Arrow IPC frames of about 100 KB in one bytes field. With the plugin, each frame is copied on decode. We generate the Rust code through connectrpc-build with CodeGenConfig::bytes_fields = [(".", BytesRepr::Bytes)] instead of buf generate, only to get this setting.

Change

The plugin accepts:

Option Effect
bytes_type=<repr> A rule for every bytes field (path .)
bytes_type_in=<proto_path>=<repr> A rule for matching fields. Repeatable; leading dot optional

<repr> is vec or bytes. Rules accumulate in option order and the last matching rule wins, as with buffa_build::Config::bytes_type / bytes_type_in. Paths go through the existing normalize_proto_path. The grammar follows codec_strategy_in, and the map options proposed in #767 (#574).

Example buf.gen.yaml:

plugins:
  - local: protoc-gen-buffa
    out: src/gen
    opt:
      - bytes_type=bytes
      - bytes_type_in=.my.pkg.Blob.scratch=vec

Not included:

  • Custom bytes types (bytes_type_custom). They need a dependency the plugin cannot check, and protoc-gen-buffa: expose map container options #767 leaves map_type_custom builder-only for the same reason.
  • string_type. StringRepr has no built-in alternative to String, so a string option would only be useful with a custom Rust path.

#492 asks whether plugin options should mirror the builder's path-scoped options in general; this is one more concrete case.

Tests

  • Unit tests in parse_config's test module: no rules by default; bytes_type adds a . rule; bytes_type_in is repeatable, normalized and kept in option order; unknown representations (Bytes, a Rust path, empty) are rejected; a rule without =<repr> or with an empty path is rejected.
  • A CLI test sends a CodeGeneratorRequest with one bytes field and bytes_type=bytes, and checks that the generated struct has pub payload: ::buffa::bytes::Bytes. The CLI tests get a run_plugin(&CodeGeneratorRequest) -> CodeGeneratorResponse helper (spawn, write stdin, check the exit status, decode), which the existing invalid_plugin_option_returns_response_error now uses too.

cargo test -p protoc-gen-buffa passes (85 unit, 6 CLI) on the pinned 1.95.0 toolchain. The steps of task lint pass (cargo fmt --all --check, cargo clippy --workspace --all-targets -- -D warnings, markdownlint-cli 0.47.0). I have not run the full workspace tests.

The guide's plugin option table lists both options, and the note in "String and bytes field representations" now says which options the plugin has. A changelog fragment is included.

Follow-up

codec_strategy_in, bytes_type_in and, once #767 lands, map_type_in parse the same <path>=<value> shape with the same error message. They could share one generic helper, for example parse_scoped_rule(key, value, parse) -> Result<(String, T), String>. I have left that out of this PR to avoid conflicts with #767, and can send it separately after both are merged.

`buffa-build` can generate `bytes` fields as `bytes::Bytes`
(`bytes_type`, `bytes_type_in`, `use_bytes_type`), which decode from a
`Bytes`-backed buffer without a copy. The plugin had no option that
sets `CodeGenConfig::bytes_fields`, so a `buf generate` or `protoc`
workflow always got `Vec<u8>` and copied every `bytes` payload on
decode.

The plugin now accepts:

- `bytes_type=<repr>`: a rule for every `bytes` field;
- `bytes_type_in=<proto_path>=<repr>`: repeatable, path-scoped.

`<repr>` is `vec` or `bytes`. Rules accumulate in option order and the
last matching rule wins, as in the builder. The grammar follows
`codec_strategy_in`. Custom types (`bytes_type_custom`) and
`string_type` stay builder-only: a string has no built-in alternative
to `String`, so a string option would only be useful with a Rust path.

Tests cover each option, path normalization, rule order, rejected
values and malformed rules, and one CLI test checks that
`bytes_type=bytes` generates a `::buffa::bytes::Bytes` field. The CLI
tests share a `run_plugin` helper that spawns the plugin and decodes its
response. The guide lists the options.
@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@g0itb4

g0itb4 commented Nov 12, 2026

Copy link
Copy Markdown
Author

recheck

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant