private static AstFilter Translate(TranslationContext context, Expression inputExpression, Regex regex) { var inputFieldAst = ExpressionToFilterFieldTranslator.Translate(context, inputExpression); var regularExpression = new BsonRegularExpression(regex); return(AstFilter.Regex(inputFieldAst, regularExpression.Pattern, regularExpression.Options)); }
public void AstFilter_should_handle_nested_elemMatch() { var ast = AstFilter.ElemMatch( new AstFilterField("Details.A", BsonValueSerializer.Instance), AstFilter.ElemMatch( new AstFilterField("@<elem>", BsonValueSerializer.Instance), AstFilter.Regex(new AstFilterField("DeviceName", BsonValueSerializer.Instance), ".Name0.", ""))); var rendered = ast.Render(); rendered.Should().Be("{ 'Details.A' : { $elemMatch : { $elemMatch : { DeviceName : /.Name0./ } } } }"); }
private static AstFilter Translate(TranslationContext context, Expression expression, Expression inputExpression, Regex regex) { var inputFieldAst = ExpressionToFilterFieldTranslator.Translate(context, inputExpression); var regularExpression = new BsonRegularExpression(regex); if (inputFieldAst.Serializer is IRepresentationConfigurable representationConfigurable && representationConfigurable.Representation != BsonType.String) { throw new ExpressionNotSupportedException(inputExpression, expression, because: $"field \"{inputFieldAst.Path}\" is not represented as a string"); } return(AstFilter.Regex(inputFieldAst, regularExpression.Pattern, regularExpression.Options)); }
// private static methods private static AstFilter CreateRegexFilter(AstFilterField field, Modifiers modifiers, string pattern) { var combinedPattern = "^" + modifiers.LeadingPattern + pattern + modifiers.TrailingPattern + "$"; if (combinedPattern.StartsWith("^.*")) { combinedPattern = combinedPattern.Substring(3); } if (combinedPattern.EndsWith(".*$")) { combinedPattern = combinedPattern.Substring(0, combinedPattern.Length - 3); } var options = CreateRegexOptions(modifiers); return(AstFilter.Regex(field, combinedPattern, options)); }