Skip to content

Commit

Permalink
Enable net60 for project SmartFormat (#387)
Browse files Browse the repository at this point in the history
Fix nullability issues caused by net60 target framework
  • Loading branch information
axunonb authored May 13, 2024
1 parent d211c0d commit 10f7842
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/SmartFormat/Extensions/ChooseFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private int GetChosenIndex(IFormattingInfo formattingInfo, string[] chooseOption
t => t.Equals(valAsString, StringComparison.OrdinalIgnoreCase));
}

valAsString = currentValueString = formattingInfo.CurrentValue.ToString();
valAsString = currentValueString = formattingInfo.CurrentValue.ToString()!;

return Array.FindIndex(chooseOptions,
t => AreEqual(t, valAsString, formattingInfo.FormatDetails.Settings.CaseSensitivity));
Expand Down
6 changes: 3 additions & 3 deletions src/SmartFormat/Extensions/DictionarySource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ private bool TryGetDictionaryValue(object obj, string key, StringComparison comp

if (!TryGetDictionaryProperties(obj.GetType(), out var propertyTuple)) return false;

var keys = (IEnumerable) propertyTuple!.Value.KeyProperty.GetValue(obj);
var keys = (IEnumerable) propertyTuple!.Value.KeyProperty.GetValue(obj)!;

foreach (var k in keys)
{
if (!k.ToString().Equals(key, comparison))
if (!k.ToString()!.Equals(key, comparison))
continue;

value = propertyTuple.Value.ItemProperty.GetValue(obj, new [] { k });
Expand All @@ -125,7 +125,7 @@ private bool TryGetDictionaryProperties(Type type, out (PropertyInfo KeyProperty
}

// get Key and Item properties of the dictionary
propertyTuple = (type.GetProperty(nameof(IDictionary.Keys)), type.GetProperty("Item"));
propertyTuple = (type.GetProperty(nameof(IDictionary.Keys)), type.GetProperty("Item"))!;

System.Diagnostics.Debug.Assert(propertyTuple.Value.KeyProperty != null && propertyTuple.Value.ItemProperty != null, "Key and Item properties must not be null");

Expand Down
2 changes: 1 addition & 1 deletion src/SmartFormat/Extensions/IsMatchFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public bool TryEvaluateFormat(IFormattingInfo formattingInfo)
}

var regEx = new Regex(expression, RegexOptions, TimeSpan.FromMilliseconds(500));
var match = regEx.Match(formattingInfo.CurrentValue.ToString());
var match = regEx.Match(formattingInfo.CurrentValue.ToString()!);

if (!match.Success)
{
Expand Down
10 changes: 5 additions & 5 deletions src/SmartFormat/Extensions/WellKnownExtensionTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ internal static int GetIndexToInsert<T>(IList<T> currentExtensions, T extensionT
var wellKnownList = typeof(T).IsAssignableFrom(typeof(ISource)) ? Sources : Formatters;

// Unknown extensions will add to the end
if (!wellKnownList.TryGetValue(extensionToInsert.GetType().FullName, out var indexOfNewExt))
if (!wellKnownList.TryGetValue(extensionToInsert.GetType().FullName!, out var indexOfNewExt))
return currentExtensions.Count;

for (var i = currentExtensions.Count - 1; i >= 0; i--)
{
var found = wellKnownList.TryGetValue(currentExtensions[i].GetType().FullName, out var index);
var found = wellKnownList.TryGetValue(currentExtensions[i].GetType().FullName!, out var index);
if (!found) continue;

if (index > indexOfNewExt)
Expand Down Expand Up @@ -169,9 +169,9 @@ static bool SingletonCondition(Type t) => typeof(T).IsAssignableFrom(t)
internal static T CreateInstanceForType<T>((Type ExtensionType, bool IsSingleton) wellKnown)
{
if (wellKnown.IsSingleton)
return (T) wellKnown.ExtensionType.GetProperty("Instance", BindingFlags.Static | BindingFlags.Public)!.GetValue(wellKnown);
return (T) wellKnown.ExtensionType.GetProperty("Instance", BindingFlags.Static | BindingFlags.Public)!.GetValue(wellKnown)!;

// It's a transient type
return (T) Activator.CreateInstance(Type.GetType(wellKnown.ExtensionType.AssemblyQualifiedName!)!);
return (T) Activator.CreateInstance(Type.GetType(wellKnown.ExtensionType.AssemblyQualifiedName!)!)!;
}
}
}
1 change: 0 additions & 1 deletion src/SmartFormat/SmartFormat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ It can format various data sources into a string with a minimal, intuitive synta
It uses extensions to provide named placeholders, localization, pluralization, gender conjugation, and list and time formatting.
</Description>
<AssemblyTitle>SmartFormat</AssemblyTitle>
<TargetFrameworks>netstandard2.0;netstandard2.1;net461</TargetFrameworks>
<AssemblyName>SmartFormat</AssemblyName>
<PackageId>SmartFormat</PackageId>
<PackageTags>string-format stringformat template templating string-composition smartformat smart-format netstandard netcore netframework csharp c-sharp</PackageTags>
Expand Down
12 changes: 6 additions & 6 deletions src/SmartFormat/ZString/ZStringWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ public override void Write(char[] buffer, int index, int count)
}

/// <inheritdoc/>
public override void Write(string value)
public override void Write(string? value)
{
_zw.Write(value);
_zw.Write(value ?? string.Empty);
}

/// <inheritdoc/>
Expand All @@ -99,9 +99,9 @@ public override Task WriteAsync(char value)
}

/// <inheritdoc/>
public override Task WriteAsync(string value)
public override Task WriteAsync(string? value)
{
return _zw.WriteAsync(value);
return _zw.WriteAsync(value ?? string.Empty);
}

/// <inheritdoc/>
Expand All @@ -117,9 +117,9 @@ public override Task WriteLineAsync(char value)
}

/// <inheritdoc/>
public override Task WriteLineAsync(string value)
public override Task WriteLineAsync(string? value)
{
return _zw.WriteLineAsync(value);
return _zw.WriteLineAsync(value ?? string.Empty);
}

/// <inheritdoc/>
Expand Down

0 comments on commit 10f7842

Please sign in to comment.