Browse Source

IN operator. (#814)

pull/815/head
Sebastian Stehle 4 years ago
committed by GitHub
parent
commit
1391031e8b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 44
      backend/src/Squidex.Infrastructure/Json/Objects/JsonValue.cs
  2. 44
      backend/src/Squidex.Infrastructure/Queries/ClrValue.cs
  3. 12
      backend/src/Squidex.Infrastructure/Queries/Json/JsonFilterVisitor.cs
  4. 8
      backend/tests/Squidex.Infrastructure.Tests/Queries/QueryFromJsonTests.cs

44
backend/src/Squidex.Infrastructure/Json/Objects/JsonValue.cs

@ -70,28 +70,28 @@ namespace Squidex.Infrastructure.Json.Objects
switch (value) switch (value)
{ {
case string s: case string typed:
return Create(s); return Create(typed);
case bool b: case bool typed:
return Create(b); return Create(typed);
case float f: case float typed:
return Create(f); return Create(typed);
case double d: case double typed:
return Create(d); return Create(typed);
case int i: case int typed:
return Create(i); return Create(typed);
case long l: case long typed:
return Create(l); return Create(typed);
case Guid g: case Guid typed:
return Create(g); return Create(typed);
case DomainId i: case DomainId typed:
return Create(i); return Create(typed);
case Instant i: case Instant typed:
return Create(i); return Create(typed);
case object[] array: case object[] typed:
return Array(array); return Array(typed);
case IReadOnlyDictionary<string, object?> obj: case IReadOnlyDictionary<string, object?> typed:
return Object(obj); return Object(typed);
} }
throw new ArgumentException("Invalid json type", nameof(value)); throw new ArgumentException("Invalid json type", nameof(value));

44
backend/src/Squidex.Infrastructure/Queries/ClrValue.cs

@ -64,6 +64,11 @@ namespace Squidex.Infrastructure.Queries
return value != null ? new ClrValue(value, ClrValueType.String, false) : Null; return value != null ? new ClrValue(value, ClrValueType.String, false) : Null;
} }
public static implicit operator ClrValue(List<FilterSphere> value)
{
return value != null ? new ClrValue(value, ClrValueType.Sphere, true) : Null;
}
public static implicit operator ClrValue(List<Instant> value) public static implicit operator ClrValue(List<Instant> value)
{ {
return value != null ? new ClrValue(value, ClrValueType.Instant, true) : Null; return value != null ? new ClrValue(value, ClrValueType.Instant, true) : Null;
@ -109,6 +114,45 @@ namespace Squidex.Infrastructure.Queries
return value != null ? new ClrValue(value, ClrValueType.Dynamic, true) : Null; return value != null ? new ClrValue(value, ClrValueType.Dynamic, true) : Null;
} }
public ClrValue ToList()
{
var value = Value;
if (IsList || ValueType == ClrValueType.Null || value == null)
{
return this;
}
ClrValue BuildList<T>(T value)
{
return new ClrValue(new List<T> { value }, ValueType, true);
}
switch (value)
{
case FilterSphere typed:
return BuildList(typed);
case Instant typed:
return BuildList(typed);
case Guid typed:
return BuildList(typed);
case bool typed:
return BuildList(typed);
case float typed:
return BuildList(typed);
case double typed:
return BuildList(typed);
case int typed:
return BuildList(typed);
case long typed:
return BuildList(typed);
case string typed:
return BuildList(typed);
}
return this;
}
public override string ToString() public override string ToString()
{ {
if (Value is IList list) if (Value is IList list)

12
backend/src/Squidex.Infrastructure/Queries/Json/JsonFilterVisitor.cs

@ -73,10 +73,20 @@ namespace Squidex.Infrastructure.Queries.Json
if (value != null && isValidOperator) if (value != null && isValidOperator)
{ {
if (value.IsList && nodeIn.Operator != CompareOperator.In) if (nodeIn.Operator == CompareOperator.In)
{
if (!value.IsList)
{
value = value.ToList();
}
}
else
{
if (value.IsList)
{ {
args.Errors.Add($"Array value is not allowed for '{nodeIn.Operator}' operator and path '{nodeIn.Path}'."); args.Errors.Add($"Array value is not allowed for '{nodeIn.Operator}' operator and path '{nodeIn.Path}'.");
} }
}
if (nodeIn.Operator == CompareOperator.Matchs && value.Value?.ToString()?.IsValidRegex() != true) if (nodeIn.Operator == CompareOperator.Matchs && value.Value?.ToString()?.IsValidRegex() != true)
{ {

8
backend/tests/Squidex.Infrastructure.Tests/Queries/QueryFromJsonTests.cs

@ -491,6 +491,14 @@ namespace Squidex.Infrastructure.Queries
AssertErrors(json, "Array value is not allowed for 'Equals' operator and path 'string'."); AssertErrors(json, "Array value is not allowed for 'Equals' operator and path 'string'.");
} }
[Fact]
public void Should_convert_single_value_to_list_for_in_operator()
{
var json = new { path = "string", op = "in", value = "Hello" };
AssertFilter(json, "string in ['Hello']");
}
} }
[Fact] [Fact]

Loading…
Cancel
Save