Skip to content

Commit 71fbcf1

Browse files
committed
object.ToLiquid() extension
1 parent af6b458 commit 71fbcf1

File tree

7 files changed

+99
-6
lines changed

7 files changed

+99
-6
lines changed

Liquid.NET.Tests/Liquid.NET.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
<Compile Include="Tags\UnlessBlockTagTests.cs" />
181181
<Compile Include="TemplateContextTests.cs" />
182182
<Compile Include="Utils\EitherTests.cs" />
183+
<Compile Include="Utils\LiquidConversionExtensionsTests.cs" />
183184
<Compile Include="Utils\OptionTests.cs" />
184185
<Compile Include="Utils\LiquidValueConverterTests.cs" />
185186
<Compile Include="Utils\RegistryTests.cs" />
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Linq;
3+
using Liquid.NET.Utils;
4+
using NUnit.Framework;
5+
6+
namespace Liquid.NET.Tests.Utils
7+
{
8+
[TestFixture]
9+
public class LiquidConversionExtensionsTests
10+
{
11+
[Test]
12+
public void It_Should_Extend_An_Object()
13+
{
14+
// Arrange
15+
var obj = new LiquidValueConverterTests.ClassWithAttributes { Ok = "To Liquid" };
16+
var templateContext = new TemplateContext()
17+
.ErrorWhenValueMissing()
18+
.DefineLocalVariable("test", obj.ToLiquid());
19+
20+
// Act
21+
var parserResult = LiquidTemplate.Create("Hello {{ test.ok }}");
22+
Assert.That(parserResult.HasParsingErrors, Is.False);
23+
var renderingResult = parserResult.LiquidTemplate.Render(templateContext);
24+
Assert.That(renderingResult.HasParsingErrors, Is.False);
25+
Console.WriteLine(String.Join(",", renderingResult.RenderingErrors.Select(x => x.Message)));
26+
Assert.That(renderingResult.HasRenderingErrors, Is.False);
27+
28+
// Assert
29+
Assert.That(renderingResult.Result, Is.EqualTo("Hello To Liquid"));
30+
31+
}
32+
}
33+
}

Liquid.NET.Tests/Utils/LiquidValueConverterTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ public void It_Should_Convert_Float_To_Decimal()
8383
Assert.That(val.Value.Value, Is.EqualTo(123.2m));
8484
}
8585

86+
[Test]
87+
public void It_Should_Convert_Nullable_Int_To_NumericValue()
88+
{
89+
int? nullableInt = 123;
90+
var val = _converter.Convert(nullableInt);
91+
Assert.That(val.HasValue, Is.True);
92+
Assert.That(val.Value, Is.TypeOf<IntLiquidNumeric>());
93+
Assert.That(val.Value.Value, Is.EqualTo(123));
94+
}
95+
96+
8697
[Test]
8798
public void It_Should_Covert_BigInteger()
8899
{
@@ -253,6 +264,27 @@ public void It_Should_Ignore_A_Field()
253264
Assert.That(objAsHash.ContainsKey("ok"), Is.True);
254265
}
255266

267+
[Test]
268+
[TestCase(null)]
269+
[TestCase("test")]
270+
public void It_Should_Ignore_A_Null_Field_When_IgnoreIfNull(String fieldValue)
271+
{
272+
// Act
273+
var testClass = new ClassWithAttributes { SomeField = fieldValue};
274+
275+
var result = _converter.Convert(testClass);
276+
277+
Assert.That(result.HasValue);
278+
var objAsHash = (LiquidHash)result.Value;
279+
280+
// Assert
281+
Assert.That(objAsHash.ContainsKey("notnull"), Is.EqualTo(fieldValue != null));
282+
if (objAsHash.ContainsKey("notnull"))
283+
{
284+
Assert.That(objAsHash.Value, Is.EqualTo(fieldValue.ToLiquid()));
285+
}
286+
}
287+
256288
public class TestClass
257289
{
258290
public String Field1 {get; set;}
@@ -270,6 +302,10 @@ public class ClassWithAttributes
270302
[LiquidName("somethingelse")]
271303
public String Renamed { get; set; }
272304

305+
[LiquidName("notnull")]
306+
[LiquidIgnoreIfNull]
307+
public String SomeField { get; set; }
308+
273309
public String Ok { get; set; }
274310

275311
}

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\LiquidConversionExtensions.cs" />
218219
<Compile Include="src\Utils\LiquidValueConverterAttributes.cs" />
219220
<Compile Include="src\Utils\LiquidExpressionResult.cs" />
220221
<Compile Include="src\Utils\LiquidVersionAttribute.cs" />
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
using Liquid.NET.Constants;
4+
5+
namespace Liquid.NET.Utils
6+
{
7+
public static class LiquidConversionExtensions
8+
{
9+
public static Option<ILiquidValue> ToLiquid(this object obj)
10+
{
11+
return new LiquidValueConverter().Convert(obj);
12+
}
13+
}
14+
}

Liquid.NET/src/Utils/LiquidValueConverter.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ private Option<ILiquidValue> FromObject(Object obj)
3737
var newHash = new LiquidHash();
3838
var kvps = obj.GetType()
3939
.GetProperties()
40-
.Where(property => !property.GetCustomAttributes<LiquidIgnoreAttribute>().Any())
40+
.Where(property => !(property.GetCustomAttributes<LiquidIgnoreAttribute>().Any()
41+
|| (property.GetCustomAttributes<LiquidIgnoreIfNullAttribute>().Any()
42+
&& ReferenceEquals(property.GetGetMethod().Invoke(obj, null), null))))
4143
.Select(property => new KeyValuePair<String, Option<ILiquidValue>> (
4244
GetPropertyName(property),
4345
GetPropertyValue(obj, property)));
@@ -167,7 +169,7 @@ private Boolean IsDictionary(Object value)
167169

168170
private bool IsList(Object value)
169171
{
170-
return value is IList; /* && value.GetType().IsGenericType*/;
172+
return value is IList;
171173
}
172174

173175
// http://stackoverflow.com/questions/1130698/checking-if-an-object-is-a-number-in-c-sharp#answer-1130705
@@ -190,7 +192,7 @@ private bool IsDecimalLike(object value)
190192
{
191193
return value is Single || value is Double || value is Decimal;
192194
}
193-
194-
195+
195196
}
197+
196198
}

Liquid.NET/src/Utils/LiquidValueConverterAttributes.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

33
namespace Liquid.NET.Utils
44
{
5-
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
5+
[AttributeUsage(AttributeTargets.Property)]
66
public class LiquidIgnoreAttribute : Attribute
77
{
88
}
99

10-
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
10+
[AttributeUsage(AttributeTargets.Property)]
11+
public class LiquidIgnoreIfNullAttribute : Attribute
12+
{
13+
}
14+
15+
16+
[AttributeUsage(AttributeTargets.Property)]
1117
public class LiquidNameAttribute : Attribute
1218
{
1319
private readonly string _key;

0 commit comments

Comments
 (0)