Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility w/ C# versions below 8 #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 48 additions & 43 deletions Generator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@
using System;
using System.Collections.Generic;

var sourceRoot = GetFullPath(Combine(GetDirectoryName(GetExecutingAssembly().Location)!, @"..\..\..\.."));
var sourceRoot = GetFullPath(Combine(GetDirectoryName(GetExecutingAssembly().Location)!, $"..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}.."));

for (var i = 1; i < 10; i++) {
var output = GetContent(true, i);
var outpath = Combine(sourceRoot, $"OneOf\\OneOfT{i - 1}.generated.cs");
var outpath = Combine(sourceRoot, $"OneOf{Path.DirectorySeparatorChar}OneOfT{i - 1}.generated.cs");
File.WriteAllText(outpath, output);

var output2 = GetContent(false, i);
var outpath2 = Combine(sourceRoot, $"OneOf\\OneOfBaseT{i - 1}.generated.cs");
var outpath2 = Combine(sourceRoot, $"OneOf{Path.DirectorySeparatorChar}OneOfBaseT{i - 1}.generated.cs");
File.WriteAllText(outpath2, output2);
}

for (var i = 10; i < 33; i++) {
var output3 = GetContent(true, i);
var outpath3 = Combine(sourceRoot, $"OneOf.Extended\\OneOfT{i - 1}.generated.cs");
var outpath3 = Combine(sourceRoot, $"OneOf.Extended{Path.DirectorySeparatorChar}OneOfT{i - 1}.generated.cs");
File.WriteAllText(outpath3, output3);

var output4 = GetContent(false, i);
var outpath4 = Combine(sourceRoot, $"OneOf.Extended\\OneOfBaseT{i - 1}.generated.cs");
var outpath4 = Combine(sourceRoot, $"OneOf.Extended{Path.DirectorySeparatorChar}OneOfBaseT{i - 1}.generated.cs");
File.WriteAllText(outpath4, output4);
}

Expand Down Expand Up @@ -67,13 +67,14 @@ namespace OneOf
}}"
)}

public object Value =>
_index switch
{{
{RangeJoined(@"
", j => $"{j} => _value{j},")}
_ => throw new InvalidOperationException()
}};
public object Value {{ get {{
switch (_index)
{{
{RangeJoined(@"
", j => $"case {j}: return _value{j};")}
default: throw new InvalidOperationException();
}}
}} }}

public int Index => _index;

Expand Down Expand Up @@ -125,15 +126,15 @@ public TResult Match<TResult>({RangeJoined(", ", e => $"Func<T{e}, TResult> f{e}
{{
throw new ArgumentNullException(nameof(mapFunc));
}}
return _index switch
switch (_index)
{{
{genericArgs.Joined(@"
", (x, k) =>
x == bindToType ?
$"{k} => mapFunc(As{x})," :
$"{k} => As{x},")}
_ => throw new InvalidOperationException()
}};
$"case {k}: return mapFunc(As{x});" :
$"case {k}: return As{x};")}
default: throw new InvalidOperationException();
}}
}}";
}))}
");
Expand All @@ -145,33 +146,35 @@ public TResult Match<TResult>({RangeJoined(", ", e => $"Func<T{e}, TResult> f{e}
var genericArgWithSkip = Range(0, i).ExceptSingle(j).Joined(", ", e => $"T{e}");
var remainderType = i == 2 ? genericArgWithSkip : $"OneOf<{genericArgWithSkip}>";
return $@"
public bool TryPickT{j}(out T{j} value, out {remainderType} remainder)
{{
value = IsT{j} ? AsT{j} : default;
remainder = _index switch
public bool TryPickT{j}(out T{j} value, out {remainderType} remainder)
{{
value = IsT{j} ? AsT{j} : default;
switch (_index)
{{
{RangeJoined(@"
", k =>
", k =>
k == j ?
$"{k} => default," :
$"{k} => AsT{k},")}
_ => throw new InvalidOperationException()
}};
return this.IsT{j};
}}";
$"case {k}: {{ remainder = default; break; }}" :
$"case {k}: {{ remainder = AsT{k}; break; }}")}
default: throw new InvalidOperationException();
}}
return this.IsT{j};
}}";
})
);
}

sb.AppendLine($@"
bool Equals({className}<{genericArg}> other) =>
_index == other._index &&
_index switch
bool Equals({className}<{genericArg}> other) {{
var check1 = _index == other._index;
if (!check1) {{ return false; }}
switch (_index)
{{
{RangeJoined(@"
", j => @$"{j} => Equals(_value{j}, other._value{j}),")}
_ => false
", j => @$"case {j}: return check1 && Equals(_value{j}, other._value{j});")}
default: return false;
}};
}}

public override bool Equals(object obj)
{{
Expand All @@ -190,23 +193,25 @@ public override bool Equals(object obj)
)}
}}

public override string ToString() =>
_index switch {{
public override string ToString() {{
switch (_index) {{
{RangeJoined(@"
", j => $"{j} => FormatValue(_value{j}),")}
_ => throw new InvalidOperationException(""Unexpected index, which indicates a problem in the OneOf codegen."")
}};
", j => $"case {j}: return FormatValue(_value{j});")}
default: throw new InvalidOperationException(""Unexpected index, which indicates a problem in the OneOf codegen."");
}}
}}

public override int GetHashCode()
{{
unchecked
{{
int hashCode = _index switch
int hashCode;
switch (_index)
{{
{RangeJoined(@"
", j => $"{j} => _value{j}?.GetHashCode(),")}
_ => 0
}} ?? 0;
", j => $"case {j}: {{ hashCode = _value{j}?.GetHashCode() ?? 0; break; }}")}
default: {{ hashCode = 0; break; }}
}}
return (hashCode*397) ^ _index;
}}
}}
Expand All @@ -228,4 +233,4 @@ public static string Joined<T>(this IEnumerable<T> source, string delimiter, Fun
}
public static IEnumerable<T> ExceptSingle<T>(this IEnumerable<T> source, T single) => source.Except(Repeat(single, 1));
public static void AppendLineTo(this string? s, StringBuilder sb) => sb.AppendLine(s);
}
}
Loading