diff --git a/Nustache.Core.Tests/Describe_ValueGetter.cs b/Nustache.Core.Tests/Describe_ValueGetter.cs index 110e3e7..db82fbc 100644 --- a/Nustache.Core.Tests/Describe_ValueGetter.cs +++ b/Nustache.Core.Tests/Describe_ValueGetter.cs @@ -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 target = new List() { "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; diff --git a/Nustache.Core/ValueGetter.cs b/Nustache.Core/ValueGetter.cs index d79638b..27f1576 100644 --- a/Nustache.Core/ValueGetter.cs +++ b/Nustache.Core/ValueGetter.cs @@ -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() diff --git a/Nustache.Core/ValueGetterFactory.cs b/Nustache.Core/ValueGetterFactory.cs index a21b7fb..6e2fef8 100644 --- a/Nustache.Core/ValueGetterFactory.cs +++ b/Nustache.Core/ValueGetterFactory.cs @@ -85,7 +85,8 @@ public static class ValueGetterFactories new DictionaryValueGetterFactory(), new MethodInfoValueGatterFactory(), new PropertyInfoValueGetterFactory(), - new FieldInfoValueGetterFactory() + new FieldInfoValueGetterFactory(), + new ListValueByIndexGetterFactory() }; public static ValueGetterFactoryCollection Factories @@ -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; + } + } }