Skip to content

Commit

Permalink
Merge d19603c into d8462ea
Browse files Browse the repository at this point in the history
  • Loading branch information
Romanx authored Aug 16, 2022
2 parents d8462ea + d19603c commit 3e62679
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/Stubble.Compilation/Contexts/CompilerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ public Expression GetTruthyExpression(Expression value)
var parameter = Expression.Parameter(value.Type, "checkThis");
var checks = new List<Tuple<Expression, ConstantExpression>>();

if (!parameter.Type.GetIsValueType())
if (parameter.Type.IsNullable())
{
checks.Add(Tuple.Create<Expression, ConstantExpression>(Expression.Equal(parameter, Expression.Constant(null)), FalseConstant));
checks.Add(Tuple.Create<Expression, ConstantExpression>(Expression.Equal(parameter, Expression.Default(parameter.Type)), FalseConstant));
}

if (CompilerSettings.TruthyChecks.TryGetValue(parameter.Type, out var typeTruthyChecks))
Expand Down
24 changes: 14 additions & 10 deletions src/Stubble.Compilation/Helpers/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,22 @@ public static Type GetElementTypeOfIEnumerable(this Type type)
}

/// <summary>
/// Returns if the type is a value type or not
/// Returns if the provided type is nullable or not.
/// </summary>
/// <param name="type">The type to evaluate</param>
/// <returns>If the type is a value type or not</returns>
[MethodImpl(MethodImplOptionPortable.AggressiveInlining)]
public static bool GetIsValueType(this Type type)
/// <param name="type">The type to evaluate.</param>
/// <returns>If the type is nullable or not.</returns>
public static bool IsNullable(this Type type)
{
#if NETSTANDARD1_3
return type.GetTypeInfo().IsValueType;
#else
return type.IsValueType;
#endif
if (type.IsValueType is false)
{
return true;
}
else if (Nullable.GetUnderlyingType(type) is not null)
{
return true;
}

return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ protected override void Write(CompilationRenderer renderer, InterpolationToken o
var formattedToString = expression.Type
.GetMethod(nameof(object.ToString), formatProviderTypeArgs);

var item = expression.Type.GetIsValueType()
? expression
: Expression.Coalesce(expression, Expression.Constant(string.Empty));
var item = expression.Type.IsNullable()
? Expression.Coalesce(expression, Expression.Constant(string.Empty))
: expression;

stringExpression = formattedToString is object
? Expression.Call(item, formattedToString, Expression.Constant(context.CompilationSettings.CultureInfo))
Expand Down
8 changes: 8 additions & 0 deletions test/Stubble.Test.Shared/Spec/Spec.Sections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,14 @@ public static partial class Specs
Template = @"|{{# boolean }}={{/ boolean }}|",
Expected = @"|=|",
Partials = null
},
new SpecTest {
Name = @"Null ints are not truthy",
Desc = @"Nullable items should be truthy checked",
Data = new { Count = (int?)null },
Template = @"""{{#Count}}Should not be visible{{/Count}}""",
Expected = @"""""",
Partials = null
},
}.Select(s => new object[] { s });
}
Expand Down
2 changes: 0 additions & 2 deletions test/Stubble.Test.Shared/Spec/SpecTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using Xunit.Abstractions;

namespace Stubble.Test.Shared.Spec
{
Expand Down

0 comments on commit 3e62679

Please sign in to comment.