Skip to content

Commit

Permalink
#72 - Initial commit for migrating to C# 10/.NET 6
Browse files Browse the repository at this point in the history
This is a rough and ready "hack" commit. I've simply added "?" and "!" where needed to silence the compilers complains about nullability, ignored 124 intellisense errors, commented out test code that didn't make sense and fixed one test that failed because .NET 6's  double.TryParse() behaves differently to NetF.
  • Loading branch information
DavidArno committed Sep 22, 2021
1 parent 022ae9d commit 6d0f98b
Show file tree
Hide file tree
Showing 51 changed files with 197 additions and 185 deletions.
8 changes: 4 additions & 4 deletions src/SuccincT.JSON/JSON/EitherConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ public override object ReadJson(JsonReader reader,
? valueJson.ToObject(type1, serializer)
: valueJson.ToObject(type2, serializer);

return Activator.CreateInstance(eitherType, value);
return Activator.CreateInstance(eitherType, value)!;
}

public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
var eitherType = value!.GetType();
var leftProperty = eitherType.GetProperty("IsLeft");
var isLeft = (bool)leftProperty.GetValue(value, null);
var isLeft = (bool)leftProperty?.GetValue(value, null)!;
var variantValue = isLeft
? eitherType.GetProperty("Left").GetValue(value, null)
: eitherType.GetProperty("Right").GetValue(value, null);
? eitherType?.GetProperty("Left")?.GetValue(value, null)
: eitherType?.GetProperty("Right")?.GetValue(value, null);

writer.WriteStartObject();
writer.WritePropertyName("isLeft");
Expand Down
8 changes: 4 additions & 4 deletions src/SuccincT.JSON/JSON/OptionConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ public override object ReadJson(JsonReader reader,

var typedMethod = optionType.GetMethod(hasValue ? "Some" : "None");

if (!hasValue) return typedMethod.Invoke(null, null);
if (!hasValue) return typedMethod?.Invoke(null, null)!;

var rawValue = jsonObject["value"] ?? throw new JsonException("No 'value' found for \"Option\" value.");
var value = rawValue.ToObject(type, serializer);

return typedMethod.Invoke(null, new[] {value});
return typedMethod?.Invoke(null, new[] {value})!;
}

public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
var optionType = value!.GetType();
var hasValueProperty = optionType.GetProperty("HasValue");
var hasValue = (bool)hasValueProperty.GetValue(value, null);
var hasValue = (bool)hasValueProperty?.GetValue(value, null)!;

writer.WriteStartObject();
writer.WritePropertyName("hasValue");
Expand All @@ -49,7 +49,7 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
{
writer.WritePropertyName("value");
var valueProperty = optionType.GetProperty("Value");
var optionValue = valueProperty.GetValue(value, null);
var optionValue = valueProperty?.GetValue(value, null);
serializer.Serialize(writer, optionValue);
}

Expand Down
10 changes: 5 additions & 5 deletions src/SuccincT.JSON/JSON/SuccessConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ public override object ReadJson(JsonReader reader,

var failure = rawFailure.ToObject(type, serializer);
var rawMethod = typeof(Success).GetMethod("CreateFailure");
var typedMethod = rawMethod.MakeGenericMethod(type);
return typedMethod.Invoke(null, new[] { failure });
var typedMethod = rawMethod?.MakeGenericMethod(type);
return typedMethod?.Invoke(null, new[] { failure })!;
}

var rawSuccessType = typeof(Success<>);
var successType = rawSuccessType.MakeGenericType(type);
return Activator.CreateInstance(successType);
return Activator.CreateInstance(successType)!;
}

public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
var successType = value!.GetType();
var isFailureProperty = successType.GetProperty("IsFailure");
var isFailure = (bool)isFailureProperty.GetValue(value, null);
var isFailure = (bool)isFailureProperty?.GetValue(value, null)!;

writer.WriteStartObject();
writer.WritePropertyName("isFailure");
Expand All @@ -53,7 +53,7 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
{
writer.WritePropertyName("failure");
var failureProperty = successType.GetProperty("Failure");
var failure = failureProperty.GetValue(value, null);
var failure = failureProperty?.GetValue(value, null);
serializer.Serialize(writer, failure);
}

Expand Down
4 changes: 2 additions & 2 deletions src/SuccincT.JSON/JSON/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ internal static class TypeExtensions
{
internal static bool IsGenericType(this Type type) => type.GetTypeInfo().IsGenericType;

internal static MethodInfo GetMethod(this Type type, string name) => type.GetTypeInfo().GetDeclaredMethod(name);
internal static MethodInfo GetMethod(this Type type, string name) => type.GetTypeInfo().GetDeclaredMethod(name)!;

internal static PropertyInfo GetProperty(this Type type, string name) =>
type.GetTypeInfo().GetDeclaredProperty(name);
type.GetTypeInfo().GetDeclaredProperty(name)!;
}
}
8 changes: 4 additions & 4 deletions src/SuccincT.JSON/JSON/UnionOf2Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ public override object ReadJson(JsonReader reader,
.With(Case2).Do(_ => valueJson.ToObject(type2, serializer))
.Result();

return Activator.CreateInstance(unionType, value);
return Activator.CreateInstance(unionType, value)!;
}

public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
var unionType = value!.GetType();
var caseProperty = unionType.GetProperty("Case");
var variant = (Variant)caseProperty.GetValue(value, null);
var variant = (Variant)caseProperty?.GetValue(value, null)!;
var variantValue = variant.Match().To<object>()
.With(Case1).Do(_ => unionType.GetProperty("Case1").GetValue(value, null))
.With(Case2).Do(_ => unionType.GetProperty("Case2").GetValue(value, null))
.With(Case1).Do(_ => unionType.GetProperty("Case1")?.GetValue(value, null)!)
.With(Case2).Do(_ => unionType.GetProperty("Case2")?.GetValue(value, null)!)
.Result();

writer.WriteStartObject();
Expand Down
10 changes: 5 additions & 5 deletions src/SuccincT.JSON/JSON/UnionOf3Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ public override object ReadJson(JsonReader reader,
.With(Case3).Do(_ => valueJson.ToObject(type3, serializer))
.Result();

return Activator.CreateInstance(unionType, value);
return Activator.CreateInstance(unionType, value)!;
}

public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
var unionType = value!.GetType();
var caseProperty = unionType.GetProperty("Case");
var variant = (Variant)caseProperty.GetValue(value, null);
var variant = (Variant)caseProperty?.GetValue(value, null)!;
var variantValue = variant.Match().To<object>()
.With(Case1).Do(_ => unionType.GetProperty("Case1").GetValue(value, null))
.With(Case2).Do(_ => unionType.GetProperty("Case2").GetValue(value, null))
.With(Case3).Do(_ => unionType.GetProperty("Case3").GetValue(value, null))
.With(Case1).Do(_ => unionType?.GetProperty("Case1")?.GetValue(value, null)!)
.With(Case2).Do(_ => unionType?.GetProperty("Case2")?.GetValue(value, null)!)
.With(Case3).Do(_ => unionType?.GetProperty("Case3")?.GetValue(value, null)!)
.Result();

writer.WriteStartObject();
Expand Down
12 changes: 6 additions & 6 deletions src/SuccincT.JSON/JSON/UnionOf4Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ public override object ReadJson(JsonReader reader,
.With(Case4).Do(_ => valueJson.ToObject(type4, serializer))
.Result();

return Activator.CreateInstance(unionType, value);
return Activator.CreateInstance(unionType, value)!;
}

public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
var unionType = value!.GetType();
var caseProperty = unionType.GetProperty("Case");
var variant = (Variant)caseProperty.GetValue(value, null);
var variant = (Variant)caseProperty?.GetValue(value, null)!;
var variantValue = variant.Match().To<object>()
.With(Case1).Do(_ => unionType.GetProperty("Case1").GetValue(value, null))
.With(Case2).Do(_ => unionType.GetProperty("Case2").GetValue(value, null))
.With(Case3).Do(_ => unionType.GetProperty("Case3").GetValue(value, null))
.With(Case4).Do(_ => unionType.GetProperty("Case4").GetValue(value, null))
.With(Case1).Do(_ => unionType?.GetProperty("Case1")?.GetValue(value, null)!)
.With(Case2).Do(_ => unionType?.GetProperty("Case2")?.GetValue(value, null)!)
.With(Case3).Do(_ => unionType?.GetProperty("Case3")?.GetValue(value, null)!)
.With(Case4).Do(_ => unionType?.GetProperty("Case4")?.GetValue(value, null)!)
.Result();

writer.WriteStartObject();
Expand Down
2 changes: 1 addition & 1 deletion src/SuccincT.JSON/SuccincT.JSON.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net6</TargetFramework>
<PackageId>SuccincT.JSON</PackageId>
<Product>SuccincT.JSON</Product>
<Authors>David Arno</Authors>
Expand Down
2 changes: 1 addition & 1 deletion src/SuccincT/Functional/Unit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace SuccincT.Functional
{
public override int GetHashCode() => 0;

public override bool Equals(object obj) => obj is Unit;
public override bool Equals(object? obj) => obj is Unit;

public override string ToString() => "()";

Expand Down
19 changes: 9 additions & 10 deletions src/SuccincT/Functional/WithExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static Option<T> TryCopy<T>(this T @object) where T : notnull

if (constructorParameterValues.Length != constructorParameters.Count) return Option<T>.None();

var newObject = Activator.CreateInstance(typeof(T), constructorParameterValues);
var newObject = Activator.CreateInstance(typeof(T), constructorParameterValues)!;
var destWriteProperties = cachedTypeInfo.Properties.Except(cachedTypeInfo.ReadOnlyProperties);

var propertiesToOverwrite =
Expand Down Expand Up @@ -72,7 +72,7 @@ public static T Copy<T>(this T @object) where T : notnull
"non-writable properties to be set via that constructor.");
}

var newObject = Activator.CreateInstance(typeof(T), constructorParameterValues);
var newObject = Activator.CreateInstance(typeof(T), constructorParameterValues)!;
var destWriteProperties = cachedTypeInfo.Properties.Except(cachedTypeInfo.ReadOnlyProperties);

var propertiesToOverwrite = sourceReadProperties
Expand Down Expand Up @@ -189,13 +189,12 @@ public static T With<T, TProps>(this T itemToCopy, TProps propertiesToUpdate)
private static object[] GetConstructorParameterValuesForCopy<T>(
T @object,
IEnumerable<PropertyInfo> sourceReadProperties,
IEnumerable<ParameterInfo>
constructorParameters) where T : notnull
IEnumerable<ParameterInfo> constructorParameters) where T : notnull
{
return constructorParameters.Select(p => sourceReadProperties.TryFirst(x => AreLinked(x, p)))
.Where(x => x.HasValue).Select(x => x.Value)
.Select(sourceReadProperty => sourceReadProperty.GetValue(@object, null))
.ToArray();
.ToArray()!;
}

private static T CreateNewObjectApplyingUpdates<T, TProps>(
Expand All @@ -206,7 +205,7 @@ private static T CreateNewObjectApplyingUpdates<T, TProps>(
IEnumerable<(PropertyInfo Value, PropertyInfo PropToUpdate)> propsToSetFromUpdateData)
where T : notnull where TProps : class
{
var newObject = Activator.CreateInstance(typeof(T), constructorParameterValues);
var newObject = Activator.CreateInstance(typeof(T), constructorParameterValues)!;

foreach (var propertyToOverwrite in propsToSetFromSourceObject)
{
Expand Down Expand Up @@ -267,12 +266,12 @@ private static object[] MapUpdateValuesToConstructorParameters<T, TProps>(
return updateProperties
.TryFirst(ptu => AreLinked(ptu, p))
.Match<Option<object>>()
.Some().Do(ptu => Option<object>.Some(ptu.GetValue(propertiesToUpdate, null)))
.Some().Do(ptu => Option<object>.Some(ptu.GetValue(propertiesToUpdate, null)!))
.None().Do(() => {
return sourceReadProperties
.TryFirst(sp => AreLinked(sp, p))
.Match<Option<object>>()
.Some().Do(sp => Option<object>.Some(sp.GetValue(@object, null)))
.Some().Do(sp => Option<object>.Some(sp.GetValue(@object, null)!))
.None().Do(Option<object>.None)
.Result();
}).Result();
Expand All @@ -297,7 +296,7 @@ orderby constructor.Parameters.Count descending
}

private static CachedTypeInfo GetCachedTypeInfo(Type type)
=> CachedTypeInfoDetails.GetOrAddValue(type.FullName, () => new CachedTypeInfo(type));
=> CachedTypeInfoDetails.GetOrAddValue(type.FullName!, () => new CachedTypeInfo(type));

private static T GetOrAddValue<T>(this Dictionary<string, T> dictionary, string key, Func<T> createValue)
{
Expand All @@ -322,7 +321,7 @@ private static bool AreLinked(MemberInfo memberInfo, PropertyInfo propertyInfo)
private static bool AreLinked(ParameterInfo parameterInfo, PropertyInfo propertyInfo) =>
string.Equals(parameterInfo.Name, propertyInfo.Name, StringComparison.CurrentCultureIgnoreCase);

private static void CopyPropertyValue<T>(T from, PropertyInfo property, T to) where T : class
private static void CopyPropertyValue<T>(T from, PropertyInfo property, T to) where T : notnull
=> property.SetValue(to, property.GetValue(from, null));

private static void CopyPropertyValue<T1, T2>(T1 from,
Expand Down
2 changes: 1 addition & 1 deletion src/SuccincT/Options/Option{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static Option<T> Some(T value) => value is {} valid

public T ValueOrDefault => HasValue ? _value : default!;

public override bool Equals(object obj)
public override bool Equals(object? obj)
=> obj is Option<T> option ? EqualsOption(option) : obj is null && !HasValue;

private bool EqualsOption(Option<T> other)
Expand Down
2 changes: 1 addition & 1 deletion src/SuccincT/Options/Success{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ISuccessFuncMatcher<T, TResult> Match<TResult>()

public ISuccessActionMatcher<T> Match() => new SuccessMatcher<T, Unit>(CreateUnion(), this);

public override bool Equals(object obj) =>
public override bool Equals(object? obj) =>
obj is Success<T> other &&
other.IsFailure == IsFailure &&
(IsFailure && other.Failure!.Equals(_error) || !IsFailure);
Expand Down
2 changes: 1 addition & 1 deletion src/SuccincT/Options/ValueOrError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void Deconstruct(out ValueOrErrorState valueOrError, out string? value, o

public override string ToString() => HasValue ? $"Value of {_value}" : $"Error of {_error}";

public override bool Equals(object obj) =>
public override bool Equals(object? obj) =>
obj is ValueOrError testObject && testObject._error == _error && testObject._value == _value;

public override int GetHashCode() => HasValue ? GetItemHashCode(_value) : GetItemHashCode(_error);
Expand Down
2 changes: 1 addition & 1 deletion src/SuccincT/Options/ValueOrError{TV,TE}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void Deconstruct(out ValueOrErrorState valueOrError, out TValue value, ou

public override string ToString() => HasValue ? $"Value of {_value}" : $"Error of {_error}";

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
switch (obj)
{
Expand Down
2 changes: 1 addition & 1 deletion src/SuccincT/PatternMatchers/Any.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public readonly struct Any

public override int GetHashCode() => 0;

public override bool Equals(object obj) => obj is Any;
public override bool Equals(object? obj) => obj is Any;

public override string ToString() => "*";

Expand Down
4 changes: 2 additions & 2 deletions src/SuccincT/PatternMatchers/Matcher{T1.TR}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ IActionMatcher<T1> IActionWhereHandler<IActionMatcher<T1>, T1>.Do(Action<T1> act
IFuncMatcher<T1, TResult> IFuncWithHandler<IFuncMatcher<T1, TResult>, T1, TResult>.Do(
Func<T1, TResult> action)
{
var values = _withValues;
var values = _withValues!;
RecordFunction(x => values.Any(y => EqualityComparer<T1>.Default.Equals(x, y)), action);
return this;
}
Expand Down Expand Up @@ -153,7 +153,7 @@ void IActionMatcherAfterElse.Exec()
{
if (!_functionSelector.DetermineResult(_item).HasValue)
{
_elseFunction!(_item);
_ = _elseFunction!(_item);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/SuccincT/SuccincT.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net6</TargetFramework>
<PackageId>SuccincT</PackageId>
<Product>SuccincT</Product>
<Authors>David Arno</Authors>
Expand Down
2 changes: 1 addition & 1 deletion src/SuccincT/Unions/Either.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public IEitherActionPatternMatcher<TLeft, TRight> Match()
public void Deconstruct(out EitherState state, out TLeft leftValue, out TRight rightValue)
=> (state, leftValue, rightValue) = (IsLeft ? EitherState.Left : EitherState.Right, _left, _right);

public override bool Equals(object obj) => obj is Either<TLeft, TRight> either && EithersEqual(this, either);
public override bool Equals(object? obj) => obj is Either<TLeft, TRight> either && EithersEqual(this, either);

public override int GetHashCode()
=> IsLeft ? _left is {} left ? left.GetHashCode() : 0 : _right is {} right ? right.GetHashCode() : 0;
Expand Down
2 changes: 1 addition & 1 deletion src/SuccincT/Unions/None.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public readonly struct None

public override string ToString() => "!none!";

public override bool Equals(object obj) => obj is None;
public override bool Equals(object? obj) => obj is None;

public override int GetHashCode() => 0;

Expand Down
4 changes: 2 additions & 2 deletions src/SuccincT/Unions/Union{T1,T2,T3,T4}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public bool TryCaseOf<T>(out T value)
_ => (false, default)
};

value = valueTemp;
value = valueTemp!;
return result;
}

Expand All @@ -78,7 +78,7 @@ public IUnionActionPatternMatcher<T1, T2, T3, T4> Match()
public void Deconstruct(out Variant variant, out T1 case1, out T2 case2, out T3 case3, out T4 case4)
=> (variant, case1, case2, case3, case4) = (Case, _value1, _value2, _value3, _value4);

public override bool Equals(object obj) => obj is Union < T1, T2, T3, T4> union && UnionsEqual(union);
public override bool Equals(object? obj) => obj is Union < T1, T2, T3, T4> union && UnionsEqual(union);

public override int GetHashCode() =>
Case switch
Expand Down
Loading

0 comments on commit 6d0f98b

Please sign in to comment.