Skip to content

Commit

Permalink
Further nullable improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
RehanSaeed committed Mar 23, 2021
1 parent 462e14c commit b6ef2f8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
19 changes: 15 additions & 4 deletions Source/Schema.NET/DateTimeHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Schema.NET
{
using System;
using System.Diagnostics.CodeAnalysis;

/// <summary>
/// Helper for parsing strings into <see cref="DateTime"/> or <see cref="DateTimeOffset"/>
Expand Down Expand Up @@ -45,7 +46,12 @@ public static bool ContainsTimeOffset(string? input)
/// <param name="input">The input string</param>
/// <param name="result">The result date and time</param>
/// <returns>True if the input string was able to be parsed into a <see cref="DateTime"/></returns>
public static bool TryParseMSDateTime(string? input, out DateTime result)
public static bool TryParseMSDateTime(
#if NETCOREAPP3_1_OR_GREATER
[NotNullWhen(true)]
#endif
string? input,
out DateTime result)
{
if (input is not null &&
input.StartsWith(MSDateStringStart, StringComparison.Ordinal) &&
Expand All @@ -54,7 +60,7 @@ public static bool TryParseMSDateTime(string? input, out DateTime result)
var dateTimeStartIndex = MSDateStringStart.Length;
var dateTimeLength = input.IndexOf(MSDateStringEnd, StringComparison.Ordinal) - dateTimeStartIndex;

#if NETCOREAPP3_1
#if NETCOREAPP3_1_OR_GREATER
var timeValue = input.AsSpan().Slice(dateTimeStartIndex, dateTimeLength);
#else
var timeValue = input.Substring(dateTimeStartIndex, dateTimeLength);
Expand All @@ -77,7 +83,12 @@ public static bool TryParseMSDateTime(string? input, out DateTime result)
/// <param name="input">The input string</param>
/// <param name="result">The result date and time with offset</param>
/// <returns>True if the input string was able to be parsed into a <see cref="DateTimeOffset"/></returns>
public static bool TryParseMSDateTimeOffset(string? input, out DateTimeOffset result)
public static bool TryParseMSDateTimeOffset(
#if NETCOREAPP3_1_OR_GREATER
[NotNullWhen(true)]
#endif
string? input,
out DateTimeOffset result)
{
if (input is not null &&
input.StartsWith(MSDateStringStart, StringComparison.Ordinal) &&
Expand All @@ -88,7 +99,7 @@ public static bool TryParseMSDateTimeOffset(string? input, out DateTimeOffset re
var dateTimeLength = offsetIndex - dateTimeStartIndex;
var offsetLength = input.IndexOf(MSDateStringEnd, offsetIndex, StringComparison.Ordinal) - offsetIndex;

#if NETCOREAPP3_1
#if NETCOREAPP3_1_OR_GREATER
var timeValue = input.AsSpan().Slice(dateTimeStartIndex, dateTimeLength);
var offsetType = input.AsSpan().Slice(offsetIndex, 1);
var offsetValue = input.AsSpan().Slice(offsetIndex + 1, offsetLength - 1);
Expand Down
9 changes: 8 additions & 1 deletion Source/Schema.NET/EnumHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Schema.NET
{
using System;
using System.Diagnostics.CodeAnalysis;

/// <summary>
/// Helper for parsing strings into Enum values.
Expand All @@ -15,7 +16,13 @@ internal static class EnumHelper
/// <param name="value">The string representation of the name or numeric value of one or more enumerated constants.</param>
/// <param name="result">When this method returns true, an object containing an enumeration constant representing the parsed value.</param>
/// <returns><see langword="true"/> if the conversion succeeded; <see langword="false"/> otherwise.</returns>
public static bool TryParse(Type enumType, string? value, out object? result)
public static bool TryParse(
Type enumType,
#if NETCOREAPP3_1_OR_GREATER
[NotNullWhen(true)]
#endif
string? value,
out object? result)
{
#if NETSTANDARD2_0 || NET472 || NET461
try
Expand Down
10 changes: 8 additions & 2 deletions Source/Schema.NET/ValuesJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace Schema.NET
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
using System.Xml;
Expand Down Expand Up @@ -395,7 +396,12 @@ private static bool TryProcessTokenAsType(JsonReader reader, Type targetType, ou
return success;
}

private static bool TryGetConcreteType(string typeName, out Type? type)
private static bool TryGetConcreteType(
string typeName,
#if NETCOREAPP3_1_OR_GREATER
[NotNullWhen(true)]
#endif
out Type? type)
{
if (BuiltInThingTypeLookup.TryGetValue(typeName, out type))
{
Expand All @@ -409,7 +415,7 @@ private static bool TryGetConcreteType(string typeName, out Type? type)
if (localType is not null && ThingInterfaceTypeInfo.IsAssignableFrom(localType.GetTypeInfo()))
{
type = localType;
return !(type is null);
return type is not null;
}
else
{
Expand Down

0 comments on commit b6ef2f8

Please sign in to comment.