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
108 stars 13 forks source link

Split enum helper class #795

Closed bernardnormier closed 2 years ago

bernardnormier commented 2 years ago

We currently generate a helper class for enums, and it's the only construct with a separate helper class (the generated struct/class is usually its own "helper").

This helper class provides 3 extension methods, but none of them are for the enum itself. For example:

    /// <summary>Helper class for marshaling and unmarshaling <see cref="MyEnum"/>.</summary>
    public static class MyEnumHelper
    {
        public static readonly global::System.Collections.Generic.HashSet<int> EnumeratorValues =
            new global::System.Collections.Generic.HashSet<int> { 0, 1, 10, 11, 20, 21, 30, 31, 40, 41, 226 };

        public static MyEnum AsMyEnum(this int value) =>
            EnumeratorValues.Contains(value) ?
                (MyEnum)value :
                throw new IceRpc.InvalidDataException($"invalid enumerator value '{value}' for MyEnum");

        public static MyEnum DecodeMyEnum(this ref SliceDecoder decoder) =>
            AsMyEnum(decoder.DecodeSize());

        public static void EncodeMyEnum(this ref SliceEncoder encoder, MyEnum value) =>
            encoder.EncodeSize((int)value);
    }

I propose to move the EncodeXxx method to a class named SliceEncoderXxxExtensions, and DecodeXxx to a class named SliceDecoderXxxExtensions.

EnumeratorValues and AsXxx could go into a class named IiiXxxExtensions where Iii = byte, int, uint (...) as appropriate.

bernardnormier commented 2 years ago

See also #786, which could be fixed at the same time.