| | | 1 | | using System.Text.Json; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | using Elsa.Secrets.Models; |
| | | 4 | | using Microsoft.EntityFrameworkCore; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Secrets.Persistence.EFCore; |
| | | 7 | | |
| | | 8 | | internal static class SecretSerialization |
| | | 9 | | { |
| | 0 | 10 | | private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web) |
| | 0 | 11 | | { |
| | 0 | 12 | | Converters = { new JsonStringEnumConverter() } |
| | 0 | 13 | | }; |
| | | 14 | | |
| | | 15 | | public static void StoreSerializedProperties(DbContext dbContext, Secret secret) |
| | | 16 | | { |
| | 0 | 17 | | dbContext.Entry(secret).Property(SecretShadowPropertyNames.SerializedTags).CurrentValue = JsonSerializer.Seriali |
| | 0 | 18 | | dbContext.Entry(secret).Property(SecretShadowPropertyNames.SerializedVersions).CurrentValue = JsonSerializer.Ser |
| | 0 | 19 | | } |
| | | 20 | | |
| | | 21 | | public static void LoadSerializedProperties(DbContext dbContext, Secret? secret) |
| | | 22 | | { |
| | 0 | 23 | | if (secret == null) |
| | 0 | 24 | | return; |
| | | 25 | | |
| | 0 | 26 | | var tagsJson = dbContext.Entry(secret).Property<string>(SecretShadowPropertyNames.SerializedTags).CurrentValue; |
| | 0 | 27 | | var versionsJson = dbContext.Entry(secret).Property<string>(SecretShadowPropertyNames.SerializedVersions).Curren |
| | 0 | 28 | | var tags = Deserialize(tagsJson, static () => new List<string>()); |
| | 0 | 29 | | var versions = Deserialize(versionsJson, static () => new List<SecretVersion>()); |
| | 0 | 30 | | secret.Tags = tags.ToHashSet(StringComparer.OrdinalIgnoreCase); |
| | 0 | 31 | | secret.Versions = versions; |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | private static T Deserialize<T>(string? json, Func<T> fallback) |
| | | 35 | | { |
| | 0 | 36 | | if (string.IsNullOrWhiteSpace(json)) |
| | 0 | 37 | | return fallback(); |
| | | 38 | | |
| | 0 | 39 | | return JsonSerializer.Deserialize<T>(json, JsonOptions) ?? fallback(); |
| | | 40 | | } |
| | | 41 | | } |