Skip to content

Commit 61f073b

Browse files
authored
Merge pull request #37 from rafaelsc/rafaelsc/genericAttribute
Add Support to C#11 Generic Attributes
2 parents 07f9065 + 0ab7e99 commit 61f073b

File tree

10 files changed

+659
-23
lines changed

10 files changed

+659
-23
lines changed

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ using UnitGenerator;
2121
public readonly partial struct UserId { }
2222
```
2323

24+
or when using C#11 and NET7 you can use
25+
26+
```csharp
27+
using UnitGenerator;
28+
29+
[UnitOf<int>]
30+
public readonly partial struct UserId { }
31+
```
32+
2433
will generates
2534

2635
```csharp
@@ -216,10 +225,27 @@ namespace UnitGenerator
216225
public Type Type { get; }
217226
public UnitGenerateOptions Options { get; }
218227
public UnitArithmeticOperators ArithmeticOperators { get; set; }
219-
public string ToStringFormat { get; set; }
228+
public string? ToStringFormat { get; set; }
220229

221230
public UnitOfAttribute(Type type, UnitGenerateOptions options = UnitGenerateOptions.None) { ... }
222231
}
232+
233+
#if NET7_0_OR_GREATER
234+
[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)]
235+
internal class UnitOfAttribute<T> : Attribute
236+
{
237+
public Type Type { get; }
238+
public UnitGenerateOptions Options { get; }
239+
public UnitArithmeticOperators ArithmeticOperators { get; set; } = UnitArithmeticOperators.All;
240+
public string? ToStringFormat { get; set; }
241+
242+
public UnitOfAttribute(UnitGenerateOptions options = UnitGenerateOptions.None)
243+
{
244+
this.Type = typeof(T);
245+
this.Options = options;
246+
  }
247+
    }
248+
#endif
223249
}
224250
```
225251

sandbox/FileGenerate/SimplePrimitive.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,20 @@ public readonly partial struct B
1616
public readonly partial struct C
1717
{
1818
}
19+
20+
21+
[UnitOf<int>]
22+
public readonly partial struct Aa
23+
{
24+
}
25+
26+
[UnitOf<string>()]
27+
public readonly partial struct Bb
28+
{
29+
}
30+
31+
[UnitOf<int>(UnitGenerateOptions.Comparable | UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator)]
32+
public readonly partial struct Cc
33+
{
34+
}
1935
}

sandbox/Generated/UnitGenerator/UnitGenerator.SourceGenerator/FileGenerate.A.g.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ public override int GetHashCode()
7171
return value.GetHashCode();
7272
}
7373

74-
public override string ToString()
75-
{
76-
return value.ToString();
77-
}
74+
public override string ToString() => value.ToString();
7875

7976
// Default
8077

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// <auto-generated>
2+
// THIS (.cs) FILE IS GENERATED BY UnitGenerator. DO NOT CHANGE IT.
3+
// </auto-generated>
4+
#pragma warning disable CS8669
5+
using System;
6+
using System.Globalization;
7+
#if NET7_0_OR_GREATER
8+
using System.Numerics;
9+
#endif
10+
namespace FileGenerate
11+
{
12+
[System.ComponentModel.TypeConverter(typeof(AaTypeConverter))]
13+
readonly partial struct Aa
14+
: IEquatable<Aa>
15+
#if NET7_0_OR_GREATER
16+
, IEqualityOperators<Aa, Aa, bool>
17+
#endif
18+
{
19+
readonly int value;
20+
21+
public int AsPrimitive() => value;
22+
23+
public Aa(int value)
24+
{
25+
this.value = value;
26+
}
27+
28+
public static explicit operator int(Aa value)
29+
{
30+
return value.value;
31+
}
32+
33+
public static explicit operator Aa(int value)
34+
{
35+
return new Aa(value);
36+
}
37+
38+
public bool Equals(Aa other)
39+
{
40+
return value.Equals(other.value);
41+
}
42+
43+
public override bool Equals(object obj)
44+
{
45+
if (obj == null) return false;
46+
var t = obj.GetType();
47+
if (t == typeof(Aa))
48+
{
49+
return Equals((Aa)obj);
50+
}
51+
if (t == typeof(int))
52+
{
53+
return value.Equals((int)obj);
54+
}
55+
56+
return value.Equals(obj);
57+
}
58+
59+
public static bool operator ==(Aa x, Aa y)
60+
{
61+
return x.value.Equals(y.value);
62+
}
63+
64+
public static bool operator !=(Aa x, Aa y)
65+
{
66+
return !x.value.Equals(y.value);
67+
}
68+
69+
public override int GetHashCode()
70+
{
71+
return value.GetHashCode();
72+
}
73+
74+
public override string ToString() => value.ToString();
75+
76+
// Default
77+
78+
private class AaTypeConverter : System.ComponentModel.TypeConverter
79+
{
80+
private static readonly Type WrapperType = typeof(Aa);
81+
private static readonly Type ValueType = typeof(int);
82+
83+
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, Type sourceType)
84+
{
85+
if (sourceType == WrapperType || sourceType == ValueType)
86+
{
87+
return true;
88+
}
89+
90+
return base.CanConvertFrom(context, sourceType);
91+
}
92+
93+
public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, Type destinationType)
94+
{
95+
if (destinationType == WrapperType || destinationType == ValueType)
96+
{
97+
return true;
98+
}
99+
100+
return base.CanConvertTo(context, destinationType);
101+
}
102+
103+
public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
104+
{
105+
if (value != null)
106+
{
107+
var t = value.GetType();
108+
if (t == typeof(Aa))
109+
{
110+
return (Aa)value;
111+
}
112+
if (t == typeof(int))
113+
{
114+
return new Aa((int)value);
115+
}
116+
}
117+
118+
return base.ConvertFrom(context, culture, value);
119+
}
120+
121+
public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
122+
{
123+
if (value is Aa wrappedValue)
124+
{
125+
if (destinationType == WrapperType)
126+
{
127+
return wrappedValue;
128+
}
129+
130+
if (destinationType == ValueType)
131+
{
132+
return wrappedValue.AsPrimitive();
133+
}
134+
}
135+
136+
return base.ConvertTo(context, culture, value, destinationType);
137+
}
138+
}
139+
}
140+
}

sandbox/Generated/UnitGenerator/UnitGenerator.SourceGenerator/FileGenerate.B.g.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ public override int GetHashCode()
7171
return value.GetHashCode();
7272
}
7373

74-
public override string ToString()
75-
{
76-
return value.ToString();
77-
}
74+
public override string ToString() => value == null ? "null" : value.ToString();
7875

7976
// Default
8077

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// <auto-generated>
2+
// THIS (.cs) FILE IS GENERATED BY UnitGenerator. DO NOT CHANGE IT.
3+
// </auto-generated>
4+
#pragma warning disable CS8669
5+
using System;
6+
using System.Globalization;
7+
#if NET7_0_OR_GREATER
8+
using System.Numerics;
9+
#endif
10+
namespace FileGenerate
11+
{
12+
[System.ComponentModel.TypeConverter(typeof(BbTypeConverter))]
13+
readonly partial struct Bb
14+
: IEquatable<Bb>
15+
#if NET7_0_OR_GREATER
16+
, IEqualityOperators<Bb, Bb, bool>
17+
#endif
18+
{
19+
readonly string value;
20+
21+
public string AsPrimitive() => value;
22+
23+
public Bb(string value)
24+
{
25+
this.value = value;
26+
}
27+
28+
public static explicit operator string(Bb value)
29+
{
30+
return value.value;
31+
}
32+
33+
public static explicit operator Bb(string value)
34+
{
35+
return new Bb(value);
36+
}
37+
38+
public bool Equals(Bb other)
39+
{
40+
return value.Equals(other.value);
41+
}
42+
43+
public override bool Equals(object obj)
44+
{
45+
if (obj == null) return false;
46+
var t = obj.GetType();
47+
if (t == typeof(Bb))
48+
{
49+
return Equals((Bb)obj);
50+
}
51+
if (t == typeof(string))
52+
{
53+
return value.Equals((string)obj);
54+
}
55+
56+
return value.Equals(obj);
57+
}
58+
59+
public static bool operator ==(Bb x, Bb y)
60+
{
61+
return x.value.Equals(y.value);
62+
}
63+
64+
public static bool operator !=(Bb x, Bb y)
65+
{
66+
return !x.value.Equals(y.value);
67+
}
68+
69+
public override int GetHashCode()
70+
{
71+
return value.GetHashCode();
72+
}
73+
74+
public override string ToString() => value == null ? "null" : value.ToString();
75+
76+
// Default
77+
78+
private class BbTypeConverter : System.ComponentModel.TypeConverter
79+
{
80+
private static readonly Type WrapperType = typeof(Bb);
81+
private static readonly Type ValueType = typeof(string);
82+
83+
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, Type sourceType)
84+
{
85+
if (sourceType == WrapperType || sourceType == ValueType)
86+
{
87+
return true;
88+
}
89+
90+
return base.CanConvertFrom(context, sourceType);
91+
}
92+
93+
public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, Type destinationType)
94+
{
95+
if (destinationType == WrapperType || destinationType == ValueType)
96+
{
97+
return true;
98+
}
99+
100+
return base.CanConvertTo(context, destinationType);
101+
}
102+
103+
public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
104+
{
105+
if (value != null)
106+
{
107+
var t = value.GetType();
108+
if (t == typeof(Bb))
109+
{
110+
return (Bb)value;
111+
}
112+
if (t == typeof(string))
113+
{
114+
return new Bb((string)value);
115+
}
116+
}
117+
118+
return base.ConvertFrom(context, culture, value);
119+
}
120+
121+
public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
122+
{
123+
if (value is Bb wrappedValue)
124+
{
125+
if (destinationType == WrapperType)
126+
{
127+
return wrappedValue;
128+
}
129+
130+
if (destinationType == ValueType)
131+
{
132+
return wrappedValue.AsPrimitive();
133+
}
134+
}
135+
136+
return base.ConvertTo(context, culture, value, destinationType);
137+
}
138+
}
139+
}
140+
}

sandbox/Generated/UnitGenerator/UnitGenerator.SourceGenerator/FileGenerate.C.g.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,7 @@ public override int GetHashCode()
8585
return value.GetHashCode();
8686
}
8787

88-
public override string ToString()
89-
{
90-
return value.ToString();
91-
}
88+
public override string ToString() => value.ToString();
9289

9390
// UnitGenerateOptions.ArithmeticOperator
9491

0 commit comments

Comments
 (0)