Skip to content

Commit

Permalink
Merge pull request #37 from rafaelsc/rafaelsc/genericAttribute
Browse files Browse the repository at this point in the history
Add Support to C#11 Generic Attributes
  • Loading branch information
neuecc authored Feb 2, 2024
2 parents 07f9065 + 0ab7e99 commit 61f073b
Show file tree
Hide file tree
Showing 10 changed files with 659 additions and 23 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ using UnitGenerator;
public readonly partial struct UserId { }
```

or when using C#11 and NET7 you can use

```csharp
using UnitGenerator;

[UnitOf<int>]
public readonly partial struct UserId { }
```

will generates

```csharp
Expand Down Expand Up @@ -216,10 +225,27 @@ namespace UnitGenerator
public Type Type { get; }
public UnitGenerateOptions Options { get; }
public UnitArithmeticOperators ArithmeticOperators { get; set; }
public string ToStringFormat { get; set; }
public string? ToStringFormat { get; set; }

public UnitOfAttribute(Type type, UnitGenerateOptions options = UnitGenerateOptions.None) { ... }
}

#if NET7_0_OR_GREATER
[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)]
internal class UnitOfAttribute<T> : Attribute
{
public Type Type { get; }
public UnitGenerateOptions Options { get; }
public UnitArithmeticOperators ArithmeticOperators { get; set; } = UnitArithmeticOperators.All;
public string? ToStringFormat { get; set; }

public UnitOfAttribute(UnitGenerateOptions options = UnitGenerateOptions.None)
{
this.Type = typeof(T);
this.Options = options;
  }
    }
#endif
}
```

Expand Down
16 changes: 16 additions & 0 deletions sandbox/FileGenerate/SimplePrimitive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,20 @@ public readonly partial struct B
public readonly partial struct C
{
}


[UnitOf<int>]
public readonly partial struct Aa
{
}

[UnitOf<string>()]
public readonly partial struct Bb
{
}

[UnitOf<int>(UnitGenerateOptions.Comparable | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator)]
public readonly partial struct Cc
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ public override int GetHashCode()
return value.GetHashCode();
}

public override string ToString()
{
return value.ToString();
}
public override string ToString() => value.ToString();

// Default

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// <auto-generated>
// THIS (.cs) FILE IS GENERATED BY UnitGenerator. DO NOT CHANGE IT.
// </auto-generated>
#pragma warning disable CS8669
using System;
using System.Globalization;
#if NET7_0_OR_GREATER
using System.Numerics;
#endif
namespace FileGenerate
{
[System.ComponentModel.TypeConverter(typeof(AaTypeConverter))]
readonly partial struct Aa
: IEquatable<Aa>
#if NET7_0_OR_GREATER
, IEqualityOperators<Aa, Aa, bool>
#endif
{
readonly int value;

public int AsPrimitive() => value;

public Aa(int value)
{
this.value = value;
}

public static explicit operator int(Aa value)
{
return value.value;
}

public static explicit operator Aa(int value)
{
return new Aa(value);
}

public bool Equals(Aa other)
{
return value.Equals(other.value);
}

public override bool Equals(object obj)
{
if (obj == null) return false;
var t = obj.GetType();
if (t == typeof(Aa))
{
return Equals((Aa)obj);
}
if (t == typeof(int))
{
return value.Equals((int)obj);
}

return value.Equals(obj);
}

public static bool operator ==(Aa x, Aa y)
{
return x.value.Equals(y.value);
}

public static bool operator !=(Aa x, Aa y)
{
return !x.value.Equals(y.value);
}

public override int GetHashCode()
{
return value.GetHashCode();
}

public override string ToString() => value.ToString();

// Default

private class AaTypeConverter : System.ComponentModel.TypeConverter
{
private static readonly Type WrapperType = typeof(Aa);
private static readonly Type ValueType = typeof(int);

public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == WrapperType || sourceType == ValueType)
{
return true;
}

return base.CanConvertFrom(context, sourceType);
}

public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == WrapperType || destinationType == ValueType)
{
return true;
}

return base.CanConvertTo(context, destinationType);
}

public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value != null)
{
var t = value.GetType();
if (t == typeof(Aa))
{
return (Aa)value;
}
if (t == typeof(int))
{
return new Aa((int)value);
}
}

return base.ConvertFrom(context, culture, value);
}

public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (value is Aa wrappedValue)
{
if (destinationType == WrapperType)
{
return wrappedValue;
}

if (destinationType == ValueType)
{
return wrappedValue.AsPrimitive();
}
}

return base.ConvertTo(context, culture, value, destinationType);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ public override int GetHashCode()
return value.GetHashCode();
}

public override string ToString()
{
return value.ToString();
}
public override string ToString() => value == null ? "null" : value.ToString();

// Default

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// <auto-generated>
// THIS (.cs) FILE IS GENERATED BY UnitGenerator. DO NOT CHANGE IT.
// </auto-generated>
#pragma warning disable CS8669
using System;
using System.Globalization;
#if NET7_0_OR_GREATER
using System.Numerics;
#endif
namespace FileGenerate
{
[System.ComponentModel.TypeConverter(typeof(BbTypeConverter))]
readonly partial struct Bb
: IEquatable<Bb>
#if NET7_0_OR_GREATER
, IEqualityOperators<Bb, Bb, bool>
#endif
{
readonly string value;

public string AsPrimitive() => value;

public Bb(string value)
{
this.value = value;
}

public static explicit operator string(Bb value)
{
return value.value;
}

public static explicit operator Bb(string value)
{
return new Bb(value);
}

public bool Equals(Bb other)
{
return value.Equals(other.value);
}

public override bool Equals(object obj)
{
if (obj == null) return false;
var t = obj.GetType();
if (t == typeof(Bb))
{
return Equals((Bb)obj);
}
if (t == typeof(string))
{
return value.Equals((string)obj);
}

return value.Equals(obj);
}

public static bool operator ==(Bb x, Bb y)
{
return x.value.Equals(y.value);
}

public static bool operator !=(Bb x, Bb y)
{
return !x.value.Equals(y.value);
}

public override int GetHashCode()
{
return value.GetHashCode();
}

public override string ToString() => value == null ? "null" : value.ToString();

// Default

private class BbTypeConverter : System.ComponentModel.TypeConverter
{
private static readonly Type WrapperType = typeof(Bb);
private static readonly Type ValueType = typeof(string);

public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == WrapperType || sourceType == ValueType)
{
return true;
}

return base.CanConvertFrom(context, sourceType);
}

public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == WrapperType || destinationType == ValueType)
{
return true;
}

return base.CanConvertTo(context, destinationType);
}

public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value != null)
{
var t = value.GetType();
if (t == typeof(Bb))
{
return (Bb)value;
}
if (t == typeof(string))
{
return new Bb((string)value);
}
}

return base.ConvertFrom(context, culture, value);
}

public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (value is Bb wrappedValue)
{
if (destinationType == WrapperType)
{
return wrappedValue;
}

if (destinationType == ValueType)
{
return wrappedValue.AsPrimitive();
}
}

return base.ConvertTo(context, culture, value, destinationType);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ public override int GetHashCode()
return value.GetHashCode();
}

public override string ToString()
{
return value.ToString();
}
public override string ToString() => value.ToString();

// UnitGenerateOptions.ArithmeticOperator

Expand Down
Loading

0 comments on commit 61f073b

Please sign in to comment.