Skip to content

Extract helpers for optional encoding in the Slice generators - #4931

Merged
bernardnormier merged 1 commit into
icerpc:mainfrom
bernardnormier:optional-value-expression-helper
Sep 11, 2026
Merged

Extract helpers for optional encoding in the Slice generators#4931
bernardnormier merged 1 commit into
icerpc:mainfrom
bernardnormier:optional-value-expression-helper

Conversation

@bernardnormier

Copy link
Copy Markdown
Member

Fixes #4870.

The choice between (value ?? default!), value!.Value, and value! when encoding an optional was written out at
five encode sites. This PR adds TypeRef.UnwrapNonNullOptional(param) and uses it everywhere.

Two further collapses fell out of it:

  • The sequence-of-optionals site had its own lambda builder that, once it used TypeRef.IsValueType instead of a
    hand-rolled value-type test, was identical to GetEncodeLambda. EncodeSequence now mirrors EncodeDictionary:
    one lambda call plus a method-name switch.
  • The Result success/failure lambda and the stream-of-optionals lambda were the same bool-prefixed lambda in two
    projects. Both now use the new TypeRef.GetEncodeLambdaWithNullMarker.

Generated code is unchanged except that the optional field/parameter site now emits x!.Value and x! inside its
null 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

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

@externl externl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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);
@bernardnormier
bernardnormier merged commit 3eff93e into icerpc:main Sep 11, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extract a helper for the optional encode value expression in the Slice generators

4 participants