-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
217 lines (186 loc) · 7.29 KB
/
Copy pathProgram.cs
File metadata and controls
217 lines (186 loc) · 7.29 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Reflection;
using System.Text.RegularExpressions;
using Json.Schema;
ICommandSource cmdSource = args.Length == 0 ? new ConsoleCommandSource() : new FileCommandSource(args[0]);
bool started = false;
BuildOptions? buildOptions = null;
var evaluationOptions = new EvaluationOptions();
var drafts = new Dictionary<string, Dialect> {
{ "https://json-schema.org/draft/2020-12/schema", Dialect.Draft202012 },
{ "https://json-schema.org/draft/2019-09/schema", Dialect.Draft201909 },
{ "http://json-schema.org/draft-07/schema#", Dialect.Draft07 },
{ "http://json-schema.org/draft-06/schema#", Dialect.Draft06 },
};
var unsupportedTests = new Dictionary<(string, string), string>();
while (cmdSource.GetNextCommand() is { } line && line != "")
{
var root = JsonNode.Parse(line);
var cmd = root["cmd"].GetValue<string>();
switch (cmd)
{
case "start":
var version = root["version"];
if (version.GetValue<int>() != 1)
{
throw new UnknownVersion(version);
}
started = true;
var startResult = new JsonObject {
["version"] = 1,
["implementation"] =
new JsonObject { ["language"] = "dotnet", ["name"] = "JsonSchema.Net",
["version"] = GetLibVersion(),
["homepage"] = "https://json-everything.net/json-schema/",
["documentation"] = "https://docs.json-everything.net/schema/basics/",
["issues"] = "https://github.com/gregsdennis/json-everything/issues",
["source"] = "https://github.com/gregsdennis/json-everything",
["dialects"] =
new JsonArray {
"https://json-schema.org/draft/2020-12/schema",
"https://json-schema.org/draft/2019-09/schema",
"http://json-schema.org/draft-07/schema#",
"http://json-schema.org/draft-06/schema#",
},
["os"] = Environment.OSVersion.Platform.ToString(),
["os_version"] = Environment.OSVersion.Version.ToString(),
["language_version"] = Environment.Version.ToString() },
};
Console.WriteLine(startResult.ToJsonString());
break;
case "dialect":
if (!started)
{
throw new NotStarted();
}
buildOptions = new() { SchemaRegistry = new(), Dialect = drafts[root["dialect"]!.GetValue<string>()] };
evaluationOptions = new EvaluationOptions {// for local debugging, change this to Verbose
OutputFormat = OutputFormat.Flag
};
var dialectResult = new JsonObject {
["ok"] = true,
};
Console.WriteLine(dialectResult.ToJsonString());
break;
case "run":
if (!started)
{
throw new NotStarted();
}
var testCase = root["case"];
var seq = root["seq"]!.DeepClone();
var testCaseDescription = testCase!["description"]!.GetValue<string>();
string? testDescription = null;
var schemaText = JsonSerializer.SerializeToElement(testCase["schema"]);
buildOptions!.SchemaRegistry = new();
var nullableRegistry = testCase["registry"];
if (nullableRegistry is not null)
{
var registry = nullableRegistry.AsObject().ToDictionary(x => new Uri(x.Key), x => x.Value);
buildOptions.SchemaRegistry.Fetch = (uri, _) =>
{
var element = JsonSerializer.SerializeToElement(registry[uri]);
return JsonSchema.Build(element, buildOptions);
};
}
var schema = JsonSchema.Build(schemaText, buildOptions);
var tests = testCase["tests"]!.AsArray();
try
{
var results = new JsonArray();
foreach (var test in tests)
{
testDescription = test!["description"]!.GetValue<string>();
var instance = JsonSerializer.SerializeToElement(test["instance"]);
var validationResult = schema.Evaluate(instance, evaluationOptions);
var testResult = JsonSerializer.SerializeToNode(validationResult);
results.Add(testResult);
}
var runResult = new JsonObject {
["seq"] = seq,
["results"] = results,
};
Console.WriteLine(runResult.ToJsonString());
}
catch (Exception)
when (unsupportedTests.TryGetValue((testCaseDescription, testDescription), out var message))
{
var skipResult = new JsonObject { ["seq"] = seq, ["skipped"] = true, ["message"] = message };
Console.WriteLine(skipResult.ToJsonString());
}
catch (Exception e)
{
var errorResult = new JsonObject {
["seq"] = seq,
["errored"] = true,
["context"] =
new JsonObject {
["message"] = e.ToString(),
["traceback"] = Environment.StackTrace,
},
};
Console.WriteLine(errorResult.ToJsonString());
}
break;
case "stop":
if (!started)
{
throw new NotStarted();
}
Environment.Exit(0);
break;
case null:
throw new UnknownCommand("Missing command!");
default:
throw new UnknownCommand(cmd);
}
}
static string GetLibVersion()
{
var attribute = typeof(JsonSchema).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
return Regex.Match(attribute!.InformationalVersion, @"\d+(\.\d+)+").Value;
}
class UnknownCommand : Exception
{
public UnknownCommand(string message) { }
}
class UnknownVersion : Exception
{
public UnknownVersion(JsonNode version) { }
}
class NotStarted : Exception
{
}
interface ICommandSource
{
string? GetNextCommand();
}
class ConsoleCommandSource : ICommandSource
{
public string? GetNextCommand()
{
return Console.ReadLine();
}
}
class FileCommandSource : ICommandSource
{
private readonly string[] _fileContents;
private int _line;
public FileCommandSource(string fileName)
{
_fileContents = File.ReadAllLines(fileName);
}
public string? GetNextCommand()
{
if (_line < _fileContents.Length)
{
return _fileContents[_line++];
}
return null;
}
}