forked from ch-robinson/dotnet-avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVerb.cs
262 lines (211 loc) · 9.48 KB
/
Verb.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using CommandLine;
using CommandLine.Text;
using Chr.Avro.Abstract;
using Chr.Avro.Codegen;
using Chr.Avro.Representation;
using Chr.Avro.Resolution;
using System;
using System.Collections.Generic;
using System.Reflection;
using Chr.Avro.Serialization;
namespace Chr.Avro.Cli
{
public abstract class Verb
{
public int Execute()
{
try
{
Run();
}
catch (ProgramException e)
{
if (e.Message is var message && !string.IsNullOrEmpty(message))
{
Console.Error.WriteLine(message);
}
return e.Code;
}
return 0;
}
protected abstract void Run();
}
[Verb("create", HelpText = "Create an Avro schema for a .NET type.")]
public class CreateSchemaVerb : Verb, IClrTypeOptions
{
[Option('a', "assembly", HelpText = "The name of or path to an assembly that contains the type.")]
public string AssemblyName { get; set; }
[Option("enums-as-integers", HelpText = "Whether enums should be represented with \"int\" or \"long\" schemas.")]
public bool EnumsAsIntegers { get; set; }
[Option("nullable-references", HelpText = "Whether reference types should be represented with nullable union schemas.")]
public bool NullableReferences { get; set; }
[Option('t', "type", Required = true, HelpText = "The type to build a schema for.")]
public string TypeName { get; set; }
[Usage(ApplicationAlias = "dotnet avro")]
public static IEnumerable<Example> Examples => new List<Example>
{
new Example("Create a schema for a built-in type", new CreateSchemaVerb
{
TypeName = "System.DateTime"
}),
new Example("Create a schema for a type in a compiled assembly", new CreateSchemaVerb
{
AssemblyName = "./out/Example.Models.dll",
TypeName = "Example.Models.ExampleModel"
}),
};
protected override void Run()
{
var schema = CreateSchema();
var writer = new JsonSchemaWriter();
Console.WriteLine(writer.Write(schema));
}
protected Schema CreateSchema()
{
var type = this.ResolveType();
var resolver = new DataContractResolver(
resolveReferenceTypesAsNullable: NullableReferences,
resolveUnderlyingEnumTypes: EnumsAsIntegers
);
var builder = new SchemaBuilder(typeResolver: resolver);
try
{
return builder.BuildSchema(type);
}
catch (UnsupportedTypeException inner)
{
throw new ProgramException(message: $"Failed to create a schema for {type}: The type is not supported by the resolver.", inner: inner);
}
}
}
[Verb("generate", HelpText = "Generates C# code for a schema from the Schema Registry.")]
public class GenerateCodeVerb : Verb, ISchemaResolutionOptions
{
[Usage(ApplicationAlias = "dotnet avro")]
public static IEnumerable<Example> Examples => new List<Example>
{
new Example("Generate code for a schema by ID", new GenerateCodeVerb
{
RegistryUrl = "http://registry:8081",
SchemaId = 120,
}),
};
private const string ByIdSet = "ById";
private const string BySubjectSet = "BySubject";
[Option('r', "registry-url", Required = true, HelpText = "The URL of the schema registry.")]
public string RegistryUrl { get; set; }
[Option('i', "id", Required = true, SetName = ByIdSet, HelpText = "If a subject/version is not specified, the ID of the schema.")]
public int? SchemaId { get; set; }
[Option('s', "subject", Required = true, SetName = BySubjectSet, HelpText = "If an ID is not specified, the subject of the schema.")]
public string SchemaSubject { get; set; }
[Option('v', "version", SetName = BySubjectSet, HelpText = "The version of the schema.")]
public int? SchemaVersion { get; set; }
protected override void Run()
{
var task = this.ResolveSchema();
task.Wait();
var generator = new CSharpCodeGenerator();
var reader = new JsonSchemaReader();
var schema = reader.Read(task.Result);
try
{
Console.WriteLine(generator.WriteCompilationUnit(schema));
}
catch (Exception exception)
{
throw new ProgramException(message: exception.Message, inner: exception);
}
}
}
[Verb("registry-get", HelpText = "Retrieve a schema from the Schema Registry.")]
public class GetSchemaVerb : Verb, ISchemaResolutionOptions
{
[Usage(ApplicationAlias = "dotnet avro")]
public static IEnumerable<Example> Examples => new List<Example>
{
new Example("Get a schema by ID", new GetSchemaVerb
{
RegistryUrl = "http://registry:8081",
SchemaId = 120,
}),
};
private const string ByIdSet = "ById";
private const string BySubjectSet = "BySubject";
[Option('r', "registry-url", Required = true, HelpText = "The URL of the schema registry.")]
public string RegistryUrl { get; set; }
[Option('i', "id", Required = true, SetName = ByIdSet, HelpText = "If a subject/version is not specified, the ID of the schema.")]
public int? SchemaId { get; set; }
[Option('s', "subject", Required = true, SetName = BySubjectSet, HelpText = "If an ID is not specified, the subject of the schema.")]
public string SchemaSubject { get; set; }
[Option('v', "version", SetName = BySubjectSet, HelpText = "The version of the schema.")]
public int? SchemaVersion { get; set; }
protected override void Run()
{
var task = this.ResolveSchema();
task.Wait();
Console.WriteLine(task.Result);
}
}
[Verb("registry-test", HelpText = "Verify that a .NET type is compatible with a schema in the Schema Registry.")]
public class TestSchemaVerb : Verb, IClrTypeOptions, ISchemaResolutionOptions
{
[Usage(ApplicationAlias = "dotnet avro")]
public static IEnumerable<Example> Examples => new List<Example>
{
new Example("Test that a type works with the latest version of a subject", new TestSchemaVerb
{
AssemblyName = "./out/Example.Models.dll",
RegistryUrl = "http://registry:8081",
SchemaSubject = "example_subject",
TypeName = "Example.Models.ExampleModel",
}),
};
private const string ByIdSet = "ById";
private const string BySubjectSet = "BySubject";
[Option('a', "assembly", HelpText = "The name of or path to an assembly that contains the type.")]
public string AssemblyName { get; set; }
[Option('t', "type", Required = true, HelpText = "The type to test.")]
public string TypeName { get; set; }
[Option('r', "registry-url", Required = true, HelpText = "The URL of the schema registry.")]
public string RegistryUrl { get; set; }
[Option('i', "id", Required = true, SetName = ByIdSet, HelpText = "If a subject/version is not specified, the ID of the target schema.")]
public int? SchemaId { get; set; }
[Option('s', "subject", Required = true, SetName = BySubjectSet, HelpText = "If an ID is not specified, the subject of the target schema.")]
public string SchemaSubject { get; set; }
[Option('v', "version", SetName = BySubjectSet, HelpText = "The version of the target schema.")]
public int? SchemaVersion { get; set; }
protected override void Run()
{
var type = this.ResolveType();
var task = this.ResolveSchema();
task.Wait();
var reader = new JsonSchemaReader();
var schema = reader.Read(task.Result);
try
{
var builder = new BinaryDeserializerBuilder();
var method = typeof(IBinaryDeserializerBuilder)
.GetMethod(nameof(IBinaryDeserializerBuilder.BuildDeserializer))
.MakeGenericMethod(type);
method.Invoke(builder, new[] { schema });
}
catch (TargetInvocationException exception) when (exception.InnerException is Exception inner)
{
throw new ProgramException(message: $"A deserializer cannot be created for {type}: {inner.Message}", inner: inner);
}
try
{
var builder = new BinarySerializerBuilder();
var method = typeof(IBinarySerializerBuilder)
.GetMethod(nameof(IBinarySerializerBuilder.BuildSerializer))
.MakeGenericMethod(type);
method.Invoke(builder, new[] { schema });
}
catch (TargetInvocationException exception) when (exception.InnerException is Exception inner)
{
throw new ProgramException(message: $"A serializer cannot be created for {type}: {inner.Message}", inner: inner);
}
Console.Error.WriteLine($"{type} is compatible with the schema.");
}
}
}