Skip to content

Commit 116b117

Browse files
committed
Use case sensitivity when parsing string to TimeUnit enum
1 parent 332f65b commit 116b117

File tree

3 files changed

+4
-5
lines changed

3 files changed

+4
-5
lines changed

Diff for: src/Nest/CommonAbstractions/Extensions/Extensions.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ internal static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> items, Fu
7272
}
7373

7474
internal static ConcurrentDictionary<string, object> _enumCache = new ConcurrentDictionary<string, object>();
75-
internal static T? ToEnum<T>(this string str) where T : struct
75+
internal static T? ToEnum<T>(this string str, StringComparison comparison = StringComparison.OrdinalIgnoreCase) where T : struct
7676
{
7777
var enumType = typeof(T);
7878
var key = $"{enumType.Name}.{str}";
@@ -82,7 +82,7 @@ internal static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> items, Fu
8282

8383
foreach (var name in Enum.GetNames(enumType))
8484
{
85-
if (name.Equals(str, StringComparison.OrdinalIgnoreCase))
85+
if (name.Equals(str, comparison))
8686
{
8787
var v = (T)Enum.Parse(enumType, name, true);
8888
_enumCache.TryAdd(key, v);
@@ -99,7 +99,6 @@ internal static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> items, Fu
9999
}
100100

101101
var alternativeEnumMemberAttribute = enumFieldInfo.GetCustomAttribute<AlternativeEnumMemberAttribute>();
102-
103102
if (alternativeEnumMemberAttribute?.Value == str)
104103
{
105104
var v = (T) Enum.Parse(enumType, name);

Diff for: src/Nest/CommonOptions/DateMath/DateMath.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static DateMath FromString(string dateMath)
7171

7272
if (match.Groups["rounding"].Success)
7373
{
74-
var rounding = match.Groups["rounding"].Value.Substring(1).ToEnum<TimeUnit>();
74+
var rounding = match.Groups["rounding"].Value.Substring(1).ToEnum<TimeUnit>(StringComparison.Ordinal);
7575
if (rounding.HasValue)
7676
return math.RoundTo(rounding.Value);
7777
}

Diff for: src/Nest/CommonOptions/TimeUnit/Time.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public Time(string timeUnit)
5151

5252
this.Factor = double.Parse(match.Groups["factor"].Value, CultureInfo.InvariantCulture);
5353
this.Interval = match.Groups["interval"].Success
54-
? match.Groups["interval"].Value.ToEnum<TimeUnit>()
54+
? match.Groups["interval"].Value.ToEnum<TimeUnit>(StringComparison.Ordinal)
5555
: TimeUnit.Millisecond;
5656

5757
this.Milliseconds = GetMilliseconds(this.Interval.Value, this.Factor.Value);

0 commit comments

Comments
 (0)