Skip to content

Commit fdcaf64

Browse files
committed
Generation of serializers can be deactivated via SmartEnum-/ValueObjectAttribute
1 parent fdbbca9 commit fdcaf64

File tree

32 files changed

+958
-121
lines changed

32 files changed

+958
-121
lines changed

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/KeyedSerializerGeneratorState.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace Thinktecture.CodeAnalysis;
55
public ITypeInformation Type { get; }
66
public IMemberInformation? KeyMember { get; }
77
public AttributeInfo AttributeInfo { get; }
8+
public SerializationFrameworks SerializationFrameworks { get; }
89

910
public string? Namespace => Type.Namespace;
1011
public IReadOnlyList<ContainingTypeState> ContainingTypes => Type.ContainingTypes;
@@ -14,17 +15,20 @@ namespace Thinktecture.CodeAnalysis;
1415
public KeyedSerializerGeneratorState(
1516
ITypeInformation type,
1617
IMemberInformation? keyMember,
17-
AttributeInfo attributeInfo)
18+
AttributeInfo attributeInfo,
19+
SerializationFrameworks serializationFrameworks)
1820
{
1921
Type = type;
2022
KeyMember = keyMember;
2123
AttributeInfo = attributeInfo;
24+
SerializationFrameworks = serializationFrameworks;
2225
}
2326

2427
public bool Equals(KeyedSerializerGeneratorState other)
2528
{
2629
return TypeInformationComparer.Instance.Equals(Type, other.Type)
2730
&& KeyMember?.TypeFullyQualified == other.KeyMember?.TypeFullyQualified
31+
&& SerializationFrameworks == other.SerializationFrameworks
2832
&& ContainingTypes.SequenceEqual(other.ContainingTypes)
2933
&& AttributeInfo.Equals(other.AttributeInfo);
3034
}
@@ -40,6 +44,7 @@ public override int GetHashCode()
4044
{
4145
var hashCode = TypeInformationComparer.Instance.GetHashCode(Type);
4246
hashCode = (hashCode * 397) ^ (KeyMember is null ? 0 : KeyMember.TypeFullyQualified.GetHashCode());
47+
hashCode = (hashCode * 397) ^ (int)SerializationFrameworks;
4348
hashCode = (hashCode * 397) ^ ContainingTypes.ComputeHashCode();
4449
hashCode = (hashCode * 397) ^ AttributeInfo.GetHashCode();
4550

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/AllEnumSettings.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public sealed class AllEnumSettings : IEquatable<AllEnumSettings>, IKeyMemberSet
1616
public SwitchMapMethodsGeneration MapMethods { get; }
1717
public ConversionOperatorsGeneration ConversionFromKeyMemberType { get; }
1818
public ConversionOperatorsGeneration ConversionToKeyMemberType { get; }
19+
public SerializationFrameworks SerializationFrameworks { get; }
1920

2021
public AllEnumSettings(AttributeData attribute)
2122
{
@@ -33,6 +34,7 @@ public AllEnumSettings(AttributeData attribute)
3334
MapMethods = attribute.FindMapMethods();
3435
ConversionToKeyMemberType = attribute.FindConversionToKeyMemberType() ?? ConversionOperatorsGeneration.Implicit;
3536
ConversionFromKeyMemberType = attribute.FindConversionFromKeyMemberType() ?? ConversionOperatorsGeneration.Explicit;
37+
SerializationFrameworks = attribute.FindSerializationFrameworks();
3638

3739
// Comparison operators depend on the equality comparison operators
3840
if (ComparisonOperators > EqualityComparisonOperators)
@@ -64,7 +66,8 @@ public bool Equals(AllEnumSettings? other)
6466
&& SwitchMethods == other.SwitchMethods
6567
&& MapMethods == other.MapMethods
6668
&& ConversionToKeyMemberType == other.ConversionToKeyMemberType
67-
&& ConversionFromKeyMemberType == other.ConversionFromKeyMemberType;
69+
&& ConversionFromKeyMemberType == other.ConversionFromKeyMemberType
70+
&& SerializationFrameworks == other.SerializationFrameworks;
6871
}
6972

7073
public override int GetHashCode()
@@ -81,10 +84,11 @@ public override int GetHashCode()
8184
hashCode = (hashCode * 397) ^ (int)EqualityComparisonOperators;
8285
hashCode = (hashCode * 397) ^ SkipIFormattable.GetHashCode();
8386
hashCode = (hashCode * 397) ^ SkipToString.GetHashCode();
84-
hashCode = (hashCode * 397) ^ SwitchMethods.GetHashCode();
85-
hashCode = (hashCode * 397) ^ MapMethods.GetHashCode();
86-
hashCode = (hashCode * 397) ^ ConversionToKeyMemberType.GetHashCode();
87-
hashCode = (hashCode * 397) ^ ConversionFromKeyMemberType.GetHashCode();
87+
hashCode = (hashCode * 397) ^ (int)SwitchMethods;
88+
hashCode = (hashCode * 397) ^ (int)MapMethods;
89+
hashCode = (hashCode * 397) ^ (int)ConversionToKeyMemberType;
90+
hashCode = (hashCode * 397) ^ (int)ConversionFromKeyMemberType;
91+
hashCode = (hashCode * 397) ^ (int)SerializationFrameworks;
8892

8993
return hashCode;
9094
}

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/JsonSmartEnumCodeGeneratorFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ private JsonSmartEnumCodeGeneratorFactory()
1515
public bool MustGenerateCode(KeyedSerializerGeneratorState state)
1616
{
1717
return !state.AttributeInfo.HasJsonConverterAttribute
18+
&& state.SerializationFrameworks.HasFlag(SerializationFrameworks.SystemTextJson)
1819
&& (state.KeyMember is not null || state.AttributeInfo.DesiredFactories.Any(f => f.UseForSerialization.Has(SerializationFrameworks.SystemTextJson)));
1920
}
2021

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/MessagePackSmartEnumCodeGeneratorFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ private MessagePackSmartEnumCodeGeneratorFactory()
1515
public bool MustGenerateCode(KeyedSerializerGeneratorState state)
1616
{
1717
return !state.AttributeInfo.HasMessagePackFormatterAttribute
18+
&& state.SerializationFrameworks.HasFlag(SerializationFrameworks.MessagePack)
1819
&& (state.KeyMember is not null || state.AttributeInfo.DesiredFactories.Any(f => f.UseForSerialization.Has(SerializationFrameworks.MessagePack)));
1920
}
2021

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/NewtonsoftJsonSmartEnumCodeGeneratorFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ private NewtonsoftJsonSmartEnumCodeGeneratorFactory()
1515
public bool MustGenerateCode(KeyedSerializerGeneratorState state)
1616
{
1717
return !state.AttributeInfo.HasNewtonsoftJsonConverterAttribute
18+
&& state.SerializationFrameworks.HasFlag(SerializationFrameworks.NewtonsoftJson)
1819
&& (state.KeyMember is not null || state.AttributeInfo.DesiredFactories.Any(f => f.UseForSerialization.Has(SerializationFrameworks.NewtonsoftJson)));
1920
}
2021

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/SmartEnumSourceGenerator.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,11 @@ private void InitializeSerializerGenerators(IncrementalGeneratorInitializationCo
191191
: states.Distinct())
192192
.WithComparer(new SetComparer<IKeyedSerializerCodeGeneratorFactory>());
193193

194-
var serializerGeneratorStates = validStates.Select((state, _) => new KeyedSerializerGeneratorState(state.State, state.KeyMember, state.AttributeInfo))
194+
var serializerGeneratorStates = validStates.Select((state, _) => new KeyedSerializerGeneratorState(
195+
state.State,
196+
state.KeyMember,
197+
state.AttributeInfo,
198+
state.Settings.SerializationFrameworks))
195199
.Combine(serializerGeneratorFactories)
196200
.SelectMany((tuple, _) => ImmutableArray.CreateRange(tuple.Right, (factory, state) => (State: state, Factory: factory), tuple.Left))
197201
.Where(tuple =>

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/ValueObjects/AllValueObjectSettings.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public sealed class AllValueObjectSettings : IEquatable<AllValueObjectSettings>,
2828
public ConversionOperatorsGeneration UnsafeConversionToKeyMemberType { get; }
2929
public ConversionOperatorsGeneration ConversionToKeyMemberType { get; }
3030
public StringComparison DefaultStringComparison { get; }
31+
public SerializationFrameworks SerializationFrameworks { get; }
3132

3233
public AllValueObjectSettings(AttributeData valueObjectAttribute)
3334
{
@@ -57,6 +58,7 @@ public AllValueObjectSettings(AttributeData valueObjectAttribute)
5758
UnsafeConversionToKeyMemberType = valueObjectAttribute.FindUnsafeConversionToKeyMemberType() ?? ConversionOperatorsGeneration.Explicit;
5859
ConversionFromKeyMemberType = valueObjectAttribute.FindConversionFromKeyMemberType() ?? ConversionOperatorsGeneration.Explicit;
5960
DefaultStringComparison = valueObjectAttribute.FindDefaultStringComparison();
61+
SerializationFrameworks = valueObjectAttribute.FindSerializationFrameworks();
6062

6163
// Comparison operators depend on the equality comparison operators
6264
if (ComparisonOperators > EqualityComparisonOperators)
@@ -100,7 +102,8 @@ public bool Equals(AllValueObjectSettings? other)
100102
&& ConversionToKeyMemberType == other.ConversionToKeyMemberType
101103
&& UnsafeConversionToKeyMemberType == other.UnsafeConversionToKeyMemberType
102104
&& ConversionFromKeyMemberType == other.ConversionFromKeyMemberType
103-
&& DefaultStringComparison == other.DefaultStringComparison;
105+
&& DefaultStringComparison == other.DefaultStringComparison
106+
&& SerializationFrameworks == other.SerializationFrameworks;
104107
}
105108

106109
public override int GetHashCode()
@@ -133,6 +136,7 @@ public override int GetHashCode()
133136
hashCode = (hashCode * 397) ^ (int)UnsafeConversionToKeyMemberType;
134137
hashCode = (hashCode * 397) ^ (int)ConversionFromKeyMemberType;
135138
hashCode = (hashCode * 397) ^ (int)DefaultStringComparison;
139+
hashCode = (hashCode * 397) ^ (int)SerializationFrameworks;
136140

137141
return hashCode;
138142
}

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/ValueObjects/ComplexSerializerGeneratorState.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace Thinktecture.CodeAnalysis.ValueObjects;
55
public ITypeInformation Type { get; }
66
public IReadOnlyList<InstanceMemberInfo> AssignableInstanceFieldsAndProperties { get; }
77
public AttributeInfo AttributeInfo { get; }
8+
public SerializationFrameworks SerializationFrameworks { get; }
89

910
public string? Namespace => Type.Namespace;
1011
public IReadOnlyList<ContainingTypeState> ContainingTypes => Type.ContainingTypes;
@@ -14,16 +15,19 @@ namespace Thinktecture.CodeAnalysis.ValueObjects;
1415
public ComplexSerializerGeneratorState(
1516
ITypeInformation type,
1617
IReadOnlyList<InstanceMemberInfo> assignableInstanceFieldsAndProperties,
17-
AttributeInfo attributeInfo)
18+
AttributeInfo attributeInfo,
19+
SerializationFrameworks serializationFrameworks)
1820
{
1921
Type = type;
2022
AssignableInstanceFieldsAndProperties = assignableInstanceFieldsAndProperties;
2123
AttributeInfo = attributeInfo;
24+
SerializationFrameworks = serializationFrameworks;
2225
}
2326

2427
public bool Equals(ComplexSerializerGeneratorState other)
2528
{
2629
return TypeInformationComparer.Instance.Equals(Type, other.Type)
30+
&& SerializationFrameworks == other.SerializationFrameworks
2731
&& AssignableInstanceFieldsAndProperties.SequenceEqual(other.AssignableInstanceFieldsAndProperties)
2832
&& ContainingTypes.SequenceEqual(other.ContainingTypes)
2933
&& AttributeInfo.Equals(other.AttributeInfo);
@@ -39,6 +43,7 @@ public override int GetHashCode()
3943
unchecked
4044
{
4145
var hashCode = TypeInformationComparer.Instance.GetHashCode(Type);
46+
hashCode = (hashCode * 397) ^ (int)SerializationFrameworks;
4247
hashCode = (hashCode * 397) ^ AssignableInstanceFieldsAndProperties.ComputeHashCode();
4348
hashCode = (hashCode * 397) ^ ContainingTypes.ComputeHashCode();
4449
hashCode = (hashCode * 397) ^ AttributeInfo.GetHashCode();

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/ValueObjects/JsonValueObjectCodeGeneratorFactory.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ private JsonValueObjectCodeGeneratorFactory()
1515
public bool MustGenerateCode(KeyedSerializerGeneratorState state)
1616
{
1717
return !state.AttributeInfo.HasJsonConverterAttribute
18+
&& state.SerializationFrameworks.HasFlag(SerializationFrameworks.SystemTextJson)
1819
&& (state.KeyMember is not null || state.AttributeInfo.DesiredFactories.Any(f => f.UseForSerialization.Has(SerializationFrameworks.SystemTextJson)));
1920
}
2021

2122
public bool MustGenerateCode(ComplexSerializerGeneratorState state)
2223
{
2324
return !state.AttributeInfo.HasJsonConverterAttribute
25+
&& state.SerializationFrameworks.HasFlag(SerializationFrameworks.SystemTextJson)
2426
&& !state.AttributeInfo.DesiredFactories.Any(f => f.UseForSerialization.Has(SerializationFrameworks.SystemTextJson));
2527
}
2628

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/ValueObjects/MessagePackValueObjectCodeGeneratorFactory.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ private MessagePackValueObjectCodeGeneratorFactory()
1515
public bool MustGenerateCode(KeyedSerializerGeneratorState state)
1616
{
1717
return !state.AttributeInfo.HasMessagePackFormatterAttribute
18+
&& state.SerializationFrameworks.HasFlag(SerializationFrameworks.MessagePack)
1819
&& (state.KeyMember is not null || state.AttributeInfo.DesiredFactories.Any(f => f.UseForSerialization.Has(SerializationFrameworks.MessagePack)));
1920
}
2021

2122
public bool MustGenerateCode(ComplexSerializerGeneratorState state)
2223
{
2324
return !state.AttributeInfo.HasMessagePackFormatterAttribute
25+
&& state.SerializationFrameworks.HasFlag(SerializationFrameworks.MessagePack)
2426
&& !state.AttributeInfo.DesiredFactories.Any(f => f.UseForSerialization.Has(SerializationFrameworks.MessagePack));
2527
}
2628

0 commit comments

Comments
 (0)