Git Product home page Git Product logo

transparentvalueobjects's Issues

Fix `CS0436`: The type conflicts with the imported type

IValueObject.g.cs(28,50): Warning CS0436 : The type 'IValueObject' in 'TransparentValueObjects/TransparentValueObjects.ValueObjectIncrementalSourceGenerator/IValueObject.g.cs' conflicts with the imported type 'IValueObject' in 'TransparentValueObjects.Sample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Using the type defined in 'TransparentValueObjects/TransparentValueObjects.ValueObjectIncrementalSourceGenerator/IValueObject.g.cs'

All code generated as part of the post initialization output is written to the assembly that references the source generator. All of these types are public and inside the TransparentValueObjects namespace.

This means that if project A and project B use the source generator and project B references project A, the compiler will produce warning CS0436 because types like ValueObjectAttribute are within project A and B and have the same namespace.

Support for overriding `ToString`

I often want a non-standard printer for my wrapped objects. Mostly so I can tell a difference between a integer and some other form of it. For example I may want Color<uint> to be displayed as #FF00122 instead of 4278194466.

Right now I can't override ToString since the code generation does it already.

Augment overhaul

  • Remove TransparentValueObjects.Augments library
  • Add IAugmentWith<T0, T1, ..., TN> interface
  • Add IAugment interface and marker classes
// new
public readonly partial struct MyValueObject : IAugmentWith<
    DefaultValue,
    DefaultEqualityComparer,
    JsonConverter,
    EFCoreSupport> { }

// old
public readonly partial struct SampleValueObjectString :
    IHasDefaultValue<SampleValueObjectString, string>,
    IHasDefaultEqualityComparer<SampleValueObjectString, string>,
    IHasSystemTextJsonConverter,
    IHasEfCore<SampleValueObjectString, string> { }

Missing XML comment for operators

0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(113,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator ==(CDNName, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(114,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator !=(CDNName, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(116,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator ==(CDNName, string)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(117,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator !=(CDNName, string)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(119,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator ==(string, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(120,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator !=(string, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(126,34): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.explicit operator CDNName(string)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(127,34): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.explicit operator string(CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(142,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator <(CDNName, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(143,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator >(CDNName, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(144,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator <=(CDNName, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(145,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator >=(CDNName, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(147,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator <(string, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(148,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator >(string, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(149,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator <=(string, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(150,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator >=(string, CDNName)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(152,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator <(CDNName, string)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(153,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator >(CDNName, string)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(154,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator <=(CDNName, string)'
0>NexusMods.Networking.NexusWebApi.Types.CDNName.g.cs(155,30): Warning CS1591 : Missing XML comment for publicly visible type or member 'CDNName.operator >=(CDNName, string)'

Nested definitions are not picked up

Works:

[ValueObject<Guid>]
public readonly partial struct MyValueObject { }

Doesn't work:

public class MyClass
{
    [ValueObject<Guid>]
    public readonly partial struct MyValueObject { }
}

Pre-defined values support

In cases when the ValueObject has functionality similar to an enum, but a VO would fit better.

I suggest a new Attribute marker ValueObjectInstanceAttribute to mark the values as pre-defined and two new Augments - AllValuesAugment and LimitValuesAugment

  • AllValuesAugment exposes an IEnumerable that returns all pre-defined values
  • LimitValuesAugment limits the From function to only handle the values from pre-definded, throwing if a non pre-defined value is provided.
[ValueObject<int>]
public readonly partial struct SampleIntValueObject : IAugmentWith<DefaultValueAugment, AllValuesAugment>
{
    [ValueObjectInstance]
    public static SampleIntValueObject Default { get; } = From(0);
    [ValueObjectInstance]
    public static SampleIntValueObject Instance1 { get; } = From(1);
    [ValueObjectInstance]
    public static SampleIntValueObject Instance2 { get; } = From(2);
    
    public static SampleIntValueObject DefaultValue => Default;

    // Auto Generated
    public static IEnumerable<SampleIntValueObject> All
    {
        get
        {
            yield return Default;
            yield return Instance1;
            yield return Instance2;
        }
    }
}

[ValueObject<int>]
public readonly partial struct SampleIntValueObject : IAugmentWith<LimitValuesAugment>
{
    [ValueObjectInstance]
    public static SampleIntValueObject Default { get; } = From(0);
    [ValueObjectInstance]
    public static SampleIntValueObject Instance1 { get; } = From(1);
    [ValueObjectInstance]
    public static SampleIntValueObject Instance2 { get; } = From(2);

    // Will throw if values other than 0,1,2 will be suplied to From
}

Generated code is only supported on .NET 7 and newer

Context

TVO generates dead code that is unfriendly towards runtimes that are older than .NET 7.

Explanation

TVO, by default generates types for the following augments, regardless of whether the features are used or not:

  • DefaultEqualityComparerAugment.g.cs
  • DefaultValueAugment.g.cs
  • EfCoreAugment.g.cs
  • JsonAugment.g.cs
  • IDefaultEqualityComparer.g.cs
  • IDefaultValue.g.cs
  • IValueObject.g.cs

The first 4 can only be used on .NET 5 and above due to ExcludeFromCodeCoverage with Justification.

/// <summary>
/// Augment to enable JSON support by generting a JSON converter for the value object.
/// </summary>
/// <remarks>
/// This only supports <c>System.Text.Json</c> at the moment.
/// </remarks>
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage(Justification = "Auto-generated")]
internal sealed class JsonAugment : global::TransparentValueObjects.IAugment { }

The last 3 can only be used on (.NET 6 with LangVersion==preview) or .NET 7+, due to use of static abstract.

/// <summary>
/// Represents the interface for <see cref="global::TransparentValueObjects.DefaultEqualityComparerAugment"/>
/// with members that the consumer has to implement to support the augment.
/// </summary>
internal interface IDefaultEqualityComparer<out TValueObject, TInnerValue>
    where TValueObject : global::TransparentValueObjects.IValueObject<TInnerValue>
    where TInnerValue : notnull
{
    /// <summary>
    /// Gets a equality comparer for the inner value type to be used by default.
    /// </summary>
    public static abstract global::System.Collections.Generic.IEqualityComparer<TInnerValue> InnerValueDefaultEqualityComparer { get; }
}

Expected Outcome

  • Unused code is not generated.

    • This code still ends up in the DLL if not trimmed, increasing size.
  • ExcludeFromCodeCoverage w/ Justification is used on .NET 5+ only. Older runtimes use no justification.

Alternatively source generator warns if TargetFramework is set too low.

Supporting Evidence

When no augments used.

20231205_23h06m37s

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.