-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
66 changed files
with
8,093 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
<Project> | ||
<ItemGroup> | ||
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0"/> | ||
<PackageVersion Include="CsvHelper" Version="33.0.1"/> | ||
<PackageVersion Include="FluentAssertions" Version="6.12.0"/> | ||
<PackageVersion Include="JetBrains.Profiler.Api" Version="1.4.6"/> | ||
<PackageVersion Include="MessagePack" Version="2.5.172"/> | ||
<PackageVersion Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.8"/> | ||
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0"/> | ||
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" Version="1.1.2"/> | ||
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing" Version="1.1.2"/> | ||
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing" Version="1.1.2"/> | ||
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.11.0"/> | ||
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.8"/> | ||
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8"/> | ||
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.8"/> | ||
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="8.0.0"/> | ||
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.10.0"/> | ||
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0"/> | ||
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3"/> | ||
<PackageVersion Include="NSubstitute" Version="5.1.0"/> | ||
<PackageVersion Include="NSubstitute.Analyzers.CSharp" Version="1.0.17"/> | ||
<PackageVersion Include="Serilog.Extensions.Logging" Version="8.0.0"/> | ||
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0"/> | ||
<PackageVersion Include="xunit" Version="2.9.0"/> | ||
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" /> | ||
<PackageVersion Include="CsvHelper" Version="33.0.1" /> | ||
<PackageVersion Include="FluentAssertions" Version="6.12.0" /> | ||
<PackageVersion Include="JetBrains.Profiler.Api" Version="1.4.8" /> | ||
<PackageVersion Include="MessagePack" Version="2.5.172" /> | ||
<PackageVersion Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.8" /> | ||
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" /> | ||
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" Version="1.1.2" /> | ||
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing" Version="1.1.2" /> | ||
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing" Version="1.1.2" /> | ||
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.11.0" /> | ||
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.8" /> | ||
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" /> | ||
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.8" /> | ||
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" /> | ||
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.10.0" /> | ||
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" /> | ||
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" /> | ||
<PackageVersion Include="NSubstitute" Version="5.1.0" /> | ||
<PackageVersion Include="NSubstitute.Analyzers.CSharp" Version="1.0.17" /> | ||
<PackageVersion Include="Serilog.Extensions.Logging" Version="8.0.0" /> | ||
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" /> | ||
<PackageVersion Include="xunit" Version="2.9.0" /> | ||
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" /> | ||
</ItemGroup> | ||
</Project> |
53 changes: 53 additions & 0 deletions
53
...s/Thinktecture.Runtime.Extensions.Samples/DiscriminatedUnions/DiscriminatedUnionsDemos.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using Serilog; | ||
|
||
namespace Thinktecture.DiscriminatedUnions; | ||
|
||
public class DiscriminatedUnionsDemos | ||
{ | ||
public static void Demo(ILogger logger) | ||
{ | ||
logger.Information(""" | ||
|
||
|
||
==== Demo for Union ==== | ||
|
||
"""); | ||
|
||
TextOrNumber textOrNumberFromString = "text"; | ||
logger.Information("Implicitly casted from string: {TextOrNumberFromString}", textOrNumberFromString); | ||
|
||
TextOrNumber textOrNumberFromInt = 42; | ||
logger.Information("Implicitly casted from int: {TextOrNumberFromInt}", textOrNumberFromInt); | ||
|
||
logger.Information("TextOrNumber from string: IsText = {IsText}", textOrNumberFromString.IsText); | ||
logger.Information("TextOrNumber from string: IsNumber = {IsNumber}", textOrNumberFromString.IsNumber); | ||
logger.Information("TextOrNumber from int: IsText = {IsText}", textOrNumberFromInt.IsText); | ||
logger.Information("TextOrNumber from int: IsNumber = {IsNumber}", textOrNumberFromInt.IsNumber); | ||
|
||
logger.Information("TextOrNumber from string: AsText = {AsText}", textOrNumberFromString.AsText); | ||
logger.Information("TextOrNumber from int: AsNumber = {AsNumber}", textOrNumberFromInt.AsNumber); | ||
|
||
logger.Information("TextOrNumber from string: Value = {Value}", textOrNumberFromString.Value); | ||
logger.Information("TextOrNumber from int: Value = {Value}", textOrNumberFromInt.Value); | ||
|
||
textOrNumberFromString.Switch(text: s => logger.Information("String Action: {Text}", s), | ||
number: i => logger.Information("Int Action: {Number}", i)); | ||
|
||
textOrNumberFromString.Switch(logger, | ||
text: static (l, s) => l.Information("String Action with context: {Text}", s), | ||
number: static (l, i) => l.Information("Int Action with context: {Number}", i)); | ||
|
||
var switchResponse = textOrNumberFromInt.Switch(text: static s => $"String Func: {s}", | ||
number: static i => $"Int Func: {i}"); | ||
logger.Information("{Response}", switchResponse); | ||
|
||
var switchResponseWithContext = textOrNumberFromInt.Switch(123.45, | ||
text: static (ctx, s) => $"String Func with context: {ctx} | {s}", | ||
number: static (ctx, i) => $"Int Func with context: {ctx} | {i}"); | ||
logger.Information("{Response}", switchResponseWithContext); | ||
|
||
var mapResponse = textOrNumberFromString.Map(text: "Mapped string", | ||
number: "Mapped int"); | ||
logger.Information("{Response}", mapResponse); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
samples/Thinktecture.Runtime.Extensions.Samples/DiscriminatedUnions/TextOrNumber.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Thinktecture.DiscriminatedUnions; | ||
|
||
[Union<string, int>(T1IsNullableReferenceType = true, | ||
T1Name = "Text", | ||
T2Name = "Number", | ||
SwitchMethods = SwitchMapMethodsGeneration.DefaultWithPartialOverloads, | ||
MapMethods = SwitchMapMethodsGeneration.DefaultWithPartialOverloads)] | ||
public sealed partial class TextOrNumber; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...re.Runtime.Extensions.SourceGenerator/CodeAnalysis/DiscriminatedUnions/AllEnumSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
namespace Thinktecture.CodeAnalysis.DiscriminatedUnions; | ||
|
||
public sealed class AllUnionSettings : IEquatable<AllUnionSettings> | ||
{ | ||
public bool SkipToString { get; } | ||
public SwitchMapMethodsGeneration SwitchMethods { get; } | ||
public SwitchMapMethodsGeneration MapMethods { get; } | ||
public IReadOnlyList<MemberTypeSetting> MemberTypeSettings { get; } | ||
public StringComparison DefaultStringComparison { get; } | ||
|
||
public AllUnionSettings(AttributeData attribute, int numberOfMemberTypes) | ||
{ | ||
SkipToString = attribute.FindSkipToString() ?? false; | ||
SwitchMethods = attribute.FindSwitchMethods(); | ||
MapMethods = attribute.FindMapMethods(); | ||
DefaultStringComparison = attribute.FindDefaultStringComparison(); | ||
|
||
var memberTypeSettings = new MemberTypeSetting[numberOfMemberTypes]; | ||
MemberTypeSettings = memberTypeSettings; | ||
|
||
for (var i = 0; i < numberOfMemberTypes; i++) | ||
{ | ||
memberTypeSettings[i] = new MemberTypeSetting(attribute.FindTxIsNullableReferenceType(i + 1), | ||
attribute.FindTxName(i + 1)); | ||
} | ||
} | ||
|
||
public override bool Equals(object? obj) | ||
{ | ||
return obj is AllUnionSettings enumSettings && Equals(enumSettings); | ||
} | ||
|
||
public bool Equals(AllUnionSettings? other) | ||
{ | ||
if (other is null) | ||
return false; | ||
if (ReferenceEquals(this, other)) | ||
return true; | ||
|
||
return SkipToString == other.SkipToString | ||
&& SwitchMethods == other.SwitchMethods | ||
&& MapMethods == other.MapMethods | ||
&& DefaultStringComparison == other.DefaultStringComparison | ||
&& MemberTypeSettings.SequenceEqual(other.MemberTypeSettings); | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
unchecked | ||
{ | ||
var hashCode = SkipToString.GetHashCode(); | ||
hashCode = (hashCode * 397) ^ SwitchMethods.GetHashCode(); | ||
hashCode = (hashCode * 397) ^ MapMethods.GetHashCode(); | ||
hashCode = (hashCode * 397) ^ (int)DefaultStringComparison; | ||
hashCode = (hashCode * 397) ^ MemberTypeSettings.ComputeHashCode(); | ||
|
||
return hashCode; | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
....Runtime.Extensions.SourceGenerator/CodeAnalysis/DiscriminatedUnions/MemberTypeSetting.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
namespace Thinktecture.CodeAnalysis.DiscriminatedUnions; | ||
|
||
public readonly struct MemberTypeSetting : IEquatable<MemberTypeSetting>, IHashCodeComputable | ||
{ | ||
public bool IsNullableReferenceType { get; } | ||
public string? Name { get; } | ||
|
||
public MemberTypeSetting( | ||
bool isNullableReferenceType, | ||
string? name) | ||
{ | ||
IsNullableReferenceType = isNullableReferenceType; | ||
Name = name; | ||
} | ||
|
||
public override bool Equals(object? obj) | ||
{ | ||
return obj is MemberTypeSetting other && Equals(other); | ||
} | ||
|
||
public bool Equals(MemberTypeSetting other) | ||
{ | ||
return IsNullableReferenceType == other.IsNullableReferenceType | ||
&& Name == other.Name; | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
unchecked | ||
{ | ||
return (IsNullableReferenceType.GetHashCode() * 397) ^ (Name?.GetHashCode() ?? 0); | ||
} | ||
} | ||
} |
Oops, something went wrong.