Skip to content

Commit 877d269

Browse files
committed
Adding enum parser that is case insensitive.
1 parent c45d16a commit 877d269

File tree

3 files changed

+52
-33
lines changed

3 files changed

+52
-33
lines changed

ReactWindows/ReactNative/ReactNative.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170
<Compile Include="ReactContextInitializedEventArgs.cs" />
171171
<Compile Include="ReactPage.cs" />
172172
<Compile Include="ReactRootView.cs" />
173+
<Compile Include="Reflection\EnumHelpers.cs" />
173174
<Compile Include="UIManager\BorderedContentControl.cs" />
174175
<Compile Include="UIManager\BorderExtensions.cs" />
175176
<Compile Include="UIManager\ColorHelpers.cs" />
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Collections.Generic;
4+
using System.Globalization;
5+
using System.Linq;
6+
7+
namespace ReactNative.Reflection
8+
{
9+
static class EnumHelpers
10+
{
11+
private static readonly ConcurrentDictionary<Type, IReadOnlyDictionary<string, object>> s_enumCache =
12+
new ConcurrentDictionary<Type, IReadOnlyDictionary<string, object>>();
13+
14+
public static T Parse<T>(string value)
15+
{
16+
var lookup = s_enumCache.GetOrAdd(
17+
typeof(T),
18+
type => Enum.GetValues(type)
19+
.Cast<object>()
20+
.ToDictionary(
21+
e => Normalize(e.ToString()),
22+
e => e));
23+
24+
var result = default(object);
25+
if (!lookup.TryGetValue(Normalize(value), out result))
26+
{
27+
throw new ArgumentOutOfRangeException(
28+
nameof(value),
29+
string.Format(
30+
CultureInfo.InvariantCulture,
31+
"Invalid value '{0}' for type '{1}'.",
32+
value,
33+
typeof(T)));
34+
}
35+
36+
return (T)result;
37+
}
38+
39+
private static string Normalize(string value)
40+
{
41+
return value.ToLowerInvariant().Replace("-", "");
42+
}
43+
}
44+
}

ReactWindows/ReactNative/UIManager/LayoutShadowNode.cs

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
using Facebook.CSSLayout;
2+
using ReactNative.Reflection;
23
using System;
34
using System.Collections.Concurrent;
45
using System.Collections.Generic;
5-
using System.Globalization;
6-
using System.Linq;
76

87
namespace ReactNative.UIManager
98
{
@@ -97,7 +96,7 @@ public void SetFlex(float flex)
9796
public void SetFlexDirection(string flexDirection)
9897
{
9998
FlexDirection = flexDirection != null
100-
? Parse<CSSFlexDirection>(flexDirection, nameof(flexDirection))
99+
? EnumHelpers.Parse<CSSFlexDirection>(flexDirection)
101100
: CSSFlexDirection.Column;
102101
}
103102

@@ -109,7 +108,7 @@ public void SetFlexDirection(string flexDirection)
109108
public void SetFlexWrap(string flexWrap)
110109
{
111110
Wrap = flexWrap != null
112-
? Parse<CSSWrap>(flexWrap, nameof(flexWrap))
111+
? EnumHelpers.Parse<CSSWrap>(flexWrap)
113112
: CSSWrap.NoWrap;
114113
}
115114

@@ -121,7 +120,7 @@ public void SetFlexWrap(string flexWrap)
121120
public void SetAlignSelf(string alignSelf)
122121
{
123122
AlignSelf = alignSelf != null
124-
? Parse<CSSAlign>(alignSelf, nameof(alignSelf))
123+
? EnumHelpers.Parse<CSSAlign>(alignSelf)
125124
: CSSAlign.Auto;
126125
}
127126

@@ -133,7 +132,7 @@ public void SetAlignSelf(string alignSelf)
133132
public void SetAlignItems(string alignItems)
134133
{
135134
AlignItems = alignItems != null
136-
? Parse<CSSAlign>(alignItems, nameof(alignItems))
135+
? EnumHelpers.Parse<CSSAlign>(alignItems)
137136
: CSSAlign.Stretch;
138137
}
139138

@@ -145,7 +144,7 @@ public void SetAlignItems(string alignItems)
145144
public void SetJustifyContent(string justifyContent)
146145
{
147146
JustifyContent = justifyContent != null
148-
? Parse<CSSJustify>(justifyContent, nameof(justifyContent))
147+
? EnumHelpers.Parse<CSSJustify>(justifyContent)
149148
: CSSJustify.FlexStart;
150149
}
151150

@@ -212,33 +211,8 @@ public void SetBorderWidth(int index, float borderWidth)
212211
public void SetPosition(string position)
213212
{
214213
PositionType = position != null
215-
? Parse<CSSPositionType>(position, nameof(position))
214+
? EnumHelpers.Parse<CSSPositionType>(position)
216215
: CSSPositionType.Relative;
217216
}
218-
219-
private static T Parse<T>(string value, string paramName)
220-
{
221-
var lookup = s_enumCache.GetOrAdd(
222-
typeof(T),
223-
type => Enum.GetValues(type)
224-
.Cast<object>()
225-
.ToDictionary(
226-
e => e.ToString().ToLowerInvariant(),
227-
e => e));
228-
229-
var result = default(object);
230-
if (!lookup.TryGetValue(value.ToLowerInvariant().Replace("-", ""), out result))
231-
{
232-
throw new ArgumentOutOfRangeException(
233-
paramName,
234-
string.Format(
235-
CultureInfo.InvariantCulture,
236-
"Invalid value '{0}' for type '{1}'.",
237-
value,
238-
typeof(T)));
239-
}
240-
241-
return (T)result;
242-
}
243217
}
244218
}

0 commit comments

Comments
 (0)