Implement surrogate for type using JObject

This commit is contained in:
Sipke Schoorstra 2020-11-14 20:49:32 +01:00
parent 0038702dd8
commit f652f1717a
7 changed files with 73 additions and 64 deletions

View file

@ -21,7 +21,6 @@
<ItemGroup>
<PackageReference Include="NodaTime" Version="3.0.3" />
<PackageReference Include="NodaTime.Serialization.JsonNet" Version="3.0.0" />
<PackageReference Include="protobuf-net.Core" Version="3.0.52" />
<PackageReference Include="Refit" Version="5.2.1" />
<PackageReference Include="Refit.HttpClientFactory" Version="5.2.1" />
</ItemGroup>

View file

@ -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];
}
}

View file

@ -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; }
}
}

View file

@ -2,6 +2,7 @@
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>

View file

@ -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<JObject>(surrogate.Options) : new JObject()
}
: default;
public static implicit operator ActivityPropertyDescriptorSurrogate?(ActivityPropertyDescriptor? source) => source != null ? new ActivityPropertyDescriptorSurrogate(source) : default;
}
}

View file

@ -1,45 +0,0 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NodaTime;
using NodaTime.Serialization.JsonNet;
using ProtoBuf;
namespace ElsaDashboard.Shared.Surrogates
{
/// <summary>
/// Surrogate for <see cref="JToken"/>.
/// </summary>
[ProtoContract(IgnoreListHandling = true)]
public class JTokenSurrogate
{
private static readonly JsonSerializerSettings SerializerSettings;
/// <summary>
/// Default constructor.
/// </summary>
static JTokenSurrogate()
{
SerializerSettings = new JsonSerializerSettings().ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
}
/// <summary>
/// Constructor overload.
/// </summary>
public JTokenSurrogate(JToken value) => Value = JsonConvert.SerializeObject(value, SerializerSettings);
/// <summary>
/// Stores the Google Protobuf Any value for serialization.
/// </summary>
public string Value { get; set; } = default!;
/// <summary>
/// Converts the surrogate to a <see cref="JToken"/>.
/// </summary>
public static implicit operator JToken(JTokenSurrogate surrogate) => JsonConvert.DeserializeObject<JToken>(surrogate.Value, SerializerSettings);
/// <summary>
/// Converts the <see cref="JToken"/> to a surrogate.
/// </summary>
public static implicit operator JTokenSurrogate(JToken source) => new(source);
}
}

View file

@ -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<Type, Type> SurrogateMapping = new Dictionary<Type, Type>
{
[typeof(JToken)] = typeof(JTokenSurrogate)
[typeof(ActivityPropertyDescriptor)] = typeof(ActivityPropertyDescriptorSurrogate)
};
/// <summary>