Extract helpers for optional encoding in the Slice generators - #4931
Merged
bernardnormier merged 1 commit intoSep 11, 2026
Merged
Conversation
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
There was a problem hiding this comment.
🟢 Approval recommended
The refactor preserves encoding behavior; only a minor line-width issue remains.
Pull request overview
Centralizes optional-value encoding logic across Slice generators.
Changes:
- Adds helpers for optional unwrapping and null-marker lambdas.
- Reuses shared lambdas for sequences, results, fields, and streams.
- Corrects generated multiline-lambda indentation.
File summaries
| File | Description |
|---|---|
TypeRefExtensions.cs |
Adds shared optional-encoding helpers. |
ITypeExtensions.cs |
Simplifies sequence and result encoding. |
FieldExtensions.cs |
Uses centralized optional unwrapping. |
OperationExtensions.cs |
Reuses the null-marker lambda for streams. |
Review details
- Files reviewed: 4/4 changed files
- Comments generated: 1
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| return $"{encoderName}.EncodeSequence({param})"; | ||
| } | ||
|
|
||
| CodeBlock elementEncodeLambda = seq.ElementType.GetEncodeLambda(seq.ElementTypeIsOptional, currentNamespace); |
pepone
approved these changes
Sep 11, 2026
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.
Fixes #4870.
The choice between
(value ?? default!),value!.Value, andvalue!when encoding an optional was written out atfive encode sites. This PR adds
TypeRef.UnwrapNonNullOptional(param)and uses it everywhere.Two further collapses fell out of it:
TypeRef.IsValueTypeinstead of ahand-rolled value-type test, was identical to
GetEncodeLambda.EncodeSequencenow mirrorsEncodeDictionary:one lambda call plus a method-name switch.
Resultsuccess/failure lambda and the stream-of-optionals lambda were the same bool-prefixed lambda in twoprojects. Both now use the new
TypeRef.GetEncodeLambdaWithNullMarker.Generated code is unchanged except that the optional field/parameter site now emits
x!.Valueandx!inside itsnull check, like the other sites, and multi-line encode bodies in a stream-of-optionals lambda are now indented.
What's Changed
None — generator internals only.
🤖 Generated with Claude Code