using System.Linq; using System.Linq.Dynamic.Core; using JetBrains.Annotations; namespace WireMock.Matchers { /// /// System.Linq.Dynamic.Core Expression Matcher /// /// public class LinqMatcher : IStringMatcher { private readonly string[] _patterns; /// public MatchBehaviour MatchBehaviour { get; } /// /// Initializes a new instance of the class. /// /// The pattern. public LinqMatcher([NotNull] string pattern) : this(new[] { pattern }) { } /// /// Initializes a new instance of the class. /// /// The patterns. public LinqMatcher([NotNull] string[] patterns) : this(MatchBehaviour.AcceptOnMatch, patterns) { } /// /// Initializes a new instance of the class. /// /// The match behaviour. /// The pattern. public LinqMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern) : this(matchBehaviour, new[] { pattern }) { } /// /// Initializes a new instance of the class. /// /// The match behaviour. /// The patterns. public LinqMatcher(MatchBehaviour matchBehaviour, [NotNull] string[] patterns) { MatchBehaviour = matchBehaviour; _patterns = patterns; } /// public double IsMatch(string input) { // Convert a single input string to a Queryable string-list with 1 entry. IQueryable queryable = new[] { input }.AsQueryable(); // Use the Any(...) method to check if the result matches double match = MatchScores.ToScore(_patterns.Select(pattern => queryable.Any(pattern))); return MatchBehaviourHelper.Convert(MatchBehaviour, match); } ///// //public double IsMatch(object input) //{ // object value; // switch (input) // { // case JObject valueAsJObject: // value = valueAsJObject.ToObject(); // break; // default: // value = input; // break; // } // // Convert a single object to a Queryable object-list with 1 entry. // IQueryable queryable = new[] { value }.AsQueryable().Select("new (it as x)"); // // Use the Any(...) method to check if the result matches // double match = MatchScores.ToScore(_patterns.Select(pattern => queryable.Any(pattern))); // return MatchBehaviourHelper.Convert(MatchBehaviour, match); //} /// public string[] GetPatterns() { return _patterns; } /// public string Name => "LinqMatcher"; } }