Skip to content

Commit

Permalink
Make Npgsql-specific JsonValueReaderWriters public for compiled model
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Dec 18, 2023
1 parent 718b939 commit 66cb0d4
Show file tree
Hide file tree
Showing 11 changed files with 319 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,38 @@ protected override string ProcessStoreType(RelationalTypeMappingParameters param
? $"numeric({parameters.Precision})"
: $"numeric({parameters.Precision},{parameters.Scale})";

private sealed class JsonBigIntegerReaderWriter : JsonValueReaderWriter<BigInteger>
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public sealed class JsonBigIntegerReaderWriter : JsonValueReaderWriter<BigInteger>
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static JsonBigIntegerReaderWriter Instance { get; } = new();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
// Other systems handling the JSON very likely won't support arbitrary-length numbers here, we encode as a string
public override BigInteger FromJsonTyped(ref Utf8JsonReaderManager manager, object? existingObject = null)
=> BigInteger.Parse(manager.CurrentReader.GetString()!);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override void ToJsonTyped(Utf8JsonWriter writer, BigInteger value)
=> writer.WriteStringValue(value.ToString());
}
Expand Down
26 changes: 25 additions & 1 deletion src/EFCore.PG/Storage/Internal/Mapping/NpgsqlCidrTypeMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,37 @@ public override Expression GenerateCodeLiteral(object value)
private static readonly ConstructorInfo NpgsqlCidrConstructor =
typeof(NpgsqlCidr).GetConstructor(new[] { typeof(IPAddress), typeof(byte) })!;

private sealed class JsonCidrReaderWriter : JsonValueReaderWriter<NpgsqlCidr>
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public sealed class JsonCidrReaderWriter : JsonValueReaderWriter<NpgsqlCidr>
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static JsonCidrReaderWriter Instance { get; } = new();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override NpgsqlCidr FromJsonTyped(ref Utf8JsonReaderManager manager, object? existingObject = null)
=> new(manager.CurrentReader.GetString()!);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override void ToJsonTyped(Utf8JsonWriter writer, NpgsqlCidr value)
=> writer.WriteStringValue(value.ToString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,28 @@ private static string Format(DateOnly date)
return date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
}

private sealed class NpgsqlJsonDateOnlyReaderWriter : JsonValueReaderWriter<DateOnly>
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public sealed class NpgsqlJsonDateOnlyReaderWriter : JsonValueReaderWriter<DateOnly>
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static NpgsqlJsonDateOnlyReaderWriter Instance { get; } = new();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override DateOnly FromJsonTyped(ref Utf8JsonReaderManager manager, object? existingObject = null)
{
var s = manager.CurrentReader.GetString()!;
Expand All @@ -109,6 +127,12 @@ public override DateOnly FromJsonTyped(ref Utf8JsonReaderManager manager, object
return DateOnly.Parse(s, CultureInfo.InvariantCulture);
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override void ToJsonTyped(Utf8JsonWriter writer, DateOnly value)
=> writer.WriteStringValue(Format(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,28 @@ private static string Format(DateTime date)
return date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
}

private sealed class NpgsqlJsonDateTimeReaderWriter : JsonValueReaderWriter<DateTime>
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public sealed class NpgsqlJsonDateTimeReaderWriter : JsonValueReaderWriter<DateTime>
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static NpgsqlJsonDateTimeReaderWriter Instance { get; } = new();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override DateTime FromJsonTyped(ref Utf8JsonReaderManager manager, object? existingObject = null)
{
var s = manager.CurrentReader.GetString()!;
Expand All @@ -109,6 +127,12 @@ public override DateTime FromJsonTyped(ref Utf8JsonReaderManager manager, object
return DateTime.Parse(s, CultureInfo.InvariantCulture);
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override void ToJsonTyped(Utf8JsonWriter writer, DateTime value)
=> writer.WriteStringValue(Format(value));
}
Expand Down
52 changes: 50 additions & 2 deletions src/EFCore.PG/Storage/Internal/Mapping/NpgsqlInetTypeMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,72 @@ public override Expression GenerateCodeLiteral(object value)
private static readonly MethodInfo IPAddressParseMethod = typeof(IPAddress).GetMethod("Parse", new[] { typeof(string) })!;
private static readonly ConstructorInfo NpgsqlInetConstructor = typeof(NpgsqlInet).GetConstructor(new[] { typeof(string) })!;

private sealed class JsonIPAddressReaderWriter : JsonValueReaderWriter<IPAddress>
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public sealed class JsonIPAddressReaderWriter : JsonValueReaderWriter<IPAddress>
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static JsonIPAddressReaderWriter Instance { get; } = new();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override IPAddress FromJsonTyped(ref Utf8JsonReaderManager manager, object? existingObject = null)
=> IPAddress.Parse(manager.CurrentReader.GetString()!);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override void ToJsonTyped(Utf8JsonWriter writer, IPAddress value)
=> writer.WriteStringValue(value.ToString());
}

private sealed class JsonNpgsqlInetReaderWriter : JsonValueReaderWriter<NpgsqlInet>
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public sealed class JsonNpgsqlInetReaderWriter : JsonValueReaderWriter<NpgsqlInet>
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static JsonNpgsqlInetReaderWriter Instance { get; } = new();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override NpgsqlInet FromJsonTyped(ref Utf8JsonReaderManager manager, object? existingObject = null)
=> new(manager.CurrentReader.GetString()!);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override void ToJsonTyped(Utf8JsonWriter writer, NpgsqlInet value)
=> writer.WriteStringValue(value.ToString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,37 @@ public static TimeSpan ParseIntervalAsTimeSpan(ReadOnlySpan<char> s)
return timeSpan;
}

private sealed class NpgsqlJsonTimeSpanReaderWriter : JsonValueReaderWriter<TimeSpan>
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public sealed class NpgsqlJsonTimeSpanReaderWriter : JsonValueReaderWriter<TimeSpan>
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static NpgsqlJsonTimeSpanReaderWriter Instance { get; } = new();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override TimeSpan FromJsonTyped(ref Utf8JsonReaderManager manager, object? existingObject = null)
=> ParseIntervalAsTimeSpan(manager.CurrentReader.GetString()!);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override void ToJsonTyped(Utf8JsonWriter writer, TimeSpan value)
=> writer.WriteStringValue(FormatTimeSpanAsInterval(value));
}
Expand Down
Loading

0 comments on commit 66cb0d4

Please sign in to comment.