Skip to content

Commit af6b458

Browse files
committed
can ignore or rename a field
1 parent f823f3a commit af6b458

File tree

4 files changed

+75
-3
lines changed

4 files changed

+75
-3
lines changed

Liquid.NET.Tests/Utils/LiquidValueConverterTests.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,38 @@ public void It_Should_Convert_An_Object_With_Null_Field()
219219

220220
// Assert
221221
Assert.That(objAsHash["objfield1"].HasValue, Is.False);
222+
}
223+
224+
[Test]
225+
public void It_Should_Rename_A_Field()
226+
{
227+
// Act
228+
var testClass = new ClassWithAttributes { Ignored = "ignored", Ok = "ok", Renamed="renamed"};
229+
230+
var result = _converter.Convert(testClass);
231+
232+
Assert.That(result.HasValue);
233+
var objAsHash = (LiquidHash)result.Value;
234+
235+
// Assert
236+
Assert.That(objAsHash["somethingelse"].HasValue);
237+
Assert.That(objAsHash["somethingelse"].Value, Is.EqualTo(LiquidString.Create("renamed")));
238+
}
239+
240+
[Test]
241+
public void It_Should_Ignore_A_Field()
242+
{
243+
// Act
244+
var testClass = new ClassWithAttributes { Ignored = "ignored", Ok = "ok", Renamed = "renamed" };
245+
246+
var result = _converter.Convert(testClass);
247+
248+
Assert.That(result.HasValue);
249+
var objAsHash = (LiquidHash)result.Value;
222250

251+
// Assert
252+
Assert.That(objAsHash.ContainsKey("ignored"), Is.False);
253+
Assert.That(objAsHash.ContainsKey("ok"), Is.True);
223254
}
224255

225256
public class TestClass
@@ -230,8 +261,18 @@ public class TestClass
230261
public Object ObjField1 {get; set;}
231262

232263
}
233-
234264

265+
public class ClassWithAttributes
266+
{
267+
[LiquidIgnore]
268+
public String Ignored { get; set; }
269+
270+
[LiquidName("somethingelse")]
271+
public String Renamed { get; set; }
272+
273+
public String Ok { get; set; }
274+
275+
}
235276

236277
}
237278
}

Liquid.NET/Liquid.NET.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@
215215
<Compile Include="src\TemplateContext.cs" />
216216
<Compile Include="src\Symbols\IASTVisitor.cs" />
217217
<Compile Include="src\Utils\Either.cs" />
218+
<Compile Include="src\Utils\LiquidValueConverterAttributes.cs" />
218219
<Compile Include="src\Utils\LiquidExpressionResult.cs" />
219220
<Compile Include="src\Utils\LiquidVersionAttribute.cs" />
220221
<Compile Include="src\Utils\Option.cs" />

Liquid.NET/src/Utils/LiquidValueConverter.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.CodeDom;
32
using System.Collections;
43
using System.Collections.Generic;
54
using System.Linq;
@@ -38,8 +37,9 @@ private Option<ILiquidValue> FromObject(Object obj)
3837
var newHash = new LiquidHash();
3938
var kvps = obj.GetType()
4039
.GetProperties()
40+
.Where(property => !property.GetCustomAttributes<LiquidIgnoreAttribute>().Any())
4141
.Select(property => new KeyValuePair<String, Option<ILiquidValue>> (
42-
property.Name.ToLower(),
42+
GetPropertyName(property),
4343
GetPropertyValue(obj, property)));
4444

4545
foreach (var kvp in kvps)
@@ -57,6 +57,14 @@ private Option<ILiquidValue> FromObject(Object obj)
5757
return newHash;
5858
}
5959

60+
private string GetPropertyName(PropertyInfo property)
61+
{
62+
var overriddenName = property.GetCustomAttributes<LiquidNameAttribute>().FirstOrDefault();
63+
64+
return overriddenName == null ? property.Name.ToLowerInvariant() : overriddenName.Key.ToLowerInvariant();
65+
66+
}
67+
6068
private Option<ILiquidValue> GetPropertyValue(Object obj, PropertyInfo property)
6169
{
6270
var val = property.GetGetMethod().Invoke(obj, null);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
3+
namespace Liquid.NET.Utils
4+
{
5+
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
6+
public class LiquidIgnoreAttribute : Attribute
7+
{
8+
}
9+
10+
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
11+
public class LiquidNameAttribute : Attribute
12+
{
13+
private readonly string _key;
14+
public String Key { get { return _key; }}
15+
16+
public LiquidNameAttribute(String key)
17+
{
18+
_key = key;
19+
}
20+
}
21+
22+
}

0 commit comments

Comments
 (0)