Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for regex matching #208

Merged
merged 1 commit into from
Apr 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions rethinkdb-net-test/Integration/BlankTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,5 +309,14 @@ public void ExpressionDateTimeWithMilliseconds()
var datetimems = connection.Run(Query.Expr(() => new DateTime(2014, 2, 3, 7, 30, 15, 500)));
Assert.That(datetimems, Is.EqualTo(new DateTime(2014, 2, 3, 7, 30, 15, 500)));
}

[Test]
public void RegexpMatch()
{
var match = connection.Run(Query.Expr(() => "hello".Match("l+")));
match.MatchedString.Should().Be("ll");
match.Start.Should().Be(2);
match.End.Should().Be(4);
}
}
}
12 changes: 12 additions & 0 deletions rethinkdb-net-test/Integration/MultiObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1121,5 +1121,17 @@ public void ToUpperInvariant()
connection.Run(testTable.Insert(new TestObject() { Id = "1", Name = "Has a Name" }));
connection.Run(testTable.Select(o => o.Name.ToUpperInvariant())).Single().Should().Be("HAS A NAME");
}

[Test]
public void FilterByRegexpMatches()
{
int count = 0;
foreach (var row in connection.Run(testTable.Filter(o => o.Name.Match("^3$") != null)))
{
row.Name.Should().Be("3");
count += 1;
}
count.Should().Be(1);
}
}
}
4 changes: 4 additions & 0 deletions rethinkdb-net/Expressions/StringExpressionConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public static void RegisterOnConverterFactory(DefaultExpressionConverterFactory
expressionConverterFactory.RegisterTemplateMapping<string, string>(
s => s.ToUpperInvariant(),
s => new Term() { type = Term.TermType.UPCASE, args = { s } });

expressionConverterFactory.RegisterTemplateMapping<string, string, MatchResponse>(
(@string, regexp) => ReQLExpression.Match(@string, regexp),
(@string, regexp) => new Term() { type = Term.TermType.MATCH, args = { @string, regexp } });
}
}
}
17 changes: 17 additions & 0 deletions rethinkdb-net/MatchGroupResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Runtime.Serialization;

namespace RethinkDb
{
[DataContract]
public class MatchGroupResponse
{
[DataMember(Name = "start")]
public uint Start;

[DataMember(Name = "end")]
public uint End;

[DataMember(Name = "str")]
public string MatchedString;
}
}
20 changes: 20 additions & 0 deletions rethinkdb-net/MatchResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Runtime.Serialization;

namespace RethinkDb
{
[DataContract]
public class MatchResponse
{
[DataMember(Name = "start")]
public uint Start;

[DataMember(Name = "end")]
public uint End;

[DataMember(Name = "str")]
public string MatchedString;

[DataMember(Name = "groups")]
public MatchGroupResponse[] Groups;
}
}
5 changes: 5 additions & 0 deletions rethinkdb-net/ReQLExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@ public static T Error<T>(string errorText)
{
throw new NotSupportedException("This method cannot be invoked directly, it can only be used as part of an expression tree.");
}

public static MatchResponse Match(this string @string, string regexp)
{
throw new NotSupportedException("This method cannot be invoked directly, it can only be used as part of an expression tree.");
}
}
}
2 changes: 2 additions & 0 deletions rethinkdb-net/rethinkdb-net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@
<Compile Include="Expressions\NullableExpressionConverters.cs" />
<Compile Include="DatumConverters\ObjectDatumConverterFactory.cs" />
<Compile Include="Expressions\StringExpressionConverters.cs" />
<Compile Include="MatchResponse.cs" />
<Compile Include="MatchGroupResponse.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down