| | | 1 | | using System.Linq; |
| | | 2 | | using System.Linq.Dynamic.Core; |
| | | 3 | | using JetBrains.Annotations; |
| | | 4 | | using Newtonsoft.Json.Linq; |
| | | 5 | | using WireMock.Util; |
| | | 6 | | |
| | | 7 | | namespace WireMock.Matchers |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// System.Linq.Dynamic.Core Expression Matcher |
| | | 11 | | /// </summary> |
| | | 12 | | /// <inheritdoc cref="IStringMatcher"/> |
| | | 13 | | public class LinqMatcher : IStringMatcher |
| | | 14 | | { |
| | | 15 | | private readonly string[] _patterns; |
| | | 16 | | |
| | | 17 | | /// <inheritdoc cref="IMatcher.MatchBehaviour"/> |
| | 8 | 18 | | public MatchBehaviour MatchBehaviour { get; } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Initializes a new instance of the <see cref="LinqMatcher"/> class. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="pattern">The pattern.</param> |
| | 6 | 24 | | public LinqMatcher([NotNull] string pattern) : this(new[] { pattern }) |
| | 6 | 25 | | { |
| | 6 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Initializes a new instance of the <see cref="LinqMatcher"/> class. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="patterns">The patterns.</param> |
| | 6 | 32 | | public LinqMatcher([NotNull] string[] patterns) : this(MatchBehaviour.AcceptOnMatch, patterns) |
| | 6 | 33 | | { |
| | 6 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Initializes a new instance of the <see cref="LinqMatcher"/> class. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="matchBehaviour">The match behaviour.</param> |
| | | 40 | | /// <param name="pattern">The pattern.</param> |
| | 2 | 41 | | public LinqMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern) : this(matchBehaviour, new[] { patte |
| | 2 | 42 | | { |
| | 2 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Initializes a new instance of the <see cref="LinqMatcher"/> class. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <param name="matchBehaviour">The match behaviour.</param> |
| | | 49 | | /// <param name="patterns">The patterns.</param> |
| | 10 | 50 | | public LinqMatcher(MatchBehaviour matchBehaviour, [NotNull] string[] patterns) |
| | 10 | 51 | | { |
| | 10 | 52 | | MatchBehaviour = matchBehaviour; |
| | 10 | 53 | | _patterns = patterns; |
| | 10 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc cref="IStringMatcher.IsMatch"/> |
| | | 57 | | public double IsMatch(string input) |
| | 3 | 58 | | { |
| | | 59 | | // Convert a single input string to a Queryable string-list with 1 entry. |
| | 3 | 60 | | IQueryable queryable = new[] { input }.AsQueryable(); |
| | | 61 | | |
| | | 62 | | // Use the Any(...) method to check if the result matches |
| | 9 | 63 | | double match = MatchScores.ToScore(_patterns.Select(pattern => queryable.Any(pattern))); |
| | | 64 | | |
| | 3 | 65 | | return MatchBehaviourHelper.Convert(MatchBehaviour, match); |
| | 3 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <inheritdoc cref="IObjectMatcher.IsMatch"/> |
| | | 69 | | public double IsMatch(object input) |
| | 2 | 70 | | { |
| | | 71 | | JObject value; |
| | 2 | 72 | | switch (input) |
| | | 73 | | { |
| | | 74 | | case JObject valueAsJObject: |
| | 1 | 75 | | value = valueAsJObject; |
| | 1 | 76 | | break; |
| | | 77 | | |
| | | 78 | | default: |
| | 1 | 79 | | value = JObject.FromObject(input); |
| | 1 | 80 | | break; |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | // Convert a single object to a Queryable JObject-list with 1 entry. |
| | 2 | 84 | | var queryable1 = new[] { value }.AsQueryable(); |
| | | 85 | | |
| | | 86 | | // Generate the DynamicLinq select statement. |
| | 2 | 87 | | string dynamicSelect = JsonUtils.GenerateDynamicLinqStatement(value); |
| | | 88 | | |
| | | 89 | | // Execute DynamicLinq Select statement. |
| | 2 | 90 | | var queryable2 = queryable1.Select(dynamicSelect); |
| | | 91 | | |
| | | 92 | | // Use the Any(...) method to check if the result matches. |
| | 6 | 93 | | double match = MatchScores.ToScore(_patterns.Select(pattern => queryable2.Any(pattern))); |
| | | 94 | | |
| | 2 | 95 | | return MatchBehaviourHelper.Convert(MatchBehaviour, match); |
| | 2 | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <inheritdoc cref="IStringMatcher.GetPatterns"/> |
| | | 99 | | public string[] GetPatterns() |
| | 4 | 100 | | { |
| | 4 | 101 | | return _patterns; |
| | 4 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <inheritdoc cref="IMatcher.Name"/> |
| | 2 | 105 | | public string Name => "LinqMatcher"; |
| | | 106 | | } |
| | | 107 | | } |