icerpc / icerpc-csharp

A C# RPC framework built for QUIC, with bidirectional streaming, first-class async/await, and Protobuf support.
https://docs.icerpc.dev
Apache License 2.0
101 stars 13 forks source link

Simplify 'encode_action_body'. #3916

Closed InsertCreativityHere closed 6 months ago

InsertCreativityHere commented 7 months ago

This PR simplifies the logic of encode_action_body and fixes a bug with the encoding of tagged custom types (which map to C# value types) in Slice1 mode (quite niche).


For example, take this snippet:

class Hello {
    tag(1) d1: CustomType?
}

We currently generate this:

encoder.StartSlice(SliceTypeId);
if (this.D1 is MyCustomType d1_)
{
    encoder.EncodeTagged(1, TagFormat.FSize, d1_, (ref SliceEncoder encoder, MyCustomType value) => CustomTypeSliceEncoderExtensions.EncodeNullableCustomType(ref encoder, value));
}
encoder.EndSlice(true);

Notably, this calls EncodeNullableCustomType which is incorrect. We know here that value is non-null. Even the type of the lambda is MyCustomType, with no ? suffix.

This PR fixes this to call the non-nullable function:

encoder.StartSlice(SliceTypeId);
if (this.D1 is MyCustomType d1_)
{
    encoder.EncodeTagged(1, TagFormat.FSize, d1_, (ref SliceEncoder encoder, MyCustomType value) => CustomTypeSliceEncoderExtensions.EncodeCustomType(ref encoder, value));
}
encoder.EndSlice(true);
InsertCreativityHere commented 7 months ago

Part of this PR got spun off into #3916. So, to save hassle on merge conflicts, I'm going to wait to merge this PR until that one, since this PR will be much simpler to fix the conflicts on.