From f652f1717a9dd92c5fb9ac85436898f8ef40f0eb Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Sat, 14 Nov 2020 20:49:32 +0100 Subject: [PATCH] Implement surrogate for type using JObject --- src/clients/Elsa.Client/Elsa.Client.csproj | 1 - .../Elsa.Client/Models/ActivityDescriptor.cs | 20 +++---- .../Models/ActivityPropertyDescriptor.cs | 14 ++--- .../ElsaDashboard.Shared.csproj | 1 + .../ActivityPropertyDescriptorSurrogate.cs | 53 +++++++++++++++++++ .../Surrogates/JTokenSurrogate.cs | 45 ---------------- .../Surrogates/RuntimeTypeModelExtensions.cs | 3 +- 7 files changed, 73 insertions(+), 64 deletions(-) create mode 100644 src/dashboards/blazor/ElsaDashboard.Shared/Surrogates/ActivityPropertyDescriptorSurrogate.cs delete mode 100644 src/dashboards/blazor/ElsaDashboard.Shared/Surrogates/JTokenSurrogate.cs diff --git a/src/clients/Elsa.Client/Elsa.Client.csproj b/src/clients/Elsa.Client/Elsa.Client.csproj index a4cf7d723..d7baa46e7 100644 --- a/src/clients/Elsa.Client/Elsa.Client.csproj +++ b/src/clients/Elsa.Client/Elsa.Client.csproj @@ -21,7 +21,6 @@ - diff --git a/src/clients/Elsa.Client/Models/ActivityDescriptor.cs b/src/clients/Elsa.Client/Models/ActivityDescriptor.cs index b2af34af7..d294cff16 100644 --- a/src/clients/Elsa.Client/Models/ActivityDescriptor.cs +++ b/src/clients/Elsa.Client/Models/ActivityDescriptor.cs @@ -1,17 +1,17 @@ -using ProtoBuf; +using System.Runtime.Serialization; namespace Elsa.Client.Models { - [ProtoContract] + [DataContract] public class ActivityDescriptor { - [ProtoMember(1)] public string Type { get; set; } = default!; - [ProtoMember(2)] public string DisplayName { get; set; } = default!; - [ProtoMember(3)] public string? Description { get; set; } - [ProtoMember(4)] public string? RuntimeDescription { get; set; } - [ProtoMember(5)] public string Category { get; set; } = default!; - [ProtoMember(6)] public string? Icon { get; set; } - [ProtoMember(7)] public string[] Outcomes { get; set; } = new string[0]; - [ProtoMember(8)] public ActivityPropertyDescriptor[] Properties { get; set; } = new ActivityPropertyDescriptor[0]; + [DataMember(Order = 1)] public string Type { get; set; } = default!; + [DataMember(Order = 2)] public string DisplayName { get; set; } = default!; + [DataMember(Order = 3)] public string? Description { get; set; } + [DataMember(Order = 4)] public string? RuntimeDescription { get; set; } + [DataMember(Order = 5)] public string Category { get; set; } = default!; + [DataMember(Order = 6)] public string? Icon { get; set; } + [DataMember(Order = 7)] public string[] Outcomes { get; set; } = new string[0]; + [DataMember(Order = 8)] public ActivityPropertyDescriptor[] Properties { get; set; } = new ActivityPropertyDescriptor[0]; } } \ No newline at end of file diff --git a/src/clients/Elsa.Client/Models/ActivityPropertyDescriptor.cs b/src/clients/Elsa.Client/Models/ActivityPropertyDescriptor.cs index f85acf14c..8a447892d 100644 --- a/src/clients/Elsa.Client/Models/ActivityPropertyDescriptor.cs +++ b/src/clients/Elsa.Client/Models/ActivityPropertyDescriptor.cs @@ -1,15 +1,15 @@ +using System.Runtime.Serialization; using Newtonsoft.Json.Linq; -using ProtoBuf; namespace Elsa.Client.Models { - [ProtoContract(IgnoreListHandling = true)] + [DataContract] public class ActivityPropertyDescriptor { - [ProtoMember(1)] public string Name { get; } = default!; - [ProtoMember(2)] public string Type { get; } = default!; - [ProtoMember(3)] public string? Label { get; set; } - [ProtoMember(4)] public string? Hint { get; set; } - [ProtoMember(5)] public JObject? Options { get; set; } + [DataMember(Order = 1)] public string Name { get; set; } = default!; + [DataMember(Order = 2)] public string Type { get; set; } = default!; + [DataMember(Order = 3)] public string? Label { get; set; } + [DataMember(Order = 4)] public string? Hint { get; set; } + [DataMember(Order = 5)] public JObject? Options { get; set; } } } \ No newline at end of file diff --git a/src/dashboards/blazor/ElsaDashboard.Shared/ElsaDashboard.Shared.csproj b/src/dashboards/blazor/ElsaDashboard.Shared/ElsaDashboard.Shared.csproj index fc78967e5..865758397 100644 --- a/src/dashboards/blazor/ElsaDashboard.Shared/ElsaDashboard.Shared.csproj +++ b/src/dashboards/blazor/ElsaDashboard.Shared/ElsaDashboard.Shared.csproj @@ -2,6 +2,7 @@ net5.0 + enable diff --git a/src/dashboards/blazor/ElsaDashboard.Shared/Surrogates/ActivityPropertyDescriptorSurrogate.cs b/src/dashboards/blazor/ElsaDashboard.Shared/Surrogates/ActivityPropertyDescriptorSurrogate.cs new file mode 100644 index 000000000..7a1fad0d5 --- /dev/null +++ b/src/dashboards/blazor/ElsaDashboard.Shared/Surrogates/ActivityPropertyDescriptorSurrogate.cs @@ -0,0 +1,53 @@ +using Elsa.Client.Models; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using NodaTime; +using NodaTime.Serialization.JsonNet; +using ProtoBuf; + +namespace ElsaDashboard.Shared.Surrogates +{ + [ProtoContract(IgnoreListHandling = true)] + public class ActivityPropertyDescriptorSurrogate + { + private static readonly JsonSerializerSettings SerializerSettings; + + static ActivityPropertyDescriptorSurrogate() + { + SerializerSettings = new JsonSerializerSettings().ConfigureForNodaTime(DateTimeZoneProviders.Tzdb); + } + + public ActivityPropertyDescriptorSurrogate() + { + } + + public ActivityPropertyDescriptorSurrogate(ActivityPropertyDescriptor value) + { + Name = value.Name; + Type = value.Type; + Label = value.Label; + Hint = value.Hint; + Options = value.Options != null ? JsonConvert.SerializeObject(value.Options, SerializerSettings) : default; + } + + [ProtoMember(1)] public string? Name { get; } + [ProtoMember(2)] public string? Type { get; } + [ProtoMember(3)] public string? Label { get; } + [ProtoMember(4)] public string? Hint { get; } + [ProtoMember(5)] public string? Options { get; } + + public static implicit operator ActivityPropertyDescriptor?(ActivityPropertyDescriptorSurrogate? surrogate) => + surrogate != null + ? new ActivityPropertyDescriptor + { + Name = surrogate.Name!, + Type = surrogate.Type!, + Hint = surrogate.Hint, + Label = surrogate.Label, + Options = !string.IsNullOrEmpty(surrogate.Options) ? JsonConvert.DeserializeObject(surrogate.Options) : new JObject() + } + : default; + + public static implicit operator ActivityPropertyDescriptorSurrogate?(ActivityPropertyDescriptor? source) => source != null ? new ActivityPropertyDescriptorSurrogate(source) : default; + } +} \ No newline at end of file diff --git a/src/dashboards/blazor/ElsaDashboard.Shared/Surrogates/JTokenSurrogate.cs b/src/dashboards/blazor/ElsaDashboard.Shared/Surrogates/JTokenSurrogate.cs deleted file mode 100644 index 003ffa866..000000000 --- a/src/dashboards/blazor/ElsaDashboard.Shared/Surrogates/JTokenSurrogate.cs +++ /dev/null @@ -1,45 +0,0 @@ -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; -using NodaTime; -using NodaTime.Serialization.JsonNet; -using ProtoBuf; - -namespace ElsaDashboard.Shared.Surrogates -{ - /// - /// Surrogate for . - /// - [ProtoContract(IgnoreListHandling = true)] - public class JTokenSurrogate - { - private static readonly JsonSerializerSettings SerializerSettings; - - /// - /// Default constructor. - /// - static JTokenSurrogate() - { - SerializerSettings = new JsonSerializerSettings().ConfigureForNodaTime(DateTimeZoneProviders.Tzdb); - } - - /// - /// Constructor overload. - /// - public JTokenSurrogate(JToken value) => Value = JsonConvert.SerializeObject(value, SerializerSettings); - - /// - /// Stores the Google Protobuf Any value for serialization. - /// - public string Value { get; set; } = default!; - - /// - /// Converts the surrogate to a . - /// - public static implicit operator JToken(JTokenSurrogate surrogate) => JsonConvert.DeserializeObject(surrogate.Value, SerializerSettings); - - /// - /// Converts the to a surrogate. - /// - public static implicit operator JTokenSurrogate(JToken source) => new(source); - } -} \ No newline at end of file diff --git a/src/dashboards/blazor/ElsaDashboard.Shared/Surrogates/RuntimeTypeModelExtensions.cs b/src/dashboards/blazor/ElsaDashboard.Shared/Surrogates/RuntimeTypeModelExtensions.cs index b3ebf5afc..ecfda61d6 100644 --- a/src/dashboards/blazor/ElsaDashboard.Shared/Surrogates/RuntimeTypeModelExtensions.cs +++ b/src/dashboards/blazor/ElsaDashboard.Shared/Surrogates/RuntimeTypeModelExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Elsa.Client.Models; using Newtonsoft.Json.Linq; using ProtoBuf.Meta; @@ -12,7 +13,7 @@ namespace ElsaDashboard.Shared.Surrogates { private static readonly IDictionary SurrogateMapping = new Dictionary { - [typeof(JToken)] = typeof(JTokenSurrogate) + [typeof(ActivityPropertyDescriptor)] = typeof(ActivityPropertyDescriptorSurrogate) }; ///