Skip to content

Commit

Permalink
Convert the action/connector Data type into a dictionary of id=value …
Browse files Browse the repository at this point in the history
…pairs.

**This will break any plugins using the Data member directly (vs. GetValue()).** See change in sample plugin.
  • Loading branch information
mpaperno committed Apr 18, 2022
1 parent c4691d6 commit 8a918b5
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 23 deletions.
4 changes: 2 additions & 2 deletions TouchPortalSDK.Sample/SamplePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
27 changes: 27 additions & 0 deletions TouchPortalSDK/Configuration/ActionDataConverter.cs
Original file line number Diff line number Diff line change
@@ -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<ActionData>
{
public override ActionData Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var ret = new ActionData();
var actionDataDicts = JsonSerializer.Deserialize<ActionData[]>(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();
}
}
}
3 changes: 2 additions & 1 deletion TouchPortalSDK/Configuration/SerializationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ internal static class Options
Converters =
{
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase),
new SettingsConverter()
new SettingsConverter(),
new ActionDataConverter()
}
};
}
Expand Down
10 changes: 5 additions & 5 deletions TouchPortalSDK/Messages/Events/DataContainerEventBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,29 @@ public abstract class DataContainerEventBase : ITouchPortalMessage
public string Id { get; set; }

/// <summary>
/// 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
/// </summary>
public IReadOnlyCollection<ActionDataSelected> Data { get; set; }
public ActionData Data { get; set; }

/// <summary>
/// Indexer to get data values.
/// </summary>
/// <param name="dataId">the id of the datafield.</param>
/// <returns>the value of the data field as string or null if not exists</returns>
public string this[string dataId]
=> GetValue(dataId);
public string this[string dataId] => GetValue(dataId);

/// <summary>
/// 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.
/// </summary>
/// <param name="dataId">the id of the datafield.</param>
/// <param name="defaultValue">Default value to return if the dataId wasn't found.</param>
/// <returns>the value of the data field as string or null if not exists</returns>
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; }
}
Expand Down
11 changes: 11 additions & 0 deletions TouchPortalSDK/Messages/Models/ActionData.cs
Original file line number Diff line number Diff line change
@@ -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<string, string>
{

}

}
15 changes: 0 additions & 15 deletions TouchPortalSDK/Messages/Models/ActionDataSelected.cs

This file was deleted.

0 comments on commit 8a918b5

Please sign in to comment.