Update handlebars code to support Regex.Match (#201) (#202)

* #201

* Handlebars : Linq and JsonPath

* Rename to Regex.Match

* unit test coverage
This commit is contained in:
Stef Heyenrath
2018-09-11 08:42:50 +02:00
committed by GitHub
parent ec39d12cfe
commit da64934c13
11 changed files with 964 additions and 579 deletions

View File

@@ -83,11 +83,13 @@ namespace WireMock.Matchers
// Convert a single object to a Queryable JObject-list with 1 entry.
var queryable1 = new[] { value }.AsQueryable();
// Generate the dynamic linq select statement and generate a dynamic Queryable.
// Generate the DynamicLinq select statement.
string dynamicSelect = JsonUtils.GenerateDynamicLinqStatement(value);
// Execute DynamicLinq Select statement.
var queryable2 = queryable1.Select(dynamicSelect);
// Use the Any(...) method to check if the result matches
// Use the Any(...) method to check if the result matches.
double match = MatchScores.ToScore(_patterns.Select(pattern => queryable2.Any(pattern)));
return MatchBehaviourHelper.Convert(MatchBehaviour, match);

View File

@@ -1,144 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text.RegularExpressions;
using HandlebarsDotNet;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WireMock.Utils;
using WireMock.Validation;
namespace WireMock.Transformers
namespace WireMock.Transformers
{
internal static class HandlebarsHelpers
{
public static void Register()
{
Handlebars.RegisterHelper("Regex.Match", (writer, context, arguments) =>
{
(string stringToProcess, string regexPattern, object defaultValue) = ParseRegexArguments(arguments);
HandleBarsRegex.Register();
Match match = Regex.Match(stringToProcess, regexPattern);
HandleBarsJsonPath.Register();
if (match.Success)
{
writer.WriteSafeString(match.Value);
}
else if (defaultValue != null)
{
writer.WriteSafeString(defaultValue);
}
});
Handlebars.RegisterHelper("Regex.Match", (writer, options, context, arguments) =>
{
(string stringToProcess, string regexPattern, object defaultValue) = ParseRegexArguments(arguments);
var regex = new Regex(regexPattern);
var namedGroups = RegexUtils.GetNamedGroups(regex, stringToProcess);
if (namedGroups.Any())
{
options.Template(writer, namedGroups);
}
else if (defaultValue != null)
{
writer.WriteSafeString(defaultValue);
}
});
Handlebars.RegisterHelper("JsonPath.SelectToken", (writer, context, arguments) =>
{
(JObject valueToProcess, string jsonpath) = ParseJsonPathArguments(arguments);
JToken result = null;
try
{
result = valueToProcess.SelectToken(jsonpath);
}
catch (JsonException)
{
// Ignore JsonException and return
return;
}
if (result != null)
{
writer.WriteSafeString(result);
}
});
Handlebars.RegisterHelper("JsonPath.SelectTokens", (writer, options, context, arguments) =>
{
(JObject valueToProcess, string jsonpath) = ParseJsonPathArguments(arguments);
IEnumerable<JToken> values = null;
try
{
values = valueToProcess.SelectTokens(jsonpath);
}
catch (JsonException)
{
// Ignore JsonException and return
return;
}
if (values == null)
{
return;
}
int id = 0;
foreach (JToken value in values)
{
options.Template(writer, new { id, value });
id++;
}
});
}
private static (JObject valueToProcess, string jsonpath) ParseJsonPathArguments(object[] arguments)
{
Check.Condition(arguments, args => args.Length == 2, nameof(arguments));
Check.NotNull(arguments[0], "arguments[0]");
Check.NotNullOrEmpty(arguments[1] as string, "arguments[1]");
JObject valueToProcess;
switch (arguments[0])
{
case string jsonAsString:
valueToProcess = JObject.Parse(jsonAsString);
break;
case JObject jsonAsJObject:
valueToProcess = jsonAsJObject;
break;
default:
throw new NotSupportedException($"The value '{arguments[0]}' with type '{arguments[0]?.GetType()}' cannot be used in Handlebars JsonPath.");
}
return (valueToProcess, arguments[1] as string);
}
private static (string stringToProcess, string regexPattern, object defaultValue) ParseRegexArguments(object[] arguments)
{
Check.Condition(arguments, args => args.Length >= 2, nameof(arguments));
string ParseAsString(object arg)
{
if (arg is string)
{
return arg as string;
}
else
{
throw new NotSupportedException($"The value '{arg}' with type '{arg?.GetType()}' cannot be used in Handlebars Regex.");
}
}
return (ParseAsString(arguments[0]), ParseAsString(arguments[1]), arguments.Length == 3 ? arguments[2] : null);
HandleBarsLinq.Register();
}
}
}

View File

@@ -0,0 +1,75 @@
using System;
using System.Linq;
using HandlebarsDotNet;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WireMock.Validation;
namespace WireMock.Transformers
{
internal static class HandleBarsJsonPath
{
public static void Register()
{
Handlebars.RegisterHelper("JsonPath.SelectToken", (writer, context, arguments) =>
{
(JObject valueToProcess, string jsonpath) = ParseArguments(arguments);
try
{
var result = valueToProcess.SelectToken(jsonpath);
writer.WriteSafeString(result);
}
catch (JsonException)
{
// Ignore JsonException and return
return;
}
});
Handlebars.RegisterHelper("JsonPath.SelectTokens", (writer, options, context, arguments) =>
{
(JObject valueToProcess, string jsonpath) = ParseArguments(arguments);
try
{
var values = valueToProcess.SelectTokens(jsonpath);
if (values != null)
{
options.Template(writer, values.ToDictionary(value => value.Path, value => value));
}
}
catch (JsonException)
{
// Ignore JsonException and return
return;
}
});
}
private static (JObject valueToProcess, string jsonpath) ParseArguments(object[] arguments)
{
Check.Condition(arguments, args => args.Length == 2, nameof(arguments));
Check.NotNull(arguments[0], "arguments[0]");
Check.NotNullOrEmpty(arguments[1] as string, "arguments[1]");
JObject valueToProcess;
switch (arguments[0])
{
case string jsonAsString:
valueToProcess = JObject.Parse(jsonAsString);
break;
case JObject jsonAsJObject:
valueToProcess = jsonAsJObject;
break;
default:
throw new NotSupportedException($"The value '{arguments[0]}' with type '{arguments[0]?.GetType()}' cannot be used in Handlebars JsonPath.");
}
return (valueToProcess, arguments[1] as string);
}
}
}

View File

@@ -0,0 +1,88 @@
using System;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Dynamic.Core.Exceptions;
using HandlebarsDotNet;
using Newtonsoft.Json.Linq;
using WireMock.Util;
using WireMock.Validation;
namespace WireMock.Transformers
{
internal static class HandleBarsLinq
{
public static void Register()
{
Handlebars.RegisterHelper("Linq", (writer, context, arguments) =>
{
(JToken valueToProcess, string linqStatement) = ParseArguments(arguments);
try
{
object result = ExecuteDynamicLinq(valueToProcess, linqStatement);
writer.WriteSafeString(result);
}
catch (ParseException)
{
// Ignore ParseException and return
return;
}
});
Handlebars.RegisterHelper("Linq", (writer, options, context, arguments) =>
{
(JToken valueToProcess, string linqStatement) = ParseArguments(arguments);
try
{
var result = ExecuteDynamicLinq(valueToProcess, linqStatement);
options.Template(writer, result);
}
catch (ParseException)
{
// Ignore ParseException and return
return;
}
});
}
private static dynamic ExecuteDynamicLinq(JToken value, string linqStatement)
{
// Convert a single object to a Queryable JObject-list with 1 entry.
var queryable1 = new[] { value }.AsQueryable();
// Generate the DynamicLinq select statement.
string dynamicSelect = JsonUtils.GenerateDynamicLinqStatement(value);
// Execute DynamicLinq Select statement.
var queryable2 = queryable1.Select(dynamicSelect);
// Execute the Select(...) method and get first result with FirstOrDefault().
return queryable2.Select(linqStatement).FirstOrDefault();
}
private static (JToken valueToProcess, string linqStatement) ParseArguments(object[] arguments)
{
Check.Condition(arguments, args => args.Length == 2, nameof(arguments));
Check.NotNull(arguments[0], "arguments[0]");
Check.NotNullOrEmpty(arguments[1] as string, "arguments[1]");
JToken valueToProcess;
switch (arguments[0])
{
case string jsonAsString:
valueToProcess = new JValue(jsonAsString);
break;
case JToken jsonAsJObject:
valueToProcess = jsonAsJObject;
break;
default:
throw new NotSupportedException($"The value '{arguments[0]}' with type '{arguments[0]?.GetType()}' cannot be used in Handlebars Linq.");
}
return (valueToProcess, arguments[1] as string);
}
}
}

View File

@@ -0,0 +1,66 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
using HandlebarsDotNet;
using WireMock.Utils;
using WireMock.Validation;
namespace WireMock.Transformers
{
internal static class HandleBarsRegex
{
public static void Register()
{
Handlebars.RegisterHelper("Regex.Match", (writer, context, arguments) =>
{
(string stringToProcess, string regexPattern, object defaultValue) = ParseArguments(arguments);
Match match = Regex.Match(stringToProcess, regexPattern);
if (match.Success)
{
writer.WriteSafeString(match.Value);
}
else if (defaultValue != null)
{
writer.WriteSafeString(defaultValue);
}
});
Handlebars.RegisterHelper("Regex.Match", (writer, options, context, arguments) =>
{
(string stringToProcess, string regexPattern, object defaultValue) = ParseArguments(arguments);
var regex = new Regex(regexPattern);
var namedGroups = RegexUtils.GetNamedGroups(regex, stringToProcess);
if (namedGroups.Any())
{
options.Template(writer, namedGroups);
}
else if (defaultValue != null)
{
options.Template(writer, defaultValue);
}
});
}
private static (string stringToProcess, string regexPattern, object defaultValue) ParseArguments(object[] arguments)
{
Check.Condition(arguments, args => args.Length == 2 || args.Length == 3, nameof(arguments));
string ParseAsString(object arg)
{
if (arg is string)
{
return arg as string;
}
else
{
throw new NotSupportedException($"The value '{arg}' with type '{arg?.GetType()}' cannot be used in Handlebars Regex.");
}
}
return (ParseAsString(arguments[0]), ParseAsString(arguments[1]), arguments.Length == 3 ? arguments[2] : null);
}
}
}

View File

@@ -20,7 +20,7 @@ namespace WireMock.Util
}
}
public static string GenerateDynamicLinqStatement(JObject jsonObject)
public static string GenerateDynamicLinqStatement(JToken jsonObject)
{
var lines = new List<string>();
WalkNode(jsonObject, null, null, lines);
@@ -40,7 +40,7 @@ namespace WireMock.Util
}
else
{
ProcessItem(node, path, propertyName, lines);
ProcessItem(node, path ?? "it", propertyName, lines);
}
}