@typespec/http-server-csharp 0.58.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +40 -0
- package/dist/src/attributes.d.ts +43 -0
- package/dist/src/attributes.d.ts.map +1 -0
- package/dist/src/attributes.js +344 -0
- package/dist/src/attributes.js.map +1 -0
- package/dist/src/boilerplate.d.ts +4 -0
- package/dist/src/boilerplate.d.ts.map +1 -0
- package/dist/src/boilerplate.js +605 -0
- package/dist/src/boilerplate.js.map +1 -0
- package/dist/src/index.d.ts +4 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +4 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/interfaces.d.ts +129 -0
- package/dist/src/interfaces.d.ts.map +1 -0
- package/dist/src/interfaces.js +222 -0
- package/dist/src/interfaces.js.map +1 -0
- package/dist/src/lib.d.ts +58 -0
- package/dist/src/lib.d.ts.map +1 -0
- package/dist/src/lib.js +60 -0
- package/dist/src/lib.js.map +1 -0
- package/dist/src/service.d.ts +4 -0
- package/dist/src/service.d.ts.map +1 -0
- package/dist/src/service.js +829 -0
- package/dist/src/service.js.map +1 -0
- package/dist/src/testing/index.d.ts +3 -0
- package/dist/src/testing/index.d.ts.map +1 -0
- package/dist/src/testing/index.js +6 -0
- package/dist/src/testing/index.js.map +1 -0
- package/dist/src/type-helpers.d.ts +40 -0
- package/dist/src/type-helpers.d.ts.map +1 -0
- package/dist/src/type-helpers.js +77 -0
- package/dist/src/type-helpers.js.map +1 -0
- package/dist/src/utils.d.ts +48 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +628 -0
- package/dist/src/utils.js.map +1 -0
- package/package.json +77 -0
|
@@ -0,0 +1,605 @@
|
|
|
1
|
+
import { LibrarySourceFile } from "./interfaces.js";
|
|
2
|
+
export function getSerializationSourceFiles(emitter) {
|
|
3
|
+
const sourceFiles = [];
|
|
4
|
+
sourceFiles.push(new LibrarySourceFile({
|
|
5
|
+
filename: "TimeSpanDurationConverter.cs",
|
|
6
|
+
emitter: emitter,
|
|
7
|
+
getContents: getTimeSpanDurationConverter,
|
|
8
|
+
}), new LibrarySourceFile({
|
|
9
|
+
filename: "Base64UrlConverter.cs",
|
|
10
|
+
emitter: emitter,
|
|
11
|
+
getContents: getBase64UrlConverter,
|
|
12
|
+
}), new LibrarySourceFile({
|
|
13
|
+
filename: "UnixEpochDateTimeConverter.cs",
|
|
14
|
+
emitter: emitter,
|
|
15
|
+
getContents: getUnixEpochDateTimeConverter,
|
|
16
|
+
}), new LibrarySourceFile({
|
|
17
|
+
filename: "UnixEpochDateTimeOffsetConverter.cs",
|
|
18
|
+
emitter: emitter,
|
|
19
|
+
getContents: getUnixEpochDateTimeOffsetConverter,
|
|
20
|
+
}), new LibrarySourceFile({
|
|
21
|
+
filename: "NumericConstraintAttribute.cs",
|
|
22
|
+
emitter: emitter,
|
|
23
|
+
getContents: getNumericConstraintConverter,
|
|
24
|
+
}), new LibrarySourceFile({
|
|
25
|
+
filename: "StringConstraintAttribute.cs",
|
|
26
|
+
emitter: emitter,
|
|
27
|
+
getContents: getStringConstraintConverter,
|
|
28
|
+
}), new LibrarySourceFile({
|
|
29
|
+
filename: "ArrayConstraintAttribute.cs",
|
|
30
|
+
emitter: emitter,
|
|
31
|
+
getContents: getArrayConstraintConverter,
|
|
32
|
+
}), new LibrarySourceFile({
|
|
33
|
+
filename: "StringArrayConstraintAttribute.cs",
|
|
34
|
+
emitter: emitter,
|
|
35
|
+
getContents: getStringArrayConstraintConverter,
|
|
36
|
+
}), new LibrarySourceFile({
|
|
37
|
+
filename: "NumericArrayConstraintAttribute.cs",
|
|
38
|
+
emitter: emitter,
|
|
39
|
+
getContents: getNumericArrayConstraintConverter,
|
|
40
|
+
}));
|
|
41
|
+
return sourceFiles;
|
|
42
|
+
}
|
|
43
|
+
function getNumericConstraintConverter() {
|
|
44
|
+
return `// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
45
|
+
// Licensed under the MIT License.
|
|
46
|
+
// <auto-generated />
|
|
47
|
+
#nullable enable
|
|
48
|
+
|
|
49
|
+
using System.Numerics;
|
|
50
|
+
using System.Text.Json;
|
|
51
|
+
using System.Text.Json.Serialization;
|
|
52
|
+
|
|
53
|
+
namespace TypeSpec.Helpers.JsonConverters
|
|
54
|
+
{
|
|
55
|
+
/// <summary>
|
|
56
|
+
/// Provides numeric constraint validation at serialization time
|
|
57
|
+
/// </summary>
|
|
58
|
+
/// <typeparam name="T"></typeparam>
|
|
59
|
+
public class NumericConstraintAttribute<T> : JsonConverterAttribute where T: struct, INumber<T>
|
|
60
|
+
{
|
|
61
|
+
|
|
62
|
+
T? _minValue = null, _maxValue = null;
|
|
63
|
+
public NumericConstraintAttribute()
|
|
64
|
+
{
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/// <summary>
|
|
68
|
+
/// Provides the minimum value
|
|
69
|
+
/// </summary>
|
|
70
|
+
public T MinValue { get { return _minValue.HasValue ? _minValue.Value : default(T); } set { _minValue = value; } }
|
|
71
|
+
/// <summary>
|
|
72
|
+
/// Provides the maximum allowed value
|
|
73
|
+
/// </summary>
|
|
74
|
+
public T MaxValue { get { return _maxValue.HasValue ? _maxValue.Value : default(T); } set { _maxValue = value; } }
|
|
75
|
+
/// <summary>
|
|
76
|
+
/// If true, then values greater than but not equal to the minimum value are allowed
|
|
77
|
+
/// </summary>
|
|
78
|
+
public bool MinValueExclusive { get; set; }
|
|
79
|
+
/// <summary>
|
|
80
|
+
/// If true, values less than, but not equal to the provided maximum are allowed
|
|
81
|
+
/// </summary>
|
|
82
|
+
public bool MaxValueExclusive { get; set; }
|
|
83
|
+
|
|
84
|
+
public override JsonConverter? CreateConverter(Type typeToConvert)
|
|
85
|
+
{
|
|
86
|
+
return new NumericJsonConverter<T>(_minValue, _maxValue, MinValueExclusive, MaxValueExclusive);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
public class NumericJsonConverter<T> : JsonConverter<T> where T : struct, INumber<T>
|
|
91
|
+
{
|
|
92
|
+
string _rangeString;
|
|
93
|
+
public NumericJsonConverter(T? minValue = null, T? maxValue = null, bool? minValueExclusive = false, bool? maxValueExclusive = false, JsonSerializerOptions? options = null)
|
|
94
|
+
{
|
|
95
|
+
MinValue = minValue;
|
|
96
|
+
MaxValue = maxValue;
|
|
97
|
+
MinValueExclusive = minValueExclusive.HasValue ? minValueExclusive.Value : false;
|
|
98
|
+
MaxValueExclusive = maxValueExclusive.HasValue ? maxValueExclusive.Value : false;
|
|
99
|
+
_rangeString = $"{(MinValue.HasValue ? (MinValueExclusive ? $"({MinValue}" : $"[{MinValue}") : $"[{typeof(T).Name}.Min")}, {(MaxValue.HasValue ? (MaxValueExclusive ? $"{MaxValue})" : $"{MaxValue}]") : $"{typeof(T).Name}.Max]")}";
|
|
100
|
+
if ( options != null)
|
|
101
|
+
{
|
|
102
|
+
InnerConverter = options.GetConverter(typeof(T)) as JsonConverter<T>;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
protected T? MinValue { get; }
|
|
107
|
+
protected bool MinValueExclusive { get; }
|
|
108
|
+
protected T? MaxValue { get; }
|
|
109
|
+
|
|
110
|
+
protected bool MaxValueExclusive { get; }
|
|
111
|
+
|
|
112
|
+
private JsonConverter<T>? InnerConverter { get; set; }
|
|
113
|
+
|
|
114
|
+
private JsonConverter<T> GetInnerConverter(JsonSerializerOptions options)
|
|
115
|
+
{
|
|
116
|
+
if (InnerConverter == null)
|
|
117
|
+
{
|
|
118
|
+
InnerConverter = (JsonConverter<T>)options.GetConverter(typeof(T));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return InnerConverter;
|
|
122
|
+
}
|
|
123
|
+
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
124
|
+
{
|
|
125
|
+
var inner = GetInnerConverter(options);
|
|
126
|
+
T candidate = inner.Read(ref reader, typeToConvert, options);
|
|
127
|
+
ValidateRange(candidate);
|
|
128
|
+
return candidate;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
|
|
132
|
+
{
|
|
133
|
+
ValidateRange(value);
|
|
134
|
+
GetInnerConverter(options).Write(writer, value, options);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
protected virtual void ValidateRange(T value)
|
|
138
|
+
{
|
|
139
|
+
if ((MinValue.HasValue && (value < MinValue.Value || (value == MinValue.Value && MinValueExclusive)))
|
|
140
|
+
|| (MaxValue.HasValue && (value > MaxValue.Value || (value == MaxValue.Value && MaxValueExclusive))))
|
|
141
|
+
throw new JsonException($"{value} is outside the allowed range of {_rangeString}");
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}`;
|
|
145
|
+
}
|
|
146
|
+
function getStringConstraintConverter() {
|
|
147
|
+
return `// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
148
|
+
// Licensed under the MIT License.
|
|
149
|
+
// <auto-generated />
|
|
150
|
+
#nullable enable
|
|
151
|
+
|
|
152
|
+
using System.Text.Json;
|
|
153
|
+
using System.Text.Json.Serialization;
|
|
154
|
+
|
|
155
|
+
namespace TypeSpec.Helpers.JsonConverters
|
|
156
|
+
{
|
|
157
|
+
/// <summary>
|
|
158
|
+
/// Provides constraints for a string values property
|
|
159
|
+
/// </summary>
|
|
160
|
+
public class StringConstraint : JsonConverterAttribute
|
|
161
|
+
{
|
|
162
|
+
int? _minLength = null, _maxLength = null;
|
|
163
|
+
public StringConstraint()
|
|
164
|
+
{
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/// <summary>
|
|
168
|
+
/// The minimum length of the string
|
|
169
|
+
/// </summary>
|
|
170
|
+
public int MinLength { get { return _minLength.HasValue ? _minLength.Value : 0; } set { _minLength = value; } }
|
|
171
|
+
/// <summary>
|
|
172
|
+
/// The maximum length of the string
|
|
173
|
+
/// </summary>
|
|
174
|
+
public int MaxLength { get { return _maxLength.HasValue ? _maxLength.Value : 0; } set { _maxLength = value; } }
|
|
175
|
+
/// <summary>
|
|
176
|
+
/// The pattern that the string must match
|
|
177
|
+
/// </summary>
|
|
178
|
+
public string? Pattern { get; set; }
|
|
179
|
+
|
|
180
|
+
public override JsonConverter? CreateConverter(Type typeToConvert)
|
|
181
|
+
{
|
|
182
|
+
return new StringJsonConverter(_minLength, _maxLength, Pattern);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
public class StringJsonConverter : JsonConverter<string>
|
|
187
|
+
{
|
|
188
|
+
public StringJsonConverter(int? minLength, int? maxLength, string? pattern, JsonSerializerOptions? options = null)
|
|
189
|
+
{
|
|
190
|
+
MinLength = minLength;
|
|
191
|
+
MaxLength = maxLength;
|
|
192
|
+
Pattern = pattern;
|
|
193
|
+
if (options != null)
|
|
194
|
+
{
|
|
195
|
+
InnerConverter = options.GetConverter(typeof(string)) as JsonConverter<string>;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
protected int? MinLength { get; }
|
|
200
|
+
protected int? MaxLength { get; }
|
|
201
|
+
protected string? Pattern { get; }
|
|
202
|
+
|
|
203
|
+
private JsonConverter<string>? InnerConverter { get; set; }
|
|
204
|
+
|
|
205
|
+
private JsonConverter<string> GetInnerConverter(JsonSerializerOptions options)
|
|
206
|
+
{
|
|
207
|
+
if (InnerConverter == null)
|
|
208
|
+
{
|
|
209
|
+
InnerConverter = (JsonConverter<string>)options.GetConverter(typeof(string));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return InnerConverter;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
public override bool CanConvert(Type typeToConvert)
|
|
216
|
+
{
|
|
217
|
+
return base.CanConvert(typeToConvert);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
221
|
+
{
|
|
222
|
+
var innerConverter = GetInnerConverter(options);
|
|
223
|
+
string? candidate = innerConverter.Read(ref reader, typeToConvert, options);
|
|
224
|
+
if (MinLength.HasValue && (candidate == null || candidate.Length < MinLength.Value))
|
|
225
|
+
{
|
|
226
|
+
throw new JsonException($"String length less than minimum length {MinLength.Value}");
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (candidate != null)
|
|
230
|
+
{
|
|
231
|
+
if (MaxLength.HasValue && candidate.Length > MaxLength.Value)
|
|
232
|
+
{
|
|
233
|
+
throw new JsonException($"String length greater than maximum length {MaxLength.Value}");
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (Pattern != null && !System.Text.RegularExpressions.Regex.IsMatch(candidate, Pattern))
|
|
237
|
+
{
|
|
238
|
+
throw new JsonException($"String does not match pattern {Pattern}");
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return candidate;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
|
|
246
|
+
{
|
|
247
|
+
if (MinLength.HasValue && (value == null || value.Length < MinLength.Value))
|
|
248
|
+
{
|
|
249
|
+
throw new JsonException($"String length less than minimum length {MinLength.Value}");
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (value != null)
|
|
253
|
+
{
|
|
254
|
+
if (MaxLength.HasValue && value.Length > MaxLength.Value)
|
|
255
|
+
{
|
|
256
|
+
throw new JsonException($"String length greater than maximum length {MaxLength.Value}");
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (Pattern != null && !System.Text.RegularExpressions.Regex.IsMatch(value, Pattern))
|
|
260
|
+
{
|
|
261
|
+
throw new JsonException($"String does not match pattern {Pattern}");
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
GetInnerConverter(options).Write(writer, value, options);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}`;
|
|
269
|
+
}
|
|
270
|
+
function getArrayConstraintConverter() {
|
|
271
|
+
return `// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
272
|
+
// Licensed under the MIT License.
|
|
273
|
+
// <auto-generated />
|
|
274
|
+
#nullable enable
|
|
275
|
+
|
|
276
|
+
using System.Text.Json;
|
|
277
|
+
using System.Text.Json.Serialization;
|
|
278
|
+
|
|
279
|
+
namespace TypeSpec.Helpers.JsonConverters
|
|
280
|
+
{
|
|
281
|
+
/// <summary>
|
|
282
|
+
/// Constrains the number of elements in an array
|
|
283
|
+
/// </summary>
|
|
284
|
+
/// <typeparam name="T">The element type of the array</typeparam>
|
|
285
|
+
public class ArrayConstraintAttribute<T> : JsonConverterAttribute
|
|
286
|
+
{
|
|
287
|
+
int? _minItems = null, _maxItems = null;
|
|
288
|
+
/// <summary>
|
|
289
|
+
/// The smallest number of allowed items
|
|
290
|
+
/// </summary>
|
|
291
|
+
public int MinItems { get { return _minItems.HasValue ? _minItems.Value : 0; } set { _minItems = value; } }
|
|
292
|
+
/// <summary>
|
|
293
|
+
/// The largest number of allowed items
|
|
294
|
+
/// </summary>
|
|
295
|
+
public int MaxItems { get { return _maxItems.HasValue ? _maxItems.Value : 0; } set { _maxItems = value; } }
|
|
296
|
+
|
|
297
|
+
public ArrayConstraintAttribute()
|
|
298
|
+
{
|
|
299
|
+
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
public override JsonConverter? CreateConverter(Type typeToConvert)
|
|
303
|
+
{
|
|
304
|
+
return new ConstrainedArrayConverter<T>(_minItems, _maxItems);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
public class ConstrainedArrayConverter<T> : JsonConverter<T[]>
|
|
311
|
+
{
|
|
312
|
+
public ConstrainedArrayConverter(int? min, int? max) : base()
|
|
313
|
+
{
|
|
314
|
+
_minItems = min;
|
|
315
|
+
_maxItems = max;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
internal int? _minItems, _maxItems;
|
|
319
|
+
public JsonConverter<T>? InnerConverter { get; set; }
|
|
320
|
+
|
|
321
|
+
public virtual Func<ConstrainedArrayConverter<T>, JsonSerializerOptions, JsonConverter<T>> InnerConverterFactory { get; set; } = ConverterHelpers.GetStandardInnerConverter<T>;
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
internal bool ValidateMin(int count)
|
|
325
|
+
{
|
|
326
|
+
return !_minItems.HasValue || count >= _minItems.Value;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
internal bool ValidateMax(int count)
|
|
330
|
+
{
|
|
331
|
+
return !_maxItems.HasValue || count <= _maxItems.Value;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
internal void ValidateRange(int count)
|
|
335
|
+
{
|
|
336
|
+
if (!ValidateMax(count) || !ValidateMin(count))
|
|
337
|
+
{
|
|
338
|
+
throw new JsonException($"Number of array elements not in range [{(_minItems.HasValue ? _minItems.Value : 0)}, {(_maxItems.HasValue ? _maxItems.Value : Array.MaxLength)}]");
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
public override T[]? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
342
|
+
{
|
|
343
|
+
var _innerConverter = InnerConverterFactory(this, options);
|
|
344
|
+
if (reader.TokenType != JsonTokenType.StartArray) { throw new JsonException("Expected start of array"); }
|
|
345
|
+
var list = new List<T>();
|
|
346
|
+
int count = 0;
|
|
347
|
+
while (reader.Read())
|
|
348
|
+
{
|
|
349
|
+
if (reader.TokenType == JsonTokenType.EndArray) { ValidateRange(count); break; }
|
|
350
|
+
if (!ValidateMax(count)) { ValidateRange(count); break; }
|
|
351
|
+
T value = _innerConverter.Read(ref reader, typeof(T), options)!;
|
|
352
|
+
list.Add(value);
|
|
353
|
+
count++;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
return list.ToArray();
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
public override void Write(Utf8JsonWriter writer, T[] value, JsonSerializerOptions options)
|
|
362
|
+
{
|
|
363
|
+
var _innerConverter = InnerConverterFactory(this, options);
|
|
364
|
+
writer.WriteStartArray();
|
|
365
|
+
for (int i = 0; i < value.Length; ++i)
|
|
366
|
+
_innerConverter.Write(writer, value[i], options);
|
|
367
|
+
writer.WriteEndArray();
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
internal static class ConverterHelpers
|
|
372
|
+
{
|
|
373
|
+
internal static JsonConverter<T> GetStandardInnerConverter<T>(this ConstrainedArrayConverter<T> converter, JsonSerializerOptions options)
|
|
374
|
+
{
|
|
375
|
+
if (converter.InnerConverter == null)
|
|
376
|
+
{
|
|
377
|
+
converter.InnerConverter = (JsonConverter<T>)options.GetConverter(typeof(T));
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
return converter.InnerConverter;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}`;
|
|
384
|
+
}
|
|
385
|
+
function getNumericArrayConstraintConverter() {
|
|
386
|
+
return `// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
387
|
+
// Licensed under the MIT License.
|
|
388
|
+
// <auto-generated />
|
|
389
|
+
#nullable enable
|
|
390
|
+
|
|
391
|
+
using System.Numerics;
|
|
392
|
+
using System.Text.Json;
|
|
393
|
+
using System.Text.Json.Serialization;
|
|
394
|
+
|
|
395
|
+
namespace TypeSpec.Helpers.JsonConverters
|
|
396
|
+
{
|
|
397
|
+
/// <summary>
|
|
398
|
+
/// Constrains an array and the item types within it
|
|
399
|
+
/// </summary>
|
|
400
|
+
/// <typeparam name="T">The item type</typeparam>
|
|
401
|
+
public class NumericArrayConstraintAttribute<T> : ArrayConstraintAttribute<T> where T: struct, INumber<T>
|
|
402
|
+
{
|
|
403
|
+
T? _minValue = null, _maxValue = null;
|
|
404
|
+
public NumericArrayConstraintAttribute() : base()
|
|
405
|
+
{
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
public T MinValue { get { return _minValue.HasValue ? _minValue.Value : default(T); } set { _minValue = value; } }
|
|
409
|
+
public T MaxValue { get { return _maxValue.HasValue ? _maxValue.Value : default(T); } set { _maxValue = value; } }
|
|
410
|
+
bool MinValueExclusive { get; set; }
|
|
411
|
+
bool MaxValueExclusive { get; set; }
|
|
412
|
+
|
|
413
|
+
public override JsonConverter? CreateConverter(Type typeToConvert)
|
|
414
|
+
{
|
|
415
|
+
var result = base.CreateConverter(typeToConvert) as ConstrainedArrayConverter<T>;
|
|
416
|
+
if (result != null) result.InnerConverterFactory = (c, o) => new NumericJsonConverter<T>(MinValue, MaxValue, MinValueExclusive, MaxValueExclusive, o);
|
|
417
|
+
return result;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}`;
|
|
421
|
+
}
|
|
422
|
+
function getStringArrayConstraintConverter() {
|
|
423
|
+
return `// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
424
|
+
// Licensed under the MIT License.
|
|
425
|
+
// <auto-generated />
|
|
426
|
+
#nullable enable
|
|
427
|
+
|
|
428
|
+
using System.Text.Json;
|
|
429
|
+
using System.Text.Json.Serialization;
|
|
430
|
+
|
|
431
|
+
namespace TypeSpec.Helpers.JsonConverters
|
|
432
|
+
{
|
|
433
|
+
/// <summary>
|
|
434
|
+
/// Constrains an array of strings
|
|
435
|
+
/// </summary>
|
|
436
|
+
public class StringArrayConstraintAttribute : ArrayConstraintAttribute<string>
|
|
437
|
+
{
|
|
438
|
+
int? _minItemLength = null, _maxItemLength = null;
|
|
439
|
+
public StringArrayConstraintAttribute() : base()
|
|
440
|
+
{
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
public int MinItemLength { get { return _minItemLength.HasValue ? _minItemLength.Value : 0; } set { _minItemLength = value; } }
|
|
444
|
+
public int MaxItemLength { get { return _maxItemLength.HasValue ? _maxItemLength.Value : 0; } set { _maxItemLength = value; } }
|
|
445
|
+
public string? Pattern { get; set; }
|
|
446
|
+
|
|
447
|
+
override public JsonConverter<string[]> CreateConverter(Type typeToConvert)
|
|
448
|
+
{
|
|
449
|
+
var result = base.CreateConverter(typeToConvert) as ConstrainedArrayConverter<string>;
|
|
450
|
+
result!.InnerConverterFactory = (c, o) => new StringJsonConverter(MinItemLength, MaxItemLength, Pattern, o);
|
|
451
|
+
return result;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}`;
|
|
455
|
+
}
|
|
456
|
+
function getTimeSpanDurationConverter() {
|
|
457
|
+
return `// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
458
|
+
// Licensed under the MIT License.
|
|
459
|
+
// <auto-generated />
|
|
460
|
+
#nullable enable
|
|
461
|
+
|
|
462
|
+
using System.Text.Json;
|
|
463
|
+
using System.Text.Json.Serialization;
|
|
464
|
+
using System.Xml;
|
|
465
|
+
|
|
466
|
+
namespace TypeSpec.Helpers.JsonConverters
|
|
467
|
+
{
|
|
468
|
+
/// <summary>
|
|
469
|
+
/// Converts between Json duration and .Net TimeSpan
|
|
470
|
+
/// </summary>
|
|
471
|
+
public class TimespanDurationConverter : JsonConverter<TimeSpan>
|
|
472
|
+
{
|
|
473
|
+
public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
474
|
+
{
|
|
475
|
+
if (typeToConvert != typeof(TimeSpan))
|
|
476
|
+
throw new ArgumentException($"Cannot apply converter {this.GetType().FullName} to type {typeToConvert.FullName}");
|
|
477
|
+
|
|
478
|
+
var value = reader.GetString();
|
|
479
|
+
if (string.IsNullOrWhiteSpace(value)) return TimeSpan.MinValue;
|
|
480
|
+
return XmlConvert.ToTimeSpan(value);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions options)
|
|
484
|
+
{
|
|
485
|
+
writer.WriteStringValue(XmlConvert.ToString(value));
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
`;
|
|
490
|
+
}
|
|
491
|
+
function getBase64UrlConverter() {
|
|
492
|
+
return `// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
493
|
+
// Licensed under the MIT License.
|
|
494
|
+
// <auto-generated />
|
|
495
|
+
#nullable enable
|
|
496
|
+
|
|
497
|
+
using System.Text.Json;
|
|
498
|
+
using System.Text.Json.Serialization;
|
|
499
|
+
|
|
500
|
+
namespace TypeSpec.Helpers.JsonConverters
|
|
501
|
+
{
|
|
502
|
+
/// <summary>
|
|
503
|
+
/// System.Text.Json converter for the properties using Base64Url encoding
|
|
504
|
+
/// </summary>
|
|
505
|
+
public class Base64UrlJsonConverter : JsonConverter<byte[]>
|
|
506
|
+
{
|
|
507
|
+
/// <summary>
|
|
508
|
+
/// Adds padding to the input
|
|
509
|
+
/// </summary>
|
|
510
|
+
/// <param name="input"> the input string </param>
|
|
511
|
+
/// <returns> the padded string </returns>
|
|
512
|
+
private static string Pad(string input)
|
|
513
|
+
{
|
|
514
|
+
var count = 3 - ((input.Length + 3) % 4);
|
|
515
|
+
if (count == 0)
|
|
516
|
+
{
|
|
517
|
+
return input;
|
|
518
|
+
}
|
|
519
|
+
return $"{input}{new string('=', count)}";
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
public override byte[]? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
523
|
+
{
|
|
524
|
+
if (typeToConvert != typeof(byte[])) throw new ArgumentException($"Cannot apply converter {this.GetType().FullName} to type {typeToConvert.FullName}");
|
|
525
|
+
var value = reader.GetString();
|
|
526
|
+
if (string.IsNullOrWhiteSpace(value)) return null;
|
|
527
|
+
return Convert.FromBase64String(Pad(value.Replace('-', '+').Replace('_', '/')));
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
public override void Write(Utf8JsonWriter writer, byte[] value, JsonSerializerOptions options)
|
|
531
|
+
{
|
|
532
|
+
writer.WriteStringValue(Convert.ToBase64String(value).TrimEnd('=').Replace('+', '-').Replace('/', '_'));
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
`;
|
|
537
|
+
}
|
|
538
|
+
function getUnixEpochDateTimeConverter() {
|
|
539
|
+
return `// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
540
|
+
// Licensed under the MIT License.
|
|
541
|
+
// <auto-generated />
|
|
542
|
+
#nullable enable
|
|
543
|
+
|
|
544
|
+
using System.Text.Json;
|
|
545
|
+
using System.Text.Json.Serialization;
|
|
546
|
+
|
|
547
|
+
namespace TypeSpec.Helpers.JsonConverters
|
|
548
|
+
{
|
|
549
|
+
/// <summary>
|
|
550
|
+
/// Converts between an integer timestamp and a .Net DateTime
|
|
551
|
+
/// </summary>
|
|
552
|
+
public sealed class UnixEpochDateTimeConverter : JsonConverter<DateTime>
|
|
553
|
+
{
|
|
554
|
+
static readonly DateTime s_epoch = new DateTime(1970, 1, 1, 0, 0, 0);
|
|
555
|
+
|
|
556
|
+
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
557
|
+
{
|
|
558
|
+
var formatted = reader.GetInt64()!;
|
|
559
|
+
return s_epoch.AddMilliseconds(formatted);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
|
|
563
|
+
{
|
|
564
|
+
long unixTime = Convert.ToInt64((value - s_epoch).TotalMilliseconds);
|
|
565
|
+
writer.WriteNumberValue(unixTime);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
`;
|
|
570
|
+
}
|
|
571
|
+
function getUnixEpochDateTimeOffsetConverter() {
|
|
572
|
+
return `// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
573
|
+
// Licensed under the MIT License.
|
|
574
|
+
// <auto-generated />
|
|
575
|
+
#nullable enable
|
|
576
|
+
|
|
577
|
+
using System.Text.Json;
|
|
578
|
+
using System.Text.Json.Serialization;
|
|
579
|
+
|
|
580
|
+
namespace TypeSpec.Helpers.JsonConverters
|
|
581
|
+
{
|
|
582
|
+
/// <summary>
|
|
583
|
+
/// Converts between a Unix TimeStamp and a .Net DateTimeOffset
|
|
584
|
+
/// </summary>
|
|
585
|
+
public sealed class UnixEpochDateTimeOffsetConverter : JsonConverter<DateTimeOffset>
|
|
586
|
+
{
|
|
587
|
+
static readonly DateTimeOffset s_epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
591
|
+
{
|
|
592
|
+
var formatted = reader.GetInt64()!;
|
|
593
|
+
return s_epoch.AddMilliseconds(formatted);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)
|
|
597
|
+
{
|
|
598
|
+
long unixTime = Convert.ToInt64((value - s_epoch).TotalMilliseconds);
|
|
599
|
+
writer.WriteNumberValue(unixTime);
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
`;
|
|
604
|
+
}
|
|
605
|
+
//# sourceMappingURL=boilerplate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"boilerplate.js","sourceRoot":"","sources":["../../src/boilerplate.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS,4BAA4B,CAAC;AAEnD,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAChC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,SAAS,GAAG,yBAAyB,CAAC;AAEnD,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAChC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC"}
|