Skip to content

Commit

Permalink
Generate BigInteger literals in CSharpHelper
Browse files Browse the repository at this point in the history
Needed for code literal generation of NodaTime types
  • Loading branch information
roji committed Nov 7, 2018
1 parent 1032daf commit 0372f6e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/EFCore.Design/Design/Internal/CSharpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Numerics;
using System.Text;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Internal;
Expand Down Expand Up @@ -161,7 +162,8 @@ public CSharpHelper([NotNull] IRelationalTypeMappingSource relationalTypeMapping
{ typeof(TimeSpan), (c, v) => c.Literal((TimeSpan)v) },
{ typeof(uint), (c, v) => c.Literal((uint)v) },
{ typeof(ulong), (c, v) => c.Literal((ulong)v) },
{ typeof(ushort), (c, v) => c.Literal((ushort)v) }
{ typeof(ushort), (c, v) => c.Literal((ushort)v) },
{ typeof(BigInteger), (c, v) => c.Literal((BigInteger)v) }
};

/// <summary>
Expand Down Expand Up @@ -497,6 +499,12 @@ public virtual string Literal(TimeSpan value)
/// </summary>
public virtual string Literal(ushort value) => "(ushort)" + value;

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual string Literal(BigInteger value) => $"BigInteger.Parse(\"{value.ToString()}\")";

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
Expand Down
7 changes: 7 additions & 0 deletions test/EFCore.Design.Tests/Design/Internal/CSharpHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Data.SqlTypes;
using System.Linq.Expressions;
using System.Numerics;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal;
using Microsoft.EntityFrameworkCore.Storage;
Expand Down Expand Up @@ -213,6 +214,12 @@ public void Literal_works_when_MultidimensionalArray()
result);
}

[Fact]
public void Literal_works_when_BigInteger() =>
Literal_works(
new BigInteger(42),
"BigInteger.Parse(\"42\")");

[Fact]
public void UnknownLiteral_throws_when_unknown()
{
Expand Down

0 comments on commit 0372f6e

Please sign in to comment.