From 2850d95cba890b63a91a7a547ecc937c8aae14d3 Mon Sep 17 00:00:00 2001 From: confusingboat Date: Fri, 2 Jun 2023 14:38:53 -0500 Subject: [PATCH] make more things internal and switch some stuff to use IEphemeralMetadata over the concrete --- .../InternalExtensions.cs | 3 +++ src/Ephemerally.Azure.Cosmos/PublicExtensions.cs | 8 ++++---- src/Ephemerally/InternalExtensions.cs | 13 +++++++++++++ src/Ephemerally/PublicExtensions.cs | 16 +++++----------- .../Ephemerally.Tests/EphemeralMetadataTests.cs | 2 +- 5 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 src/Ephemerally/InternalExtensions.cs diff --git a/src/Ephemerally.Azure.Cosmos/InternalExtensions.cs b/src/Ephemerally.Azure.Cosmos/InternalExtensions.cs index ad9a595..50d0e92 100644 --- a/src/Ephemerally.Azure.Cosmos/InternalExtensions.cs +++ b/src/Ephemerally.Azure.Cosmos/InternalExtensions.cs @@ -5,6 +5,9 @@ namespace Ephemerally.Azure.Cosmos; internal static class InternalExtensions { + internal static T OrDefault(this T options) where T : EphemeralOptions, new() => + options ?? new T(); + internal static async Task ExistsAsync(this Database database) => await database.Client.DatabaseExistsAsync(database.Id).ConfigureAwait(false); diff --git a/src/Ephemerally.Azure.Cosmos/PublicExtensions.cs b/src/Ephemerally.Azure.Cosmos/PublicExtensions.cs index b4cc879..b1ce80b 100644 --- a/src/Ephemerally.Azure.Cosmos/PublicExtensions.cs +++ b/src/Ephemerally.Azure.Cosmos/PublicExtensions.cs @@ -33,15 +33,15 @@ public static async Task CreateEphemeralContainerAsync return database.GetContainer(response.Resource.Id).ToEphemeral(options); } - public static EphemeralMetadata GetEphemeralMetadata(this DatabaseProperties container) => + public static IEphemeralMetadata GetEphemeralMetadata(this DatabaseProperties container) => container.Id.GetContainerMetadata(); - public static EphemeralMetadata GetEphemeralMetadata(this Database container) => + public static IEphemeralMetadata GetEphemeralMetadata(this Database container) => container.Id.GetContainerMetadata(); - public static EphemeralMetadata GetEphemeralMetadata(this ContainerProperties container) => + public static IEphemeralMetadata GetEphemeralMetadata(this ContainerProperties container) => container.Id.GetContainerMetadata(); - public static EphemeralMetadata GetEphemeralMetadata(this Container container) => + public static IEphemeralMetadata GetEphemeralMetadata(this Container container) => container.Id.GetContainerMetadata(); } \ No newline at end of file diff --git a/src/Ephemerally/InternalExtensions.cs b/src/Ephemerally/InternalExtensions.cs new file mode 100644 index 0000000..33caa58 --- /dev/null +++ b/src/Ephemerally/InternalExtensions.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ephemerally; + +internal static class InternalExtensions +{ + internal static T OrDefault(this T options) where T : EphemeralOptions, new() => + options ?? new T(); +} \ No newline at end of file diff --git a/src/Ephemerally/PublicExtensions.cs b/src/Ephemerally/PublicExtensions.cs index 67fd58d..5633d52 100644 --- a/src/Ephemerally/PublicExtensions.cs +++ b/src/Ephemerally/PublicExtensions.cs @@ -2,21 +2,15 @@ public static class PublicExtensions { - //public static EphemeralCreationOptions OrDefault(this EphemeralCreationOptions options) => - // options ?? EphemeralCreationOptions.Default; - - public static T OrDefault(this T options) where T : EphemeralOptions, new() => - options ?? new T(); - - public static EphemeralMetadata GetNewMetadata(this EphemeralCreationOptions options) => + public static IEphemeralMetadata GetNewMetadata(this EphemeralCreationOptions options) => EphemeralMetadata.New(options.Name, options.GetExpiration(DateTimeOffset.UtcNow)); - public static bool IsExpired(this EphemeralMetadata metadata) => - IsExpired(metadata, DateTimeOffset.UtcNow); + public static bool IsExpired(this IEphemeralMetadata metadata) => + IsExpiredAsOf(metadata, DateTimeOffset.UtcNow); - internal static bool IsExpired(this EphemeralMetadata metadata, DateTimeOffset now) => + public static bool IsExpiredAsOf(this IEphemeralMetadata metadata, DateTimeOffset now) => metadata.Expiration.HasValue && metadata.Expiration.Value <= now; - public static EphemeralMetadata GetContainerMetadata(this string fullName) => + public static IEphemeralMetadata GetContainerMetadata(this string fullName) => EphemeralMetadata.New(fullName); } \ No newline at end of file diff --git a/tests/Ephemerally.Tests/EphemeralMetadataTests.cs b/tests/Ephemerally.Tests/EphemeralMetadataTests.cs index 4a82b24..c3b7903 100644 --- a/tests/Ephemerally.Tests/EphemeralMetadataTests.cs +++ b/tests/Ephemerally.Tests/EphemeralMetadataTests.cs @@ -18,6 +18,6 @@ public bool IsExpired_as_of_1684991963(long now) Nonce = "ABCDEF" }; - return metadata.IsExpired(DateTimeOffset.FromUnixTimeSeconds(now)); + return metadata.IsExpiredAsOf(DateTimeOffset.FromUnixTimeSeconds(now)); } } \ No newline at end of file