-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2363 from FirelyTeam/feature/2338-fix-nullref-in-…
…enumutil 2338 Fix nullref exception in EnumUtility
- Loading branch information
Showing
2 changed files
with
20 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
/* | ||
#nullable enable | ||
|
||
/* | ||
* Copyright (c) 2014, Firely ([email protected]) and contributors | ||
* See the file CONTRIBUTORS for details. | ||
* | ||
|
@@ -9,11 +11,8 @@ | |
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.ComponentModel; | ||
using System.Collections.Concurrent; | ||
|
||
#nullable enable | ||
|
||
namespace Hl7.Fhir.Utility | ||
{ | ||
/// <summary> | ||
|
@@ -43,13 +42,13 @@ public static string GetDocumentation(this Enum e) => | |
/// <summary> | ||
/// Finds an enumeration value from <paramref name="enumType"/> where the literal is the same as <paramref name="rawValue"/>. | ||
/// </summary> | ||
public static Enum? ParseLiteral(string rawValue, Type enumType, bool ignoreCase = false) | ||
public static Enum? ParseLiteral(string? rawValue, Type enumType, bool ignoreCase = false) | ||
=> getEnumMapping(enumType).ParseLiteral(rawValue, ignoreCase); | ||
|
||
/// <summary> | ||
/// Finds an enumeration value from enum <typeparamref name="T"/> where the literal is the same as <paramref name="rawValue"/>. | ||
/// </summary> | ||
public static T? ParseLiteral<T>(string rawValue, bool ignoreCase = false) where T : struct | ||
public static T? ParseLiteral<T>(string? rawValue, bool ignoreCase = false) where T : struct | ||
=> (T?)(object?)ParseLiteral(rawValue, typeof(T), ignoreCase); | ||
|
||
/// <summary> | ||
|
@@ -88,20 +87,15 @@ public string GetLiteral(Enum value) => | |
? throw new InvalidOperationException($"Should only pass enum values that are member of the given enum: {value} is not a member of {Name}.") | ||
: result; | ||
|
||
public Enum? ParseLiteral(string literal, bool ignoreCase) | ||
public Enum? ParseLiteral(string? literal, bool ignoreCase) | ||
{ | ||
Enum? result; | ||
if (literal is null) return null; | ||
|
||
if (ignoreCase) | ||
{ | ||
_lowercaseLiteralToEnum.TryGetValue(literal.ToLowerInvariant(), out result); | ||
} | ||
else | ||
{ | ||
_literalToEnum.TryGetValue(literal, out result); | ||
} | ||
var success = ignoreCase | ||
? _lowercaseLiteralToEnum.TryGetValue(literal.ToLowerInvariant(), out Enum? result) | ||
: _literalToEnum.TryGetValue(literal, out result); | ||
|
||
return result; | ||
return success ? result : null; | ||
} | ||
|
||
public static EnumMapping Create(Type enumType) | ||
|
@@ -134,4 +128,4 @@ static string getEnumName(Type t) | |
} | ||
} | ||
|
||
#nullable restore | ||
#nullable restore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters