Skip to content

Commit b6ef2f8

Browse files
committed
Further nullable improvements
1 parent 462e14c commit b6ef2f8

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

Source/Schema.NET/DateTimeHelper.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace Schema.NET
22
{
33
using System;
4+
using System.Diagnostics.CodeAnalysis;
45

56
/// <summary>
67
/// Helper for parsing strings into <see cref="DateTime"/> or <see cref="DateTimeOffset"/>
@@ -45,7 +46,12 @@ public static bool ContainsTimeOffset(string? input)
4546
/// <param name="input">The input string</param>
4647
/// <param name="result">The result date and time</param>
4748
/// <returns>True if the input string was able to be parsed into a <see cref="DateTime"/></returns>
48-
public static bool TryParseMSDateTime(string? input, out DateTime result)
49+
public static bool TryParseMSDateTime(
50+
#if NETCOREAPP3_1_OR_GREATER
51+
[NotNullWhen(true)]
52+
#endif
53+
string? input,
54+
out DateTime result)
4955
{
5056
if (input is not null &&
5157
input.StartsWith(MSDateStringStart, StringComparison.Ordinal) &&
@@ -54,7 +60,7 @@ public static bool TryParseMSDateTime(string? input, out DateTime result)
5460
var dateTimeStartIndex = MSDateStringStart.Length;
5561
var dateTimeLength = input.IndexOf(MSDateStringEnd, StringComparison.Ordinal) - dateTimeStartIndex;
5662

57-
#if NETCOREAPP3_1
63+
#if NETCOREAPP3_1_OR_GREATER
5864
var timeValue = input.AsSpan().Slice(dateTimeStartIndex, dateTimeLength);
5965
#else
6066
var timeValue = input.Substring(dateTimeStartIndex, dateTimeLength);
@@ -77,7 +83,12 @@ public static bool TryParseMSDateTime(string? input, out DateTime result)
7783
/// <param name="input">The input string</param>
7884
/// <param name="result">The result date and time with offset</param>
7985
/// <returns>True if the input string was able to be parsed into a <see cref="DateTimeOffset"/></returns>
80-
public static bool TryParseMSDateTimeOffset(string? input, out DateTimeOffset result)
86+
public static bool TryParseMSDateTimeOffset(
87+
#if NETCOREAPP3_1_OR_GREATER
88+
[NotNullWhen(true)]
89+
#endif
90+
string? input,
91+
out DateTimeOffset result)
8192
{
8293
if (input is not null &&
8394
input.StartsWith(MSDateStringStart, StringComparison.Ordinal) &&
@@ -88,7 +99,7 @@ public static bool TryParseMSDateTimeOffset(string? input, out DateTimeOffset re
8899
var dateTimeLength = offsetIndex - dateTimeStartIndex;
89100
var offsetLength = input.IndexOf(MSDateStringEnd, offsetIndex, StringComparison.Ordinal) - offsetIndex;
90101

91-
#if NETCOREAPP3_1
102+
#if NETCOREAPP3_1_OR_GREATER
92103
var timeValue = input.AsSpan().Slice(dateTimeStartIndex, dateTimeLength);
93104
var offsetType = input.AsSpan().Slice(offsetIndex, 1);
94105
var offsetValue = input.AsSpan().Slice(offsetIndex + 1, offsetLength - 1);

Source/Schema.NET/EnumHelper.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace Schema.NET
22
{
33
using System;
4+
using System.Diagnostics.CodeAnalysis;
45

56
/// <summary>
67
/// Helper for parsing strings into Enum values.
@@ -15,7 +16,13 @@ internal static class EnumHelper
1516
/// <param name="value">The string representation of the name or numeric value of one or more enumerated constants.</param>
1617
/// <param name="result">When this method returns true, an object containing an enumeration constant representing the parsed value.</param>
1718
/// <returns><see langword="true"/> if the conversion succeeded; <see langword="false"/> otherwise.</returns>
18-
public static bool TryParse(Type enumType, string? value, out object? result)
19+
public static bool TryParse(
20+
Type enumType,
21+
#if NETCOREAPP3_1_OR_GREATER
22+
[NotNullWhen(true)]
23+
#endif
24+
string? value,
25+
out object? result)
1926
{
2027
#if NETSTANDARD2_0 || NET472 || NET461
2128
try

Source/Schema.NET/ValuesJsonConverter.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace Schema.NET
33
using System;
44
using System.Collections.Generic;
55
using System.Diagnostics;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Globalization;
78
using System.Reflection;
89
using System.Xml;
@@ -395,7 +396,12 @@ private static bool TryProcessTokenAsType(JsonReader reader, Type targetType, ou
395396
return success;
396397
}
397398

398-
private static bool TryGetConcreteType(string typeName, out Type? type)
399+
private static bool TryGetConcreteType(
400+
string typeName,
401+
#if NETCOREAPP3_1_OR_GREATER
402+
[NotNullWhen(true)]
403+
#endif
404+
out Type? type)
399405
{
400406
if (BuiltInThingTypeLookup.TryGetValue(typeName, out type))
401407
{
@@ -409,7 +415,7 @@ private static bool TryGetConcreteType(string typeName, out Type? type)
409415
if (localType is not null && ThingInterfaceTypeInfo.IsAssignableFrom(localType.GetTypeInfo()))
410416
{
411417
type = localType;
412-
return !(type is null);
418+
return type is not null;
413419
}
414420
else
415421
{

0 commit comments

Comments
 (0)