-
Notifications
You must be signed in to change notification settings - Fork 520
Expand file tree
/
Copy pathQueryParser.cs
More file actions
95 lines (78 loc) · 2.62 KB
/
QueryParser.cs
File metadata and controls
95 lines (78 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using Squidex.Infrastructure.Json;
using Squidex.Infrastructure.Json.Objects;
using Squidex.Infrastructure.Reflection;
using Squidex.Infrastructure.Validation;
namespace Squidex.Infrastructure.Queries.Json;
public static class QueryParser
{
public static ClrQuery Parse(this QueryModel model, string json, IJsonSerializer serializer)
{
if (string.IsNullOrWhiteSpace(json))
{
return new ClrQuery();
}
var query = ParseFromJson(json, serializer);
return Convert(model, query);
}
public static ClrQuery Convert(this QueryModel model, Query<JsonValue> query)
{
if (query == null)
{
return new ClrQuery();
}
var result = SimpleMapper.Map(query, new ClrQuery());
var errors = new List<string>();
model.ConvertSorting(result, errors);
model.ConvertFilters(result, errors, query);
if (errors.Count > 0)
{
throw new ValidationException(errors.Select(BuildError).ToList());
}
return result;
}
private static void ConvertFilters(this QueryModel model, ClrQuery result, List<string> errors, Query<JsonValue> query)
{
if (query.Filter == null)
{
return;
}
var filter = JsonFilterVisitor.Parse(query.Filter, model, errors);
if (filter != null)
{
result.Filter = Optimizer<ClrValue>.Optimize(filter);
}
}
private static void ConvertSorting(this QueryModel model, ClrQuery result, List<string> errors)
{
if (result.Sort == null)
{
return;
}
foreach (var sorting in result.Sort)
{
sorting.Path.GetMatchingFields(model.Schema, errors);
}
}
public static Query<JsonValue> ParseFromJson(string json, IJsonSerializer serializer)
{
try
{
return serializer.Deserialize<Query<JsonValue>>(json);
}
catch (JsonException ex)
{
var error = Errors.InvalidQueryJson(ex.Message);
throw new ValidationException(error);
}
}
private static ValidationError BuildError(string message)
{
return new ValidationError(Errors.InvalidQuery(message));
}
}