Skip to content

Commit

Permalink
Adding enum parser that is case insensitive.
Browse files Browse the repository at this point in the history
  • Loading branch information
rozele authored and GantMan committed Sep 29, 2016
1 parent 7972a70 commit 1aa1950
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 33 deletions.
1 change: 1 addition & 0 deletions ReactNative/ReactNative.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
<Compile Include="ReactContextInitializedEventArgs.cs" />
<Compile Include="ReactPage.cs" />
<Compile Include="ReactRootView.cs" />
<Compile Include="Reflection\EnumHelpers.cs" />
<Compile Include="UIManager\BorderedContentControl.cs" />
<Compile Include="UIManager\BorderExtensions.cs" />
<Compile Include="UIManager\ColorHelpers.cs" />
Expand Down
44 changes: 44 additions & 0 deletions ReactNative/Reflection/EnumHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace ReactNative.Reflection
{
static class EnumHelpers
{
private static readonly ConcurrentDictionary<Type, IReadOnlyDictionary<string, object>> s_enumCache =
new ConcurrentDictionary<Type, IReadOnlyDictionary<string, object>>();

public static T Parse<T>(string value)
{
var lookup = s_enumCache.GetOrAdd(
typeof(T),
type => Enum.GetValues(type)
.Cast<object>()
.ToDictionary(
e => Normalize(e.ToString()),
e => e));

var result = default(object);
if (!lookup.TryGetValue(Normalize(value), out result))
{
throw new ArgumentOutOfRangeException(
nameof(value),
string.Format(
CultureInfo.InvariantCulture,
"Invalid value '{0}' for type '{1}'.",
value,
typeof(T)));
}

return (T)result;
}

private static string Normalize(string value)
{
return value.ToLowerInvariant().Replace("-", "");
}
}
}
40 changes: 7 additions & 33 deletions ReactNative/UIManager/LayoutShadowNode.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using Facebook.CSSLayout;
using ReactNative.Reflection;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace ReactNative.UIManager
{
Expand Down Expand Up @@ -97,7 +96,7 @@ public void SetFlex(float flex)
public void SetFlexDirection(string flexDirection)
{
FlexDirection = flexDirection != null
? Parse<CSSFlexDirection>(flexDirection, nameof(flexDirection))
? EnumHelpers.Parse<CSSFlexDirection>(flexDirection)
: CSSFlexDirection.Column;
}

Expand All @@ -109,7 +108,7 @@ public void SetFlexDirection(string flexDirection)
public void SetFlexWrap(string flexWrap)
{
Wrap = flexWrap != null
? Parse<CSSWrap>(flexWrap, nameof(flexWrap))
? EnumHelpers.Parse<CSSWrap>(flexWrap)
: CSSWrap.NoWrap;
}

Expand All @@ -121,7 +120,7 @@ public void SetFlexWrap(string flexWrap)
public void SetAlignSelf(string alignSelf)
{
AlignSelf = alignSelf != null
? Parse<CSSAlign>(alignSelf, nameof(alignSelf))
? EnumHelpers.Parse<CSSAlign>(alignSelf)
: CSSAlign.Auto;
}

Expand All @@ -133,7 +132,7 @@ public void SetAlignSelf(string alignSelf)
public void SetAlignItems(string alignItems)
{
AlignItems = alignItems != null
? Parse<CSSAlign>(alignItems, nameof(alignItems))
? EnumHelpers.Parse<CSSAlign>(alignItems)
: CSSAlign.Stretch;
}

Expand All @@ -145,7 +144,7 @@ public void SetAlignItems(string alignItems)
public void SetJustifyContent(string justifyContent)
{
JustifyContent = justifyContent != null
? Parse<CSSJustify>(justifyContent, nameof(justifyContent))
? EnumHelpers.Parse<CSSJustify>(justifyContent)
: CSSJustify.FlexStart;
}

Expand Down Expand Up @@ -212,33 +211,8 @@ public void SetBorderWidth(int index, float borderWidth)
public void SetPosition(string position)
{
PositionType = position != null
? Parse<CSSPositionType>(position, nameof(position))
? EnumHelpers.Parse<CSSPositionType>(position)
: CSSPositionType.Relative;
}

private static T Parse<T>(string value, string paramName)
{
var lookup = s_enumCache.GetOrAdd(
typeof(T),
type => Enum.GetValues(type)
.Cast<object>()
.ToDictionary(
e => e.ToString().ToLowerInvariant(),
e => e));

var result = default(object);
if (!lookup.TryGetValue(value.ToLowerInvariant().Replace("-", ""), out result))
{
throw new ArgumentOutOfRangeException(
paramName,
string.Format(
CultureInfo.InvariantCulture,
"Invalid value '{0}' for type '{1}'.",
value,
typeof(T)));
}

return (T)result;
}
}
}

0 comments on commit 1aa1950

Please sign in to comment.