AnyOf icon indicating copy to clipboard operation
AnyOf copied to clipboard

Refactor of AnyOfCodeGenerator to ease future development

Open pacyfist opened this issue 1 year ago • 0 comments

I would like to develop a feature that I think is missing from this library and could be very useful when working in ASP.NET and service->controller communication.

First I thought I'll refactor the code generator so any future changes are easier to implement. I changed the generator from appending the files line by line to using as large as possible interpolated string literals. They are converted into a string builder before compilation so the logic remains exactly the same, but it makes the code cleaner and easier to maintain.

Newly generated classes are almost identical to old ones.

There are two changes.

  • AnyOfTypes.g.cs - I added values to the enum:

(old)

public enum AnyOfType
{
    Undefined = 0, First, Second, Third, Fourth, Fifth, Sixth, Seventh, Eighth, Ninth, Tenth
}

(new)

public enum AnyOfType
{
    Undefined = 0, First = 1, Second = 2, Third = 3, Fourth = 4, Fifth = 5, Sixth = 6, Seventh = 7, Eighth = 8, Ninth = 9, Tenth = 10
}
  • AnyOf_??.g.cs - the order of fields in the constructor now is always sequential:

(old)

public AnyOf(TSecond value)
{
    _numberOfTypes = 3;
    _currentType = AnyOfType.Second;
    _currentValue = value;
    _currentValueType = typeof(TSecond);
    _second = value;
    _first = default!;
    _third = default!;
}

(new)

public AnyOf(TSecond value)
{
    _numberOfTypes = 3;
    _currentType = AnyOfType.Second;
    _currentValue = value;
    _currentValueType = typeof(TSecond);
    _first = default!;
    _second = value;
    _third = default!;
}

pacyfist avatar Feb 10 '24 12:02 pacyfist