Skip to content

Commit

Permalink
Merge pull request #77 from Romanx/feat/ListValuesByIndex
Browse files Browse the repository at this point in the history
Feature: List Values by Index
  • Loading branch information
Romanx committed Jul 9, 2014
2 parents 957a95f + f379e9b commit bdd6f8c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
21 changes: 21 additions & 0 deletions Nustache.Core.Tests/Describe_ValueGetter.cs
Expand Up @@ -152,6 +152,27 @@ public void It_gets_XmlNode_multiple_child_element_values_as_a_list()
Assert.AreEqual("text2", elements[1].InnerText);
}

[Test]
public void It_gets_ListValueByIndex_values_from_array()
{
string[] target = new[] { "hello", "world" };
Assert.AreEqual("hello", ValueGetter.GetValue(target, "0"));
}

[Test]
public void It_gets_ListValueByIndex_values_from_List()
{
List<string> target = new List<string>() { "hello", "world" };
Assert.AreEqual("hello", ValueGetter.GetValue(target, "0"));
}

[Test]
public void It_fails_ListValueByIndex_values_from_List()
{
string[] target = new[] { "hello", "world" };
Assert.AreEqual(ValueGetter.NoValue, ValueGetter.GetValue(target, "2"));
}

public class ReadWriteInts
{
public int IntField = -1;
Expand Down
16 changes: 16 additions & 0 deletions Nustache.Core/ValueGetter.cs
Expand Up @@ -226,6 +226,22 @@ public override object GetValue()
}
}

internal class ListValueByIndexGetter : ValueGetter
{
private readonly IList _target;
private readonly int _index;

public ListValueByIndexGetter(IList target, int index)
{
_target = target;
_index = index;
}
public override object GetValue()
{
return _target[_index];
}
}

internal class NoValueGetter : ValueGetter
{
public override object GetValue()
Expand Down
30 changes: 29 additions & 1 deletion Nustache.Core/ValueGetterFactory.cs
Expand Up @@ -85,7 +85,8 @@ public static class ValueGetterFactories
new DictionaryValueGetterFactory(),
new MethodInfoValueGatterFactory(),
new PropertyInfoValueGetterFactory(),
new FieldInfoValueGetterFactory()
new FieldInfoValueGetterFactory(),
new ListValueByIndexGetterFactory()
};

public static ValueGetterFactoryCollection Factories
Expand Down Expand Up @@ -247,4 +248,31 @@ private static Type GetSupportedInterfaceType(Type targetType)
}
}
}

internal class ListValueByIndexGetterFactory : ValueGetterFactory
{
public override ValueGetter GetValueGetter(object target, Type targetType, string name)
{
//Both Lists and Arrays internally can be assigned to IList.
if (typeof(IList).IsAssignableFrom(targetType))
{
var listTarget = target as IList;
int arrayIndex;
bool parseSuccess = Int32.TryParse(name, out arrayIndex);

/*
* There is an index as per the success of the parse, it is not greater than the count
* (minus one since index is zero referenced) or less than zero.
*/
if(parseSuccess &&
!(arrayIndex > (listTarget.Count - 1)) &&
!(arrayIndex < 0))
{
return new ListValueByIndexGetter(listTarget, arrayIndex);
}
}

return null;
}
}
}

0 comments on commit bdd6f8c

Please sign in to comment.