Skip to content

Commit

Permalink
ShortConnectorIdNotificationEvent() can now parse the long connectorI…
Browse files Browse the repository at this point in the history
…d into its constituent parts with each action data key/value pair broken out into an ActionData dict. Uses lazy loading to avoid unnecessary overhead.
  • Loading branch information
mpaperno committed Apr 18, 2022
1 parent 8a918b5 commit 2238db9
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
53 changes: 53 additions & 0 deletions TouchPortalSDK/Configuration/ConnectorIdParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using TouchPortalSDK.Messages.Models;

namespace TouchPortalSDK.Configuration
{
/// <summary>
/// Parses a "long" connectorId string from a ConnectorShortIdNotification message into a
/// dict of key value pairs.
/// </summary>
/// Example connectorId:
/// pc_pluginID_connectorId|actionDataId1=dataValue1|actionDataId2=dataValue2
/// where the "pc_pluginID_" part is prepended by TP to the actual connectorId.
/// This helper will first split split the string on the pipe delimeter,
/// and then further into key/value pairs split on the equals sign.
/// The result of the parsing (if any) is available from the Data property.
/// The first member of the initial split on the pipe char is saved in the
/// ConnectorIdPart property. If a pluginId is passed to the class c'tor, then
/// the leading "pc_pluginId_" part will be stripped out of the ConnectorIdPart result.
class ConnectorIdParser
{
public string ConnectorIdPart { get; }
public ActionData Data => _kvPairs;

private readonly ActionData _kvPairs = new ActionData();

/// <param name="connectorId"></param>
/// <param name="pluginId"></param>
public ConnectorIdParser(string connectorId, string pluginId = null)
{
var values = connectorId?.Split('|', StringSplitOptions.RemoveEmptyEntries);
// get our actual connector id
ConnectorIdPart = values.FirstOrDefault();
if (string.IsNullOrWhiteSpace(ConnectorIdPart))
return;

if (!string.IsNullOrWhiteSpace(pluginId))
ConnectorIdPart.Replace("pc_" + pluginId + "_", "");

for (int i = 1, e = values.Length; i < e; ++i) {
var keyVal = values[i].Split('=', StringSplitOptions.RemoveEmptyEntries);
var len = keyVal.Length;
if (len == 2)
_kvPairs.Add(keyVal[0], keyVal[1]);
else if (len == 1)
_kvPairs.Add(keyVal[0], "");
else if (len > 2)
_kvPairs.Add(keyVal[0], string.Join('=', keyVal[1..^0]));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using TouchPortalSDK.Interfaces;
using TouchPortalSDK.Messages.Models;
using TouchPortalSDK.Configuration;

namespace TouchPortalSDK.Messages.Events
{
Expand All @@ -11,6 +12,47 @@ public class ShortConnectorIdNotificationEvent : ITouchPortalMessage
public string PluginId { get; set; }
public string ConnectorId { get; set; }
public string ShortId { get; set; }
/// <summary>
/// Data is name = value pairs dictionary of the connector data members extracted
/// from the connectorId string.
/// It is populated only when the Data or ActualConnectorId members are first requested (lazy loading),
/// or can be pre-populated by calling the ParseData() method.
/// </summary>
public ActionData Data {
get {
if (_data == null)
ParseData();
return _data;
}
}
/// <summary>
/// Contains the actual connector ID, which is the first part of the sent
/// connectorId field, before the first "|" delimiter, and also with the
/// "pc_pluginId_" prefix stripped out.
/// It is populated only when the ActualConnectorId or Data are first requested (lazy loading),
/// or can be pre-populated by calling the ParseData() method.
/// </summary>
public string ActualConnectorId {
get {
if (_data == null)
ParseData();
return _parsedConnectorId;
}
}

private ActionData _data = null;
private string _parsedConnectorId = null;

/// <summary>
/// Parse the long connectorId string into key/value pairs of data fields it represents.
/// Populates the Data and ActualConnectorId properties.
/// </summary>
public void ParseData()
{
var p = new ConnectorIdParser(ConnectorId, PluginId);
_data = p.Data;
_parsedConnectorId = p.ConnectorIdPart;
}

public Identifier GetIdentifier()
=> new Identifier(Type, ConnectorId, default);
Expand Down

0 comments on commit 2238db9

Please sign in to comment.