Skip to content

Commit 163e9fd

Browse files
committed
saving
1 parent f39355d commit 163e9fd

File tree

79 files changed

+3510
-1299
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+3510
-1299
lines changed

Directory.Build.props

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project>
2+
<PropertyGroup>
3+
<Authors>Chase Crawford</Authors>
4+
<RepositoryType>git</RepositoryType>
5+
<Copyright>© Assimalign LLC $([System.DateTime]::Now.ToString(`yyyy`))</Copyright>
6+
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
7+
<VersionPrefix>1.0.0</VersionPrefix>
8+
<VersionSuffix>pre.1.0.5</VersionSuffix>
9+
</PropertyGroup>
10+
</Project>
File renamed without changes.

libraries/Assimalign.Extensions.sln

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ EndProject
1010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Mapping", "Mapping", "{F5AAF2E0-51F5-4C71-9F69-B11E58900136}"
1111
EndProject
1212
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Validation", "Validation", "{60F770D9-148F-40CD-928A-A9EE0E9DF0CF}"
13+
ProjectSection(SolutionItems) = preProject
14+
validation\Directory.Build.props = validation\Directory.Build.props
15+
validation\README.md = validation\README.md
16+
EndProjectSection
1317
EndProject
1418
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DependencyInjection", "DependencyInjection", "{3A5AA190-54BF-4A08-8FF8-34846ADBCA43}"
1519
EndProject
@@ -92,11 +96,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{
9296
..\.github\workflows\assimalign.extensions.validation.yml = ..\.github\workflows\assimalign.extensions.validation.yml
9397
EndProjectSection
9498
EndProject
95-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{46C683E7-852F-43B7-A6EF-37819E8ABEEB}"
96-
ProjectSection(SolutionItems) = preProject
97-
validation\Directory.Build.props = validation\Directory.Build.props
98-
EndProjectSection
99-
EndProject
10099
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Assimalign.Extensions.Validation", "validation\src\Assimalign.Extensions.Validation\Assimalign.Extensions.Validation.csproj", "{E2355536-17B2-4627-801C-0F786B534B0A}"
101100
EndProject
102101
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Assimalign.Extensions.Validation.Configurable", "validation\src\Assimalign.Extensions.Validation.Configurable\Assimalign.Extensions.Validation.Configurable.csproj", "{D065205A-4571-4277-8293-080F74609DB5}"
@@ -328,11 +327,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docs", "Docs", "{0CFCC4B2-5
328327
..\README.md = ..\README.md
329328
EndProjectSection
330329
EndProject
331-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{1D6645A0-2632-4AE3-B9D7-0073AE626882}"
332-
ProjectSection(SolutionItems) = preProject
333-
validation\README.md = validation\README.md
334-
EndProjectSection
335-
EndProject
336330
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Assimalign.Extensions.Text.Json.Schema", "text\src\Assimalign.Extensions.Text.Json.Schema\Assimalign.Extensions.Text.Json.Schema.csproj", "{0E7B187F-E07A-42AB-BAF3-5219042FDEDC}"
337331
EndProject
338332
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Assimalign.Extensions.FileProviders.Physical", "file\src\Assimalign.Extensions.FileProviders.Physical\Assimalign.Extensions.FileProviders.Physical.csproj", "{77A745F8-F683-494C-94AF-CD31969CDD01}"
@@ -387,7 +381,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{D88714E8-C3B
387381
EndProject
388382
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{B9965AD8-A2EC-45B9-822D-6409717BE03A}"
389383
EndProject
390-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Assimalign.Extensions.SyntaxBridge", "SyntaxBridge\src\Assimalign.Extensions.SyntaxBridge\Assimalign.Extensions.SyntaxBridge.csproj", "{3C37F00C-5342-4189-819C-BE17176723A8}"
384+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Assimalign.Extensions.SyntaxBridge", "SyntaxBridge\src\Assimalign.Extensions.SyntaxBridge\Assimalign.Extensions.SyntaxBridge.csproj", "{3C37F00C-5342-4189-819C-BE17176723A8}"
385+
EndProject
386+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{9D120398-5ED5-4446-97E8-31971D7E578E}"
387+
EndProject
388+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{A5C31367-7769-4AE9-A7F6-E4C0E06CB124}"
389+
EndProject
390+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Assimalign.Extensions.Core.Tests", "Core\tests\Assimalign.Extensions.Core.Tests\Assimalign.Extensions.Core.Tests.csproj", "{AF63505E-3477-44F4-A6BA-447235EFE9A2}"
391391
EndProject
392392
Global
393393
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -659,6 +659,10 @@ Global
659659
{3C37F00C-5342-4189-819C-BE17176723A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
660660
{3C37F00C-5342-4189-819C-BE17176723A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
661661
{3C37F00C-5342-4189-819C-BE17176723A8}.Release|Any CPU.Build.0 = Release|Any CPU
662+
{AF63505E-3477-44F4-A6BA-447235EFE9A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
663+
{AF63505E-3477-44F4-A6BA-447235EFE9A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
664+
{AF63505E-3477-44F4-A6BA-447235EFE9A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
665+
{AF63505E-3477-44F4-A6BA-447235EFE9A2}.Release|Any CPU.Build.0 = Release|Any CPU
662666
EndGlobalSection
663667
GlobalSection(SolutionProperties) = preSolution
664668
HideSolutionNode = FALSE
@@ -697,7 +701,6 @@ Global
697701
{3758076E-042C-42E8-B2D1-F5AF9229607E} = {60F770D9-148F-40CD-928A-A9EE0E9DF0CF}
698702
{5095EBED-69EE-40F6-B14E-822892CBE3E5} = {60F770D9-148F-40CD-928A-A9EE0E9DF0CF}
699703
{791E9E29-0851-4197-8886-8E5CFC952EFE} = {60F770D9-148F-40CD-928A-A9EE0E9DF0CF}
700-
{46C683E7-852F-43B7-A6EF-37819E8ABEEB} = {60F770D9-148F-40CD-928A-A9EE0E9DF0CF}
701704
{E2355536-17B2-4627-801C-0F786B534B0A} = {3758076E-042C-42E8-B2D1-F5AF9229607E}
702705
{D065205A-4571-4277-8293-080F74609DB5} = {3758076E-042C-42E8-B2D1-F5AF9229607E}
703706
{CE6C7860-AD1E-4666-8DB7-06A53BA918D1} = {3758076E-042C-42E8-B2D1-F5AF9229607E}
@@ -768,6 +771,7 @@ Global
768771
{67C7E836-F500-494D-AC84-9C54195E2B49} = {62FAEAE4-250A-41D6-BF11-74B67387798E}
769772
{5D6918ED-8020-4364-A360-295E95DEB0C5} = {62FAEAE4-250A-41D6-BF11-74B67387798E}
770773
{980C3E2F-3ADD-4274-98CB-D87BA3005B35} = {545C55AB-C4B2-4DC5-B5BF-7F0D2F0B9D59}
774+
{6B0C8BA2-B684-4317-AD21-358E2B082E84} = {0FD0D3ED-6214-4029-825F-1B4E67FC9EF1}
771775
{807ECC6A-8B32-4F1D-BD4C-FFE6E86CAE9B} = {F25953EA-8813-42C6-BDAF-3A8E014B24EA}
772776
{0362A885-3272-4030-A47C-C1B0D3217501} = {EC26D9A0-0DDE-4C2D-9907-674FBF4CDC15}
773777
{77B9192E-4517-4049-8CCE-B7F1B8C91F18} = {EC26D9A0-0DDE-4C2D-9907-674FBF4CDC15}
@@ -777,11 +781,10 @@ Global
777781
{DE5B4249-9CBD-421D-B827-202AF661EE79} = {6BEDBF5B-9C60-4E1A-ACD8-F409B86C3009}
778782
{2033D439-4985-485F-8AEB-86B3E0E8AAD0} = {6BEDBF5B-9C60-4E1A-ACD8-F409B86C3009}
779783
{912F9877-44A7-48FE-98C0-21743B2120C2} = {9606B392-7FC6-423B-9664-F78AFFDE0CA3}
780-
{1410A9ED-D7CA-46FC-A044-BF468BF1B663} = {6B0C8BA2-B684-4317-AD21-358E2B082E84}
784+
{1410A9ED-D7CA-46FC-A044-BF468BF1B663} = {A5C31367-7769-4AE9-A7F6-E4C0E06CB124}
781785
{14B0CF34-649A-4E4B-9F3D-CDC408E1E1CE} = {41D66E26-035B-4F19-9557-99A23D5549BD}
782786
{69DFC51D-8AA4-439F-958D-A495114A11BC} = {6BEDBF5B-9C60-4E1A-ACD8-F409B86C3009}
783787
{C20A249B-68D6-44AE-B1A7-6A4B32882F11} = {6BEDBF5B-9C60-4E1A-ACD8-F409B86C3009}
784-
{1D6645A0-2632-4AE3-B9D7-0073AE626882} = {60F770D9-148F-40CD-928A-A9EE0E9DF0CF}
785788
{0E7B187F-E07A-42AB-BAF3-5219042FDEDC} = {6D8BE87E-1825-42C1-95DF-E3073C444226}
786789
{77A745F8-F683-494C-94AF-CD31969CDD01} = {62FAEAE4-250A-41D6-BF11-74B67387798E}
787790
{09FF8353-264E-4E15-A04A-C311715B9034} = {62FAEAE4-250A-41D6-BF11-74B67387798E}
@@ -810,6 +813,9 @@ Global
810813
{D88714E8-C3BF-4260-A4B1-4F4D65F146FE} = {8458DFE6-5531-4797-A7CE-D2DA41A4FFE6}
811814
{B9965AD8-A2EC-45B9-822D-6409717BE03A} = {8458DFE6-5531-4797-A7CE-D2DA41A4FFE6}
812815
{3C37F00C-5342-4189-819C-BE17176723A8} = {D88714E8-C3BF-4260-A4B1-4F4D65F146FE}
816+
{9D120398-5ED5-4446-97E8-31971D7E578E} = {6B0C8BA2-B684-4317-AD21-358E2B082E84}
817+
{A5C31367-7769-4AE9-A7F6-E4C0E06CB124} = {6B0C8BA2-B684-4317-AD21-358E2B082E84}
818+
{AF63505E-3477-44F4-A6BA-447235EFE9A2} = {9D120398-5ED5-4446-97E8-31971D7E578E}
813819
EndGlobalSection
814820
GlobalSection(ExtensibilityGlobals) = postSolution
815821
SolutionGuid = {FCCF2D81-2E3A-4508-918E-048D800EABE8}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
5-
<ImplicitUsings>enable</ImplicitUsings>
6-
<Nullable>enable</Nullable>
4+
<RootNamespace>Assimalign.Extensions</RootNamespace>
75
</PropertyGroup>
86

97
</Project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace Assimalign.Extensions;
4+
5+
public interface IEither
6+
{
7+
int TypeIndex { get; }
8+
Type Type { get; }
9+
object Value { get; }
10+
}
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
using System;
2+
3+
namespace Assimalign.Extensions;
4+
5+
public record Either<T1, T2> : IEither
6+
{
7+
#region Implicit Conversion From Value
8+
public static implicit operator Either<T1, T2>(T1 value) => new Either<T1, T2>(value);
9+
public static implicit operator Either<T1, T2>(T2 value) => new Either<T1, T2>(value);
10+
#endregion Implicit Conversion From Value
11+
12+
#region Implicit Conversion By Type Swap
13+
public static implicit operator Either<T1, T2>(Either<T2, T1> other)
14+
{
15+
int[] map = new[] { 2, 1 };
16+
return new Either<T1, T2>(map[other._typeIndex - 1], other._value);
17+
}
18+
#endregion Implicit Conversion By Type Swap
19+
20+
#region Implicit Widening Conversions
21+
#endregion Implicit Widening Conversions
22+
23+
#region Constructors
24+
public Either(T1 value) { _value = value; _typeIndex = 1; }
25+
public Either(T2 value) { _value = value; _typeIndex = 2; }
26+
#endregion Constructors
27+
28+
#region Or methods
29+
public Either<T1, T2, T3> Or<T3>() => this
30+
.Match((T1 v1) => new Either<T1, T2, T3>(v1))
31+
.Match((T2 v2) => new Either<T1, T2, T3>(v2));
32+
public Either<T1, T2, T3, T4> Or<T3, T4>() => this
33+
.Match((T1 v1) => new Either<T1, T2, T3, T4>(v1))
34+
.Match((T2 v2) => new Either<T1, T2, T3, T4>(v2));
35+
public Either<T1, T2, T3, T4, T5> Or<T3, T4, T5>() => this
36+
.Match((T1 v1) => new Either<T1, T2, T3, T4, T5>(v1))
37+
.Match((T2 v2) => new Either<T1, T2, T3, T4, T5>(v2));
38+
#endregion Or methods
39+
40+
#region IEither Implementation
41+
int _typeIndex;
42+
object _value;
43+
44+
int IEither.TypeIndex => _typeIndex;
45+
46+
Type IEither.Type => _typeIndex switch
47+
{
48+
1 => typeof(T1),
49+
2 => typeof(T2),
50+
_ => throw new InvalidOperationException()
51+
};
52+
53+
object IEither.Value => _value;
54+
55+
Either(int typeIndex, object value) => (_typeIndex, _value) = (typeIndex, value);
56+
#endregion IEither Implementation
57+
58+
#region Value Casts
59+
T1 AsT1 => (T1)_value;
60+
T2 AsT2 => (T2)_value;
61+
#endregion Value Casts
62+
63+
#region Explicit Casts
64+
public static explicit operator T1(Either<T1, T2> either) => either.AsT1;
65+
public static explicit operator T2(Either<T1, T2> either) => either.AsT2;
66+
#endregion Explicit Casts
67+
68+
#region Switch method
69+
public void Switch(Action<T1> ifT1, Action<T2> ifT2)
70+
{
71+
switch (_typeIndex)
72+
{
73+
case 1: ifT1(AsT1); break;
74+
case 2: ifT2(AsT2); break;
75+
default: throw new InvalidOperationException();
76+
}
77+
}
78+
#endregion Switch method
79+
80+
#region Nonreductive Match
81+
public Either<TResult1, T2> Match<TResult1>(Func<T1, TResult1> ifT1) => _typeIndex switch
82+
{
83+
1 => ifT1(AsT1),
84+
2 => AsT2,
85+
_ => throw new InvalidOperationException()
86+
};
87+
public Either<T1, T2, TResult1> Match<TResult1>
88+
(Func<T1, TResult1> ifT1, Func<T1, bool> when) => _typeIndex switch
89+
{
90+
1 when (when(AsT1)) => ifT1(AsT1),
91+
2 => AsT2,
92+
1 => AsT1,
93+
_ => throw new InvalidOperationException()
94+
};
95+
public Either<T1, TResult2> Match<TResult2>(Func<T2, TResult2> ifT2) => _typeIndex switch
96+
{
97+
1 => AsT1,
98+
2 => ifT2(AsT2),
99+
_ => throw new InvalidOperationException()
100+
};
101+
public Either<T1, T2, TResult2> Match<TResult2>
102+
(Func<T2, TResult2> ifT2, Func<T2, bool> when) => _typeIndex switch
103+
{
104+
1 => AsT1,
105+
2 when (when(AsT2)) => ifT2(AsT2),
106+
2 => AsT2,
107+
_ => throw new InvalidOperationException()
108+
};
109+
#endregion Nonreductive Match
110+
111+
#region Nonreductive Match - Compositional
112+
public Either<TResult1, T2> Match<TResult1>(Func<T1, Either<TResult1, T2>> ifT1) => _typeIndex switch
113+
{
114+
1 => ifT1(AsT1),
115+
2 => AsT2,
116+
_ => throw new InvalidOperationException()
117+
};
118+
public Either<T1, T2, TResult1> Match<TResult1>
119+
(Func<T1, Either<T1, T2, TResult1>> ifT1, Func<T1, bool> when) => _typeIndex switch
120+
{
121+
1 when (when(AsT1)) => ifT1(AsT1),
122+
2 => AsT2,
123+
1 => AsT1,
124+
_ => throw new InvalidOperationException()
125+
};
126+
public Either<T1, TResult2> Match<TResult2>(Func<T2, Either<T1, TResult2>> ifT2) => _typeIndex switch
127+
{
128+
1 => AsT1,
129+
2 => ifT2(AsT2),
130+
_ => throw new InvalidOperationException()
131+
};
132+
public Either<T1, T2, TResult2> Match<TResult2>
133+
(Func<T2, Either<T1, T2, TResult2>> ifT2, Func<T2, bool> when) => _typeIndex switch
134+
{
135+
1 => AsT1,
136+
2 when (when(AsT2)) => ifT2(AsT2),
137+
2 => AsT2,
138+
_ => throw new InvalidOperationException()
139+
};
140+
#endregion Nonreductive Match - Compositional
141+
142+
#region Reductive Match
143+
public T2 Match
144+
(Func<T1, T2> ifT1) => _typeIndex switch
145+
{
146+
1 => ifT1(AsT1),
147+
2 => AsT2,
148+
_ => throw new InvalidOperationException()
149+
};
150+
public Either<T1, T2> Match
151+
(Func<T1, T2> ifT1, Func<T1, bool> when) => _typeIndex switch
152+
{
153+
1 when (when(AsT1)) => ifT1(AsT1),
154+
2 => AsT2,
155+
1 => AsT1,
156+
_ => throw new InvalidOperationException()
157+
};
158+
public T1 Match
159+
(Func<T2, T1> ifT2) => _typeIndex switch
160+
{
161+
1 => AsT1,
162+
2 => ifT2(AsT2),
163+
_ => throw new InvalidOperationException()
164+
};
165+
public Either<T1, T2> Match
166+
(Func<T2, T1> ifT2, Func<T2, bool> when) => _typeIndex switch
167+
{
168+
1 => AsT1,
169+
2 when (when(AsT2)) => ifT2(AsT2),
170+
2 => AsT2,
171+
_ => throw new InvalidOperationException()
172+
};
173+
#endregion Reductive Match
174+
175+
#region Throw Methods
176+
public T2 ThrowIf
177+
(Func<T1, Exception> ifT1) => _typeIndex switch
178+
{
179+
1 => throw ifT1(AsT1),
180+
2 => AsT2,
181+
_ => throw new InvalidOperationException()
182+
};
183+
public Either<T1, T2> ThrowIf
184+
(Func<T1, Exception> ifT1, Func<T1, bool> when) => _typeIndex switch
185+
{
186+
1 when (when(AsT1)) => throw ifT1(AsT1),
187+
2 => AsT2,
188+
1 => AsT1,
189+
_ => throw new InvalidOperationException()
190+
};
191+
public T1 ThrowIf
192+
(Func<T2, Exception> ifT2) => _typeIndex switch
193+
{
194+
1 => AsT1,
195+
2 => throw ifT2(AsT2),
196+
_ => throw new InvalidOperationException()
197+
};
198+
public Either<T1, T2> ThrowIf
199+
(Func<T2, Exception> ifT2, Func<T2, bool> when) => _typeIndex switch
200+
{
201+
1 => AsT1,
202+
2 when (when(AsT2)) => throw ifT2(AsT2),
203+
2 => AsT2,
204+
_ => throw new InvalidOperationException()
205+
};
206+
#endregion Throw Methods
207+
208+
#region If (methods)
209+
public bool If(out T1 @if) => If(out @if, out _);
210+
public bool If(out T1 @if, out T2 @else)
211+
{
212+
switch (_typeIndex)
213+
{
214+
case 1:
215+
@if = AsT1;
216+
@else = default;
217+
return true;
218+
case 2:
219+
@if = default;
220+
@else = AsT2;
221+
return false;
222+
default:
223+
throw new InvalidOperationException();
224+
}
225+
}
226+
public bool If(out T2 @if) => If(out @if, out _);
227+
public bool If(out T2 @if, out T1 @else)
228+
{
229+
switch (_typeIndex)
230+
{
231+
case 1:
232+
@if = default;
233+
@else = AsT1;
234+
return false;
235+
case 2:
236+
@if = AsT2;
237+
@else = default;
238+
return true;
239+
default:
240+
throw new InvalidOperationException();
241+
}
242+
}
243+
#endregion If (methods)
244+
245+
#region ToString
246+
public override string ToString() => $"{((IEither)this).Type.Name}:{_value}";
247+
248+
// For LINQPad:
249+
object ToDump() => new { Type = ((IEither)this).Type, Value = ((IEither)this).Value };
250+
#endregion ToString
251+
}

0 commit comments

Comments
 (0)