From 8a918b5ad1e82f01b459c233447465a9c6157de0 Mon Sep 17 00:00:00 2001 From: Max Paperno Date: Sun, 17 Apr 2022 20:05:12 -0400 Subject: [PATCH] Convert the action/connector Data type into a dictionary of id=value pairs. **This will break any plugins using the Data member directly (vs. GetValue()).** See change in sample plugin. --- TouchPortalSDK.Sample/SamplePlugin.cs | 4 +-- .../Configuration/ActionDataConverter.cs | 27 +++++++++++++++++++ .../Configuration/SerializationOptions.cs | 3 ++- .../Messages/Events/DataContainerEventBase.cs | 10 +++---- TouchPortalSDK/Messages/Models/ActionData.cs | 11 ++++++++ .../Messages/Models/ActionDataSelected.cs | 15 ----------- 6 files changed, 47 insertions(+), 23 deletions(-) create mode 100644 TouchPortalSDK/Configuration/ActionDataConverter.cs create mode 100644 TouchPortalSDK/Messages/Models/ActionData.cs delete mode 100644 TouchPortalSDK/Messages/Models/ActionDataSelected.cs diff --git a/TouchPortalSDK.Sample/SamplePlugin.cs b/TouchPortalSDK.Sample/SamplePlugin.cs index d790317..28b0b6f 100644 --- a/TouchPortalSDK.Sample/SamplePlugin.cs +++ b/TouchPortalSDK.Sample/SamplePlugin.cs @@ -169,7 +169,7 @@ public void OnActionEvent(ActionEvent message) default: var dataArray = message.Data - .Select(dataItem => $"\"{dataItem.Id}\":\"{dataItem.Value}\"") + .Select(dataItem => $"\"{dataItem.Key}\":\"{dataItem.Value}\"") .ToArray(); var dataString = string.Join(", ", dataArray); @@ -218,7 +218,7 @@ public void OnNotificationOptionClickedEvent(NotificationOptionClickedEvent mess public void OnConnecterChangeEvent(ConnectorChangeEvent message) { var dataArray = message.Data - .Select(dataItem => $"\"{dataItem.Id}\":\"{dataItem.Value}\"") + .Select(dataItem => $"\"{dataItem.Key}\":\"{dataItem.Value}\"") .ToArray(); var dataString = string.Join(", ", dataArray); diff --git a/TouchPortalSDK/Configuration/ActionDataConverter.cs b/TouchPortalSDK/Configuration/ActionDataConverter.cs new file mode 100644 index 0000000..f379d14 --- /dev/null +++ b/TouchPortalSDK/Configuration/ActionDataConverter.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Text.Json; +using System.Text.Json.Serialization; +using TouchPortalSDK.Messages.Models; + +namespace TouchPortalSDK.Configuration +{ + internal class ActionDataConverter : JsonConverter + { + public override ActionData Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var ret = new ActionData(); + var actionDataDicts = JsonSerializer.Deserialize(ref reader); + if (actionDataDicts != null){ + foreach (var dict in actionDataDicts) + ret.TryAdd(dict.GetValueOrDefault("id"), dict.GetValueOrDefault("value")); + } + return ret; + } + + public override void Write(Utf8JsonWriter writer, ActionData value, JsonSerializerOptions options) + { + throw new NotImplementedException(); + } + } +} diff --git a/TouchPortalSDK/Configuration/SerializationOptions.cs b/TouchPortalSDK/Configuration/SerializationOptions.cs index a83a2f4..a2acc59 100644 --- a/TouchPortalSDK/Configuration/SerializationOptions.cs +++ b/TouchPortalSDK/Configuration/SerializationOptions.cs @@ -12,7 +12,8 @@ internal static class Options Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase), - new SettingsConverter() + new SettingsConverter(), + new ActionDataConverter() } }; } diff --git a/TouchPortalSDK/Messages/Events/DataContainerEventBase.cs b/TouchPortalSDK/Messages/Events/DataContainerEventBase.cs index d6db68f..5f010e6 100644 --- a/TouchPortalSDK/Messages/Events/DataContainerEventBase.cs +++ b/TouchPortalSDK/Messages/Events/DataContainerEventBase.cs @@ -26,29 +26,29 @@ public abstract class DataContainerEventBase : ITouchPortalMessage public string Id { get; set; } /// - /// Data is name/value pairs of options the user has selected for this action. + /// Data is name = value pairs dictionary of options the user has selected for this action. /// Ex. data1: dropdown1 /// data2: dropdown2 /// - public IReadOnlyCollection Data { get; set; } + public ActionData Data { get; set; } /// /// Indexer to get data values. /// /// the id of the datafield. /// the value of the data field as string or null if not exists - public string this[string dataId] - => GetValue(dataId); + public string this[string dataId] => GetValue(dataId); /// /// Returns the value of the selected item in an action data field. /// This value can be null in some cases, and will be null if data field is miss written. /// /// the id of the datafield. + /// Default value to return if the dataId wasn't found. /// the value of the data field as string or null if not exists public string GetValue(string dataId, string defaultValue = null) { try { - return Data?.SingleOrDefault(data => data.Id == dataId)?.Value ?? defaultValue; + return Data?.GetValueOrDefault(dataId, defaultValue) ?? defaultValue; } catch { return defaultValue; } } diff --git a/TouchPortalSDK/Messages/Models/ActionData.cs b/TouchPortalSDK/Messages/Models/ActionData.cs new file mode 100644 index 0000000..0039e18 --- /dev/null +++ b/TouchPortalSDK/Messages/Models/ActionData.cs @@ -0,0 +1,11 @@ + +namespace TouchPortalSDK.Messages.Models +{ + // the only real reason this is a custom type is for the JSON parser so we can write a custom handler + // to break up the action data array into a dictionary. + public sealed class ActionData : System.Collections.Generic.Dictionary + { + + } + +} diff --git a/TouchPortalSDK/Messages/Models/ActionDataSelected.cs b/TouchPortalSDK/Messages/Models/ActionDataSelected.cs deleted file mode 100644 index 605d893..0000000 --- a/TouchPortalSDK/Messages/Models/ActionDataSelected.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace TouchPortalSDK.Messages.Models -{ - public class ActionDataSelected - { - /// - /// The id of the data field as string - /// - public string Id { get; set; } - - /// - /// The value of the data field as string - /// - public string Value { get; set; } - } -} \ No newline at end of file