1
0
mirror of synced 2024-11-15 01:33:51 +00:00
PettingZoo/PettingZoo.Core/Macros/PayloadMacroProcessor.cs

42 lines
1.2 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace PettingZoo.Core.Macros
{
public class PayloadMacroProcessor : IPayloadMacroProcessor
{
private readonly BasePayloadMacro[] macros;
public IEnumerable<IPayloadMacro> Macros => macros;
public PayloadMacroProcessor()
{
macros = new BasePayloadMacro[]
{
new NewGuidPayloadMacro(),
new JsonDateTimePayloadMacro()
};
}
// For now we only support simple one-keyboard macros, but this could be extended with parameters if required
private static readonly Regex MacroRegex = new("{{(.+?)}}", RegexOptions.Compiled);
public string Apply(string payload)
{
return MacroRegex.Replace(payload, match =>
{
var macroCommand = match.Groups[1].Value.Trim();
var macro = macros.FirstOrDefault(m => string.Equals(m.MacroCommand, macroCommand, StringComparison.CurrentCultureIgnoreCase));
return macro != null
? macro.GetValue()
: match.Groups[0].Value;
});
}
}
}