diff --git a/src/Hl7.Fhir.Base/Utility/EnumUtility.cs b/src/Hl7.Fhir.Base/Utility/EnumUtility.cs index 62f2357f30..956305e7b8 100644 --- a/src/Hl7.Fhir.Base/Utility/EnumUtility.cs +++ b/src/Hl7.Fhir.Base/Utility/EnumUtility.cs @@ -1,4 +1,6 @@ -/* +#nullable enable + +/* * Copyright (c) 2014, Firely (info@fire.ly) 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 { /// @@ -43,13 +42,13 @@ public static string GetDocumentation(this Enum e) => /// /// Finds an enumeration value from where the literal is the same as . /// - 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); /// /// Finds an enumeration value from enum where the literal is the same as . /// - public static T? ParseLiteral(string rawValue, bool ignoreCase = false) where T : struct + public static T? ParseLiteral(string? rawValue, bool ignoreCase = false) where T : struct => (T?)(object?)ParseLiteral(rawValue, typeof(T), ignoreCase); /// @@ -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 \ No newline at end of file +#nullable restore diff --git a/src/Hl7.Fhir.Support.Tests/Utility/EnumMappingTest.cs b/src/Hl7.Fhir.Support.Tests/Utility/EnumMappingTest.cs index d671f39a44..41db9fee0c 100644 --- a/src/Hl7.Fhir.Support.Tests/Utility/EnumMappingTest.cs +++ b/src/Hl7.Fhir.Support.Tests/Utility/EnumMappingTest.cs @@ -6,6 +6,7 @@ * available at https://raw.githubusercontent.com/FirelyTeam/firely-net-sdk/master/LICENSE */ +using FluentAssertions; using Hl7.Fhir.Utility; using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Diagnostics; @@ -137,5 +138,12 @@ enum X a, b } + + [TestMethod] + public void NullLiteralHandling() + { + EnumUtility.ParseLiteral(null).Should().BeNull(); + EnumUtility.ParseLiteral(null, ignoreCase: true).Should().BeNull(); + } } }