| | | 1 | | using System; |
| | | 2 | | using System.Linq; |
| | | 3 | | using System.Linq.Dynamic.Core; |
| | | 4 | | using System.Linq.Dynamic.Core.Exceptions; |
| | | 5 | | using HandlebarsDotNet; |
| | | 6 | | using Newtonsoft.Json.Linq; |
| | | 7 | | using WireMock.Util; |
| | | 8 | | using WireMock.Validation; |
| | | 9 | | |
| | | 10 | | namespace WireMock.Transformers |
| | | 11 | | { |
| | | 12 | | internal static class HandleBarsLinq |
| | | 13 | | { |
| | | 14 | | public static void Register() |
| | 1 | 15 | | { |
| | 1 | 16 | | Handlebars.RegisterHelper("Linq", (writer, context, arguments) => |
| | 8 | 17 | | { |
| | 8 | 18 | | (JToken valueToProcess, string linqStatement) = ParseArguments(arguments); |
| | 1 | 19 | | |
| | 1 | 20 | | try |
| | 5 | 21 | | { |
| | 5 | 22 | | object result = ExecuteDynamicLinq(valueToProcess, linqStatement); |
| | 4 | 23 | | writer.WriteSafeString(result); |
| | 4 | 24 | | } |
| | 2 | 25 | | catch (ParseException) |
| | 2 | 26 | | { |
| | 1 | 27 | | // Ignore ParseException |
| | 2 | 28 | | } |
| | 5 | 29 | | }); |
| | | 30 | | |
| | 1 | 31 | | Handlebars.RegisterHelper("Linq", (writer, options, context, arguments) => |
| | 3 | 32 | | { |
| | 3 | 33 | | (JToken valueToProcess, string linqStatement) = ParseArguments(arguments); |
| | 1 | 34 | | |
| | 1 | 35 | | try |
| | 3 | 36 | | { |
| | 3 | 37 | | var result = ExecuteDynamicLinq(valueToProcess, linqStatement); |
| | 2 | 38 | | options.Template(writer, result); |
| | 2 | 39 | | } |
| | 2 | 40 | | catch (ParseException) |
| | 2 | 41 | | { |
| | 1 | 42 | | // Ignore ParseException |
| | 2 | 43 | | } |
| | 3 | 44 | | }); |
| | 1 | 45 | | } |
| | | 46 | | |
| | | 47 | | private static dynamic ExecuteDynamicLinq(JToken value, string linqStatement) |
| | 6 | 48 | | { |
| | | 49 | | // Convert a single object to a Queryable JObject-list with 1 entry. |
| | 6 | 50 | | var queryable1 = new[] { value }.AsQueryable(); |
| | | 51 | | |
| | | 52 | | // Generate the DynamicLinq select statement. |
| | 6 | 53 | | string dynamicSelect = JsonUtils.GenerateDynamicLinqStatement(value); |
| | | 54 | | |
| | | 55 | | // Execute DynamicLinq Select statement. |
| | 6 | 56 | | var queryable2 = queryable1.Select(dynamicSelect); |
| | | 57 | | |
| | | 58 | | // Execute the Select(...) method and get first result with FirstOrDefault(). |
| | 6 | 59 | | return queryable2.Select(linqStatement).FirstOrDefault(); |
| | 4 | 60 | | } |
| | | 61 | | |
| | | 62 | | private static (JToken valueToProcess, string linqStatement) ParseArguments(object[] arguments) |
| | 9 | 63 | | { |
| | 18 | 64 | | Check.Condition(arguments, args => args.Length == 2, nameof(arguments)); |
| | 8 | 65 | | Check.NotNull(arguments[0], "arguments[0]"); |
| | 7 | 66 | | Check.NotNullOrEmpty(arguments[1] as string, "arguments[1]"); |
| | | 67 | | |
| | | 68 | | JToken valueToProcess; |
| | 7 | 69 | | switch (arguments[0]) |
| | | 70 | | { |
| | | 71 | | case string jsonAsString: |
| | 1 | 72 | | valueToProcess = new JValue(jsonAsString); |
| | 1 | 73 | | break; |
| | | 74 | | |
| | | 75 | | case JToken jsonAsJObject: |
| | 5 | 76 | | valueToProcess = jsonAsJObject; |
| | 5 | 77 | | break; |
| | | 78 | | |
| | | 79 | | default: |
| | 1 | 80 | | throw new NotSupportedException($"The value '{arguments[0]}' with type '{arguments[0]?.GetType()}' c |
| | | 81 | | } |
| | | 82 | | |
| | 6 | 83 | | return (valueToProcess, (string) arguments[1]); |
| | 6 | 84 | | } |
| | | 85 | | } |
| | | 86 | | } |