Fix issues with nullability and netstandard2.0

This commit is contained in:
Patrik Svensson 2022-11-10 10:21:05 +01:00 committed by Patrik Svensson
parent 7dce4af552
commit a70cc90797
7 changed files with 48 additions and 28 deletions

View File

@ -85,7 +85,7 @@ internal static class CommandValueResolver
} }
// Assign the value to the parameter. // Assign the value to the parameter.
binder.Bind(mapped.Parameter, resolver, converter.ConvertFromInvariantString(mapped.Value)); binder.Bind(mapped.Parameter, resolver, converter.ConvertFromInvariantString(mapped.Value ?? string.Empty));
} }
} }
@ -130,7 +130,13 @@ internal static class CommandValueResolver
if (parameter.ParameterType.IsArray) if (parameter.ParameterType.IsArray)
{ {
// Return a converter for each array item (not the whole array) // Return a converter for each array item (not the whole array)
return TypeDescriptor.GetConverter(parameter.ParameterType.GetElementType()); var elementType = parameter.ParameterType.GetElementType();
if (elementType == null)
{
throw new InvalidOperationException("Could not get element type");
}
return TypeDescriptor.GetConverter(elementType);
} }
if (parameter.IsFlagValue()) if (parameter.IsFlagValue())

View File

@ -29,13 +29,13 @@ internal static class HelpWriter
private sealed class HelpOption private sealed class HelpOption
{ {
public string Short { get; } public string? Short { get; }
public string Long { get; } public string? Long { get; }
public string? Value { get; } public string? Value { get; }
public bool? ValueIsOptional { get; } public bool? ValueIsOptional { get; }
public string? Description { get; } public string? Description { get; }
public HelpOption(string @short, string @long, string? @value, bool? valueIsOptional, string? description) public HelpOption(string? @short, string? @long, string? @value, bool? valueIsOptional, string? description)
{ {
Short = @short; Short = @short;
Long = @long; Long = @long;

View File

@ -17,7 +17,12 @@
<InternalsVisibleTo Include="Spectre.Console.Cli.Tests" /> <InternalsVisibleTo Include="Spectre.Console.Cli.Tests" />
</ItemGroup> </ItemGroup>
<ItemGroup> <PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<AnnotatedReferenceAssemblyVersion>3.0.0</AnnotatedReferenceAssemblyVersion>
<GenerateNullableAttributes>False</GenerateNullableAttributes>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="TunnelVisionLabs.ReferenceAssemblyAnnotator" Version="1.0.0-alpha.160" PrivateAssets="all" /> <PackageReference Include="TunnelVisionLabs.ReferenceAssemblyAnnotator" Version="1.0.0-alpha.160" PrivateAssets="all" />
<PackageDownload Include="Microsoft.NETCore.App.Ref" Version="[$(AnnotatedReferenceAssemblyVersion)]" /> <PackageDownload Include="Microsoft.NETCore.App.Ref" Version="[$(AnnotatedReferenceAssemblyVersion)]" />
<PackageReference Include="Nullable" Version="1.3.1"> <PackageReference Include="Nullable" Version="1.3.1">
@ -26,9 +31,4 @@
</PackageReference> </PackageReference>
</ItemGroup> </ItemGroup>
<PropertyGroup>
<AnnotatedReferenceAssemblyVersion>3.0.0</AnnotatedReferenceAssemblyVersion>
<GenerateNullableAttributes>False</GenerateNullableAttributes>
</PropertyGroup>
</Project> </Project>

View File

@ -4,14 +4,20 @@ internal static class TypeConverterHelper
{ {
public static string ConvertToString<T>(T input) public static string ConvertToString<T>(T input)
{ {
return GetTypeConverter<T>().ConvertToInvariantString(input); var result = GetTypeConverter<T>().ConvertToInvariantString(input);
if (result == null)
{
throw new InvalidOperationException("Could not convert input to a string");
}
return result;
} }
public static bool TryConvertFromString<T>(string input, [MaybeNull] out T result) public static bool TryConvertFromString<T>(string input, [MaybeNull] out T? result)
{ {
try try
{ {
result = (T)GetTypeConverter<T>().ConvertFromInvariantString(input); result = (T?)GetTypeConverter<T>().ConvertFromInvariantString(input);
return true; return true;
} }
catch catch
@ -21,7 +27,7 @@ internal static class TypeConverterHelper
} }
} }
public static bool TryConvertFromStringWithCulture<T>(string input, CultureInfo? info, [MaybeNull] out T result) public static bool TryConvertFromStringWithCulture<T>(string input, CultureInfo? info, [MaybeNull] out T? result)
{ {
try try
{ {
@ -31,7 +37,7 @@ internal static class TypeConverterHelper
} }
else else
{ {
result = (T)GetTypeConverter<T>().ConvertFromString(null!, info, input); result = (T?)GetTypeConverter<T>().ConvertFromString(null!, info, input);
} }
return true; return true;

View File

@ -17,22 +17,25 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="System.Memory" Version="4.5.5" /> <PackageReference Include="System.Memory" Version="4.5.5" />
<PackageReference Include="Wcwidth.Sources" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<AnnotatedReferenceAssemblyVersion>3.0.0</AnnotatedReferenceAssemblyVersion>
<GenerateNullableAttributes>False</GenerateNullableAttributes>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="TunnelVisionLabs.ReferenceAssemblyAnnotator" Version="1.0.0-alpha.160" PrivateAssets="all" /> <PackageReference Include="TunnelVisionLabs.ReferenceAssemblyAnnotator" Version="1.0.0-alpha.160" PrivateAssets="all" />
<PackageDownload Include="Microsoft.NETCore.App.Ref" Version="[$(AnnotatedReferenceAssemblyVersion)]" /> <PackageDownload Include="Microsoft.NETCore.App.Ref" Version="[$(AnnotatedReferenceAssemblyVersion)]" />
<PackageReference Include="Nullable" Version="1.3.1"> <PackageReference Include="Nullable" Version="1.3.1">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Wcwidth.Sources" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup> </ItemGroup>
<PropertyGroup>
<AnnotatedReferenceAssemblyVersion>3.0.0</AnnotatedReferenceAssemblyVersion>
<GenerateNullableAttributes>False</GenerateNullableAttributes>
</PropertyGroup>
<PropertyGroup> <PropertyGroup>
<DefineConstants>$(DefineConstants)TRACE;WCWIDTH_VISIBILITY_INTERNAL</DefineConstants> <DefineConstants>$(DefineConstants)TRACE;WCWIDTH_VISIBILITY_INTERNAL</DefineConstants>
</PropertyGroup> </PropertyGroup>

View File

@ -5,14 +5,20 @@ internal static class FigletFontParser
public static FigletFont Parse(string source) public static FigletFont Parse(string source)
{ {
var lines = source.SplitLines(); var lines = source.SplitLines();
var header = ParseHeader(lines.FirstOrDefault());
var characters = new List<FigletCharacter>(); var headerLine = lines.FirstOrDefault();
if (headerLine == null)
{
throw new InvalidOperationException("Could not read header line");
}
var header = ParseHeader(headerLine);
var index = 32; var index = 32;
var indexOverridden = false; var indexOverridden = false;
var hasOverriddenIndex = false; var hasOverriddenIndex = false;
var buffer = new List<string>(); var buffer = new List<string>();
var characters = new List<FigletCharacter>();
foreach (var line in lines.Skip(header.CommentLines + 1)) foreach (var line in lines.Skip(header.CommentLines + 1))
{ {

View File

@ -107,8 +107,7 @@ public sealed class TableRowCollection : IReadOnlyList<TableRow>
throw new IndexOutOfRangeException("Table row index cannot exceed the number of rows in the table."); throw new IndexOutOfRangeException("Table row index cannot exceed the number of rows in the table.");
} }
var tableRow = _list.ElementAtOrDefault(row); var tableRow = _list.ElementAt(row);
var currentRenderables = tableRow.ToList(); var currentRenderables = tableRow.ToList();
if (column < 0) if (column < 0)