codegen: add with_* builder-style setter methods for explicit-presence fields (#30)#93
Open
tejas-dharani wants to merge 3 commits intoanthropics:mainfrom
Open
codegen: add with_* builder-style setter methods for explicit-presence fields (#30)#93tejas-dharani wants to merge 3 commits intoanthropics:mainfrom
tejas-dharani wants to merge 3 commits intoanthropics:mainfrom
Conversation
…e fields (anthropics#30) Generates pub fn with_<name>(mut self, value: impl Into<T>) -> Self for every explicit-presence scalar, bytes, and enum field. String/bytes/enum fields use impl Into<T> so &str, b"..." literals, and bare enum variants work directly without wrapping. Controlled by CodeGenConfig::generate_with_setters (default true).
|
All contributors have signed the CLA ✍️ ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #30.
Setting explicit-presence fields during construction required mutating a
default()in place — no chaining, noSome(...)elision.Problem
Fix
Every explicit-presence scalar, bytes, and enum field now gets a
with_<name>setter:The setter signature is
pub fn with_<name>(mut self, value: impl Into<T>) -> Self, decorated with#[must_use]and#[inline].impl Into<T>is used where a natural conversion exists:Stringaccepts&str;Vec<u8>bytes acceptsb"..."literals (From<&[T; N]> for Vec<T>stable since Rust 1.74, MSRV is 1.85);bytes::BytesacceptsVec<u8>; open-enum fields accept the bare variant directly —EnumValue<E>: From<E>meanswith_priority(Priority::HIGH)works withoutEnumValue::Known(...). Plain scalars take the bare type.The gate is
inner_opt_type.is_some()— the bare innerTfromclassify_field— rather thanis_optional. Proto2 repeated fields haveis_optional = true(proto2's EXPLICIT-presence default) while their struct field isVec<T>, notOption<T>; the gate correctly excludes them. Message fields, repeated, map, oneof variants, and proto2requiredfields are all excluded for the same reason. To clear a field:msg.name = None;.Opt out per-compilation unit with
CodeGenConfig { generate_with_setters: false, ..Default::default() }.Tests
Six integration tests cover: setters present for explicit-presence fields and absent for implicit/repeated;
generate_with_setters = falsesuppresses all setters;Vec<u8>,bytes::Bytes, and enum setters each useimpl Into; proto2 repeated produces no setter (regression guard for the gate).Seven runtime tests cover: chained construction, overwrite semantics, unset fields stay
None, encode/decode round-trip,&strfor strings,b"..."for bytes, and bare variant for enums.Semver
Additive — new inherent methods on all generated message types.