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

Support lots of case insensitive text expressions through TextP.regex. #1578

Merged
merged 5 commits into from
May 13, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 31 additions & 3 deletions src/Core/ExpressionParsing/PFactory/PFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Gremlin.Net.Process.Traversal;
using System.Text.RegularExpressions;

using Gremlin.Net.Process.Traversal;

namespace ExRam.Gremlinq.Core.ExpressionParsing
{
Expand All @@ -8,7 +10,7 @@
{
private static readonly P PNeqNull = P.Neq(null);

public P TryGetP(ExpressionSemantics semantics, object? maybeValue, IGremlinQueryEnvironment environment)
public P? TryGetP(ExpressionSemantics semantics, object? maybeValue, IGremlinQueryEnvironment environment)
{
switch (semantics)
{
Expand Down Expand Up @@ -58,6 +60,32 @@
}
}
}
else if (!environment.Options.GetValue(GremlinqOption.DisabledTextPredicates).HasFlag(DisabledTextPredicates.Regex))
{
switch (stringExpressionSemantics)
{
case StringEqualsExpressionSemantics:
{
return TextP.Regex($"(?i)^{Regex.Escape(stringValue)}$");
}
case IsPrefixOfExpressionSemantics:
{
return TextP.Regex($"(?i)^{string.Join('|', SubStrings(stringValue).Select(x => $"({Regex.Escape((string)x)})"))}$");
}
case HasInfixExpressionSemantics:
{
return TextP.Regex($"(?i){Regex.Escape(stringValue)}");
}
case StartsWithExpressionSemantics:
{
return TextP.Regex($"(?i)^{Regex.Escape(stringValue)}");
}
case EndsWithExpressionSemantics:
{
return TextP.Regex($"(?i){Regex.Escape(stringValue)}$");
}
}
}

break;
}
Expand Down Expand Up @@ -95,7 +123,7 @@
}
}

throw new ExpressionNotSupportedException();
return null;

Check warning on line 126 in src/Core/ExpressionParsing/PFactory/PFactory.cs

View check run for this annotation

Codecov / codecov/patch

src/Core/ExpressionParsing/PFactory/PFactory.cs#L126

Added line #L126 was not covered by tests
}

private static object[] SubStrings(string value)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
g.V().hasLabel('Country').has('CountryCallingCode',containing('456')).project('id','label','properties').by(id).by(label).by(__.properties().group().by(label).by(__.project('id','label','value','properties').by(id).by(label).by(value).by(__.valueMap()).fold()))
g.V().hasLabel('Country').has('CountryCallingCode',regex('(?i)456')).project('id','label','properties').by(id).by(label).by(__.properties().group().by(label).by(__.project('id','label','value','properties').by(id).by(label).by(value).by(__.valueMap()).fold()))
Original file line number Diff line number Diff line change
@@ -1 +1 @@
g.V().hasLabel('Country').has('CountryCallingCode',endingWith('7890')).project('id','label','properties').by(id).by(label).by(__.properties().group().by(label).by(__.project('id','label','value','properties').by(id).by(label).by(value).by(__.valueMap()).fold()))
g.V().hasLabel('Country').has('CountryCallingCode',regex('(?i)7890$')).project('id','label','properties').by(id).by(label).by(__.properties().group().by(label).by(__.project('id','label','value','properties').by(id).by(label).by(value).by(__.valueMap()).fold()))
Original file line number Diff line number Diff line change
@@ -1 +1 @@
g.V().hasLabel('Country').has('CountryCallingCode',within('','+','+4','+49','+491','+4912','+49123')).project('id','label','properties').by(id).by(label).by(__.properties().group().by(label).by(__.project('id','label','value','properties').by(id).by(label).by(value).by(__.valueMap()).fold()))
g.V().hasLabel('Country').has('CountryCallingCode',regex('(?i)^()|(\+)|(\+4)|(\+49)|(\+491)|(\+4912)|(\+49123)$')).project('id','label','properties').by(id).by(label).by(__.properties().group().by(label).by(__.project('id','label','value','properties').by(id).by(label).by(value).by(__.valueMap()).fold()))
Original file line number Diff line number Diff line change
@@ -1 +1 @@
g.V().hasLabel('Country').has('CountryCallingCode',within('','+','+4','+49','+491','+4912','+49123')).project('id','label','properties').by(id).by(label).by(__.properties().group().by(label).by(__.project('id','label','value','properties').by(id).by(label).by(value).by(__.valueMap()).fold()))
g.V().hasLabel('Country').has('CountryCallingCode',regex('(?i)^()|(\+)|(\+4)|(\+49)|(\+491)|(\+4912)|(\+49123)$')).project('id','label','properties').by(id).by(label).by(__.properties().group().by(label).by(__.project('id','label','value','properties').by(id).by(label).by(value).by(__.valueMap()).fold()))
Original file line number Diff line number Diff line change
@@ -1 +1 @@
g.V().hasLabel('Country').has('CountryCallingCode',within('','+','+4','+49','+491','+4912','+49123')).project('id','label','properties').by(id).by(label).by(__.properties().group().by(label).by(__.project('id','label','value','properties').by(id).by(label).by(value).by(__.valueMap()).fold()))
g.V().hasLabel('Country').has('CountryCallingCode',regex('(?i)^()|(\+)|(\+4)|(\+49)|(\+491)|(\+4912)|(\+49123)$')).project('id','label','properties').by(id).by(label).by(__.properties().group().by(label).by(__.project('id','label','value','properties').by(id).by(label).by(value).by(__.valueMap()).fold()))
Original file line number Diff line number Diff line change
@@ -1 +1 @@
g.V().hasLabel('Country').has('CountryCallingCode',startingWith('+49123')).project('id','label','properties').by(id).by(label).by(__.properties().group().by(label).by(__.project('id','label','value','properties').by(id).by(label).by(value).by(__.valueMap()).fold()))
g.V().hasLabel('Country').has('CountryCallingCode',regex('(?i)^\+49123$')).project('id','label','properties').by(id).by(label).by(__.properties().group().by(label).by(__.project('id','label','value','properties').by(id).by(label).by(value).by(__.valueMap()).fold()))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
g.V().hasLabel('Country').has('CountryCallingCode',startingWith('+49123')).project('id','label','properties').by(id).by(label).by(__.properties().group().by(label).by(__.project('id','label','value','properties').by(id).by(label).by(value).by(__.valueMap()).fold()))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
g.V().hasLabel('Country').has('CountryCallingCode',regex('(?i)^\+49123')).project('id','label','properties').by(id).by(label).by(__.properties().group().by(label).by(__.project('id','label','value','properties').by(id).by(label).by(value).by(__.valueMap()).fold()))
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
Arguments: [
CountryCallingCode,
{
OperatorName: containing,
Value: 456
OperatorName: regex,
Value: (?i)456
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
Arguments: [
CountryCallingCode,
{
OperatorName: endingWith,
Value: 7890
OperatorName: regex,
Value: (?i)7890$
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,8 @@
Arguments: [
CountryCallingCode,
{
OperatorName: within,
Value: [
,
+,
+4,
+49,
+491,
+4912,
+49123
]
OperatorName: regex,
Value: (?i)^()|(\+)|(\+4)|(\+49)|(\+491)|(\+4912)|(\+49123)$
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,8 @@
Arguments: [
CountryCallingCode,
{
OperatorName: within,
Value: [
,
+,
+4,
+49,
+491,
+4912,
+49123
]
OperatorName: regex,
Value: (?i)^()|(\+)|(\+4)|(\+49)|(\+491)|(\+4912)|(\+49123)$
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,8 @@
Arguments: [
CountryCallingCode,
{
OperatorName: within,
Value: [
,
+,
+4,
+49,
+491,
+4912,
+49123
]
OperatorName: regex,
Value: (?i)^()|(\+)|(\+4)|(\+49)|(\+491)|(\+4912)|(\+49123)$
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
Arguments: [
CountryCallingCode,
{
OperatorName: startingWith,
Value: +49123
OperatorName: regex,
Value: (?i)^\+49123$
}
]
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
{
StepInstructions: [
{
OperatorName: V
},
{
OperatorName: hasLabel,
Arguments: [
Country
]
},
{
OperatorName: has,
Arguments: [
CountryCallingCode,
{
OperatorName: startingWith,
Value: +49123
}
]
},
{
OperatorName: project,
Arguments: [
id,
label,
properties
]
},
{
OperatorName: by,
Arguments: [
{
EnumName: T,
EnumValue: id
}
]
},
{
OperatorName: by,
Arguments: [
{
EnumName: T,
EnumValue: label
}
]
},
{
OperatorName: by,
Arguments: [
{
StepInstructions: [
{
OperatorName: properties
},
{
OperatorName: group
},
{
OperatorName: by,
Arguments: [
{
EnumName: T,
EnumValue: label
}
]
},
{
OperatorName: by,
Arguments: [
{
StepInstructions: [
{
OperatorName: project,
Arguments: [
id,
label,
value,
properties
]
},
{
OperatorName: by,
Arguments: [
{
EnumName: T,
EnumValue: id
}
]
},
{
OperatorName: by,
Arguments: [
{
EnumName: T,
EnumValue: label
}
]
},
{
OperatorName: by,
Arguments: [
{
EnumName: T,
EnumValue: value
}
]
},
{
OperatorName: by,
Arguments: [
{
StepInstructions: [
{
OperatorName: valueMap
}
]
}
]
},
{
OperatorName: fold
}
]
}
]
}
]
}
]
}
]
}