-
Notifications
You must be signed in to change notification settings - Fork 1
/
Syntaxe.cs
99 lines (81 loc) · 2.75 KB
/
Syntaxe.cs
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
96
97
98
99
namespace SyntaxeDotNet
{
public class ISyntaxeConfig
{
public dynamic? Data { get; set; }
public string? Schema { get; set; }
}
public class Syntaxe
{
private dynamic QueryData = null!;
private string QuerySchema = string.Empty;
public bool Success = false;
public string Error = string.Empty;
public Syntaxe() => Init(null!);
public Syntaxe(ISyntaxeConfig config) => Init(config);
private void Init(ISyntaxeConfig config)
{
if (config != null)
{
QueryData = config!.Data! ?? QueryData;
QuerySchema = config!.Schema! ?? QuerySchema;
}
Success = false;
Error = string.Empty;
}
public async Task<dynamic> Query() => await QueryDelegte(null!);
public async Task<dynamic> Query(ISyntaxeConfig config) => await QueryDelegte(config);
public async Task<dynamic> QueryDelegte(ISyntaxeConfig config)
{
try
{
Init(config);
if (QueryData == null)
Error = "'Data' is invalid.";
else if (QuerySchema == null)
Error = "'Schema' is invalid.";
if (!string.IsNullOrEmpty(Error))
return QueryData!;
var engine = new SyntaxeEngine();
IFilterSchemaResponse filtered = await engine.FilterSchema(QuerySchema!);
if (filtered.Status == true)
{
var result = await engine.WalkThroughHandler(new IWalkThroughContextConfig
{
Data = QueryData!,
Schema = filtered.Schema,
Status = filtered.Status
});
if (result == null)
{
Error = "Query failed. Check your schema and try again.";
return QueryData!;
}
Success = true;
return result;
} else
{
Error = filtered.Error!;
return await Task.FromResult(QueryData);
}
}
catch (Exception ex)
{
Error = ex.ToString();
return QueryData;
}
}
public Syntaxe Schema(string schema)
{
if (!string.IsNullOrEmpty(schema))
QuerySchema = schema!;
return this;
}
public Syntaxe Data(dynamic data)
{
if (data != null)
QueryData = data!;
return this;
}
}
}