-
Notifications
You must be signed in to change notification settings - Fork 57
/
everything.ts
619 lines (544 loc) · 21 KB
/
everything.ts
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*
Generated by atdts from type definitions in 'everything.atd'.
Type-safe translations from/to JSON
For each type 'Foo', there is a pair of functions:
- 'writeFoo': convert a 'Foo' value into a JSON-compatible value.
- 'readFoo': convert a JSON-compatible value into a TypeScript value
of type 'Foo'.
*/
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
/* tslint:disable */
/* eslint-disable */
export type Anything = any
export type DifferentKindsOfThings =
| { kind: 'Root' }
| { kind: 'Thing'; value: number /*int*/ }
| { kind: 'WOW' /* JSON: "wow" */ }
| { kind: 'Amaze' /* JSON: "!!!" */; value: string[] }
export type This = number /*int*/
export type SimpleRecord = {
str: string;
}
export type Root = {
id: string;
this_: This;
items: number /*int*/[][];
maybe?: number /*int*/;
maybe2?: SimpleRecord;
extras: number /*int*/[];
answer: number /*int*/;
aliased: Alias;
point: [number, number];
kinds: DifferentKindsOfThings[];
assoc1: [number, number /*int*/][];
assoc2: [string, number /*int*/][];
assoc3: Map<number, number /*int*/>;
assoc4: Map<string, number /*int*/>;
options: Option<number /*int*/>[];
nullables: (number /*int*/ | null)[];
untyped_things: any[];
foo: (Foo | null);
parametrized_record: IntFloatParametrizedRecord;
parametrized_tuple: TupleE1a4b40;
anything: Anything;
wrapped: string[];
}
export type Alias = number /*int*/[]
export type Pair = [string, number /*int*/]
export type Foo = {
foo: string;
}
export type SpecialString = string
export type IntFloatParametrizedRecord = {
field_a: number /*int*/;
field_b: number[];
}
export type TupleE1a4b40 = [DifferentKindsOfThings, DifferentKindsOfThings, number /*int*/]
export function writeAnything(x: Anything, context: any = x): any {
return ((x: any, context): any => x)(x, context);
}
export function readAnything(x: any, context: any = x): Anything {
return ((x: any, context): any => x)(x, context);
}
export function writeDifferentKindsOfThings(x: DifferentKindsOfThings, context: any = x): any {
switch (x.kind) {
case 'Root':
return 'Root'
case 'Thing':
return ['Thing', _atd_write_int(x.value, x)]
case 'WOW':
return 'wow'
case 'Amaze':
return ['!!!', _atd_write_array(_atd_write_string)(x.value, x)]
}
}
export function readDifferentKindsOfThings(x: any, context: any = x): DifferentKindsOfThings {
if (typeof x === 'string') {
switch (x) {
case 'Root':
return { kind: 'Root' }
case 'wow':
return { kind: 'WOW' }
default:
_atd_bad_json('DifferentKindsOfThings', x, context)
throw new Error('impossible')
}
}
else {
_atd_check_json_tuple(2, x, context)
switch (x[0]) {
case 'Thing':
return { kind: 'Thing', value: _atd_read_int(x[1], x) }
case '!!!':
return { kind: 'Amaze', value: _atd_read_array(_atd_read_string)(x[1], x) }
default:
_atd_bad_json('DifferentKindsOfThings', x, context)
throw new Error('impossible')
}
}
}
export function writeThis(x: This, context: any = x): any {
return _atd_write_int(x, context);
}
export function readThis(x: any, context: any = x): This {
return _atd_read_int(x, context);
}
export function writeSimpleRecord(x: SimpleRecord, context: any = x): any {
return {
'str': _atd_write_required_field('SimpleRecord', 'str', _atd_write_string, x.str, x),
};
}
export function readSimpleRecord(x: any, context: any = x): SimpleRecord {
return {
str: _atd_read_required_field('SimpleRecord', 'str', _atd_read_string, x['str'], x),
};
}
export function writeRoot(x: Root, context: any = x): any {
return {
'ID': _atd_write_required_field('Root', 'id', _atd_write_string, x.id, x),
'this': _atd_write_required_field('Root', 'this', writeThis, x.this_, x),
'items': _atd_write_required_field('Root', 'items', _atd_write_array(_atd_write_array(_atd_write_int)), x.items, x),
'maybe': _atd_write_optional_field(_atd_write_int, x.maybe, x),
'maybe2': _atd_write_optional_field(writeSimpleRecord, x.maybe2, x),
'extras': _atd_write_field_with_default(_atd_write_array(_atd_write_int), [], x.extras, x),
'answer': _atd_write_field_with_default(_atd_write_int, 42, x.answer, x),
'aliased': _atd_write_required_field('Root', 'aliased', writeAlias, x.aliased, x),
'point': _atd_write_required_field('Root', 'point', ((x, context) => [_atd_write_float(x[0], x), _atd_write_float(x[1], x)]), x.point, x),
'kinds': _atd_write_required_field('Root', 'kinds', _atd_write_array(writeDifferentKindsOfThings), x.kinds, x),
'assoc1': _atd_write_required_field('Root', 'assoc1', _atd_write_array(((x, context) => [_atd_write_float(x[0], x), _atd_write_int(x[1], x)])), x.assoc1, x),
'assoc2': _atd_write_required_field('Root', 'assoc2', _atd_write_assoc_array_to_object(_atd_write_int), x.assoc2, x),
'assoc3': _atd_write_required_field('Root', 'assoc3', _atd_write_assoc_map_to_array(_atd_write_float, _atd_write_int), x.assoc3, x),
'assoc4': _atd_write_required_field('Root', 'assoc4', _atd_write_assoc_map_to_object(_atd_write_int), x.assoc4, x),
'options': _atd_write_field_with_default(_atd_write_array(_atd_write_option(_atd_write_int)), [], x.options, x),
'nullables': _atd_write_field_with_default(_atd_write_array(_atd_write_nullable(_atd_write_int)), [], x.nullables, x),
'untyped_things': _atd_write_required_field('Root', 'untyped_things', _atd_write_array(((x: any, context): any => x)), x.untyped_things, x),
'foo': _atd_write_required_field('Root', 'foo', _atd_write_nullable(writeFoo), x.foo, x),
'parametrized_record': _atd_write_required_field('Root', 'parametrized_record', writeIntFloatParametrizedRecord, x.parametrized_record, x),
'parametrized_tuple': _atd_write_required_field('Root', 'parametrized_tuple', writeTupleE1a4b40, x.parametrized_tuple, x),
'anything': _atd_write_required_field('Root', 'anything', writeAnything, x.anything, x),
'wrapped': _atd_write_required_field('Root', 'wrapped', _atd_write_array(_atd_write_string), x.wrapped, x),
};
}
export function readRoot(x: any, context: any = x): Root {
return {
id: _atd_read_required_field('Root', 'ID', _atd_read_string, x['ID'], x),
this_: _atd_read_required_field('Root', 'this', readThis, x['this'], x),
items: _atd_read_required_field('Root', 'items', _atd_read_array(_atd_read_array(_atd_read_int)), x['items'], x),
maybe: _atd_read_optional_field(_atd_read_int, x['maybe'], x),
maybe2: _atd_read_optional_field(readSimpleRecord, x['maybe2'], x),
extras: _atd_read_field_with_default(_atd_read_array(_atd_read_int), [], x['extras'], x),
answer: _atd_read_field_with_default(_atd_read_int, 42, x['answer'], x),
aliased: _atd_read_required_field('Root', 'aliased', readAlias, x['aliased'], x),
point: _atd_read_required_field('Root', 'point', ((x, context): [number, number] => { _atd_check_json_tuple(2, x, context); return [_atd_read_float(x[0], x), _atd_read_float(x[1], x)] }), x['point'], x),
kinds: _atd_read_required_field('Root', 'kinds', _atd_read_array(readDifferentKindsOfThings), x['kinds'], x),
assoc1: _atd_read_required_field('Root', 'assoc1', _atd_read_array(((x, context): [number, number /*int*/] => { _atd_check_json_tuple(2, x, context); return [_atd_read_float(x[0], x), _atd_read_int(x[1], x)] })), x['assoc1'], x),
assoc2: _atd_read_required_field('Root', 'assoc2', _atd_read_assoc_object_into_array(_atd_read_int), x['assoc2'], x),
assoc3: _atd_read_required_field('Root', 'assoc3', _atd_read_assoc_array_into_map(_atd_read_float, _atd_read_int), x['assoc3'], x),
assoc4: _atd_read_required_field('Root', 'assoc4', _atd_read_assoc_object_into_map(_atd_read_int), x['assoc4'], x),
options: _atd_read_field_with_default(_atd_read_array(_atd_read_option(_atd_read_int)), [], x['options'], x),
nullables: _atd_read_field_with_default(_atd_read_array(_atd_read_nullable(_atd_read_int)), [], x['nullables'], x),
untyped_things: _atd_read_required_field('Root', 'untyped_things', _atd_read_array(((x: any, context): any => x)), x['untyped_things'], x),
foo: _atd_read_required_field('Root', 'foo', _atd_read_nullable(readFoo), x['foo'], x),
parametrized_record: _atd_read_required_field('Root', 'parametrized_record', readIntFloatParametrizedRecord, x['parametrized_record'], x),
parametrized_tuple: _atd_read_required_field('Root', 'parametrized_tuple', readTupleE1a4b40, x['parametrized_tuple'], x),
anything: _atd_read_required_field('Root', 'anything', readAnything, x['anything'], x),
wrapped: _atd_read_required_field('Root', 'wrapped', _atd_read_array(_atd_read_string), x['wrapped'], x),
};
}
export function writeAlias(x: Alias, context: any = x): any {
return _atd_write_array(_atd_write_int)(x, context);
}
export function readAlias(x: any, context: any = x): Alias {
return _atd_read_array(_atd_read_int)(x, context);
}
export function writePair(x: Pair, context: any = x): any {
return ((x, context) => [_atd_write_string(x[0], x), _atd_write_int(x[1], x)])(x, context);
}
export function readPair(x: any, context: any = x): Pair {
return ((x, context): [string, number /*int*/] => { _atd_check_json_tuple(2, x, context); return [_atd_read_string(x[0], x), _atd_read_int(x[1], x)] })(x, context);
}
export function writeFoo(x: Foo, context: any = x): any {
return {
'foo': _atd_write_required_field('Foo', 'foo', _atd_write_string, x.foo, x),
};
}
export function readFoo(x: any, context: any = x): Foo {
return {
foo: _atd_read_required_field('Foo', 'foo', _atd_read_string, x['foo'], x),
};
}
export function writeSpecialString(x: SpecialString, context: any = x): any {
return _atd_write_string(x, context);
}
export function readSpecialString(x: any, context: any = x): SpecialString {
return _atd_read_string(x, context);
}
export function writeIntFloatParametrizedRecord(x: IntFloatParametrizedRecord, context: any = x): any {
return {
'field_a': _atd_write_required_field('IntFloatParametrizedRecord', 'field_a', _atd_write_int, x.field_a, x),
'field_b': _atd_write_field_with_default(_atd_write_array(_atd_write_float), [], x.field_b, x),
};
}
export function readIntFloatParametrizedRecord(x: any, context: any = x): IntFloatParametrizedRecord {
return {
field_a: _atd_read_required_field('IntFloatParametrizedRecord', 'field_a', _atd_read_int, x['field_a'], x),
field_b: _atd_read_field_with_default(_atd_read_array(_atd_read_float), [], x['field_b'], x),
};
}
export function writeTupleE1a4b40(x: TupleE1a4b40, context: any = x): any {
return ((x, context) => [writeDifferentKindsOfThings(x[0], x), writeDifferentKindsOfThings(x[1], x), _atd_write_int(x[2], x)])(x, context);
}
export function readTupleE1a4b40(x: any, context: any = x): TupleE1a4b40 {
return ((x, context): [DifferentKindsOfThings, DifferentKindsOfThings, number /*int*/] => { _atd_check_json_tuple(3, x, context); return [readDifferentKindsOfThings(x[0], x), readDifferentKindsOfThings(x[1], x), _atd_read_int(x[2], x)] })(x, context);
}
/////////////////////////////////////////////////////////////////////
// Runtime library
/////////////////////////////////////////////////////////////////////
export type Option<T> = null | { value: T }
function _atd_missing_json_field(type_name: string, json_field_name: string) {
throw new Error(`missing field '${json_field_name}'` +
` in JSON object of type '${type_name}'`)
}
function _atd_missing_ts_field(type_name: string, ts_field_name: string) {
throw new Error(`missing field '${ts_field_name}'` +
` in TypeScript object of type '${type_name}'`)
}
function _atd_bad_json(expected_type: string, json_value: any, context: any) {
let value_str = JSON.stringify(json_value)
if (value_str.length > 200)
value_str = value_str.substring(0, 200) + '…';
throw new Error(`incompatible JSON value where` +
` type '${expected_type}' was expected: '${value_str}'.` +
` Occurs in '${JSON.stringify(context)}'.`)
}
function _atd_bad_ts(expected_type: string, ts_value: any, context: any) {
let value_str = JSON.stringify(ts_value)
if (value_str.length > 200)
value_str = value_str.substring(0, 200) + '…';
throw new Error(`incompatible TypeScript value where` +
` type '${expected_type}' was expected: '${value_str}'.` +
` Occurs in '${JSON.stringify(context)}'.`)
}
function _atd_check_json_tuple(len: number /*int*/, x: any, context: any) {
if (! Array.isArray(x) || x.length !== len)
_atd_bad_json('tuple of length ' + len, x, context);
}
function _atd_read_unit(x: any, context: any): null {
if (x === null)
return null
else {
_atd_bad_json('null', x, context)
throw new Error('impossible')
}
}
function _atd_read_bool(x: any, context: any): boolean {
if (typeof x === 'boolean')
return x
else {
_atd_bad_json('boolean', x, context)
throw new Error('impossible')
}
}
function _atd_read_int(x: any, context: any): number /*int*/ {
if (Number.isInteger(x))
return x
else {
_atd_bad_json('integer', x, context)
throw new Error('impossible')
}
}
function _atd_read_float(x: any, context: any): number {
if (isFinite(x))
return x
else {
_atd_bad_json('number', x, context)
throw new Error('impossible')
}
}
function _atd_read_string(x: any, context: any): string {
if (typeof x === 'string')
return x
else {
_atd_bad_json('string', x, context)
throw new Error('impossible')
}
}
function _atd_read_required_field<T>(type_name: string,
field_name: string,
read_elt: (x: any, context: any) => T,
x: any,
context: any): T {
if (x === undefined) {
_atd_missing_json_field(type_name, field_name)
throw new Error('impossible')
}
else
return read_elt(x, context)
}
function _atd_read_optional_field<T>(read_elt: (x: any, context: any) => T,
x: any,
context: any): T {
if (x === undefined || x === null)
return x
else
return read_elt(x, context)
}
function _atd_read_field_with_default<T>(read_elt: (x: any, context: any) => T,
default_: T,
x: any,
context: any): T {
if (x === undefined || x === null)
return default_
else
return read_elt(x, context)
}
function _atd_read_option<T>(read_elt: (x: any, context: any) => T):
(x: any, context: any) => Option<T> {
function read_option(x: any, context: any): Option<T> {
if (x === 'None')
return null
else {
_atd_check_json_tuple(2, x, context);
switch (x[0]) {
case 'Some':
return { value: read_elt(x[1], context) }
default:
_atd_bad_json('option', x, context)
throw new Error('impossible')
}
}
}
return read_option
}
function _atd_read_nullable<T>(read_elt: (x: any, context: any) => T):
(x: any, context: any) => T | null {
function read_nullable(x: any, context: any): T | null {
if (x === null)
return null
else
return read_elt(x, context)
}
return read_nullable
}
function _atd_read_array<T>(read_elt: (x: any, context: any) => T):
(elts: any, context: any) => T[] {
function read_array(elts: any, context: any): T[] {
if (Array.isArray(elts))
return elts.map((x) => read_elt(x, elts))
else {
_atd_bad_json('array', elts, context)
throw new Error('impossible')
}
}
return read_array
}
function _atd_read_assoc_array_into_map<K, V>(
read_key: (key: any, context: any) => K,
read_value: (value: any, context: any) => V
): (x: any, context: any) => Map<K, V> {
function read_assoc(elts: any, context: any): Map<K, V> {
if (Array.isArray(elts)) {
const res = new Map<K, V>([])
for (const x of elts) {
if (Array.isArray(x) && x.length === 2)
res.set(read_key(x[0], x), read_value(x[1], x))
else {
_atd_bad_json('pair', x, elts)
throw new Error('impossible')
}
}
return res
}
else {
_atd_bad_json('array', elts, context)
throw new Error('impossible')
}
}
return read_assoc
}
function _atd_read_assoc_object_into_map<T>(
read_value: (value: any, context: any) => T
): (x: any, context: any) => Map<string, T> {
function read_assoc(elts: any, context: any): Map<string, T> {
if (typeof elts === 'object') {
const res = new Map<string, T>([])
for (const [key, value] of Object.entries(elts))
res.set(key, read_value(value, elts))
return res
}
else {
_atd_bad_json('object', elts, context)
throw new Error('impossible')
}
}
return read_assoc
}
function _atd_read_assoc_object_into_array<T>(
read_value: (value: any, context: any) => T
): (x: any, context: any) => [string, T][] {
function read_assoc(elts: any, context: any): [string, T][] {
if (typeof elts === 'object') {
const res: [string, T][] = []
for (const [key, value] of Object.entries(elts))
res.push([key, read_value(value, elts)])
return res
}
else {
_atd_bad_json('object', elts, context)
throw new Error('impossible')
}
}
return read_assoc
}
function _atd_write_unit(x: any, context: any) {
if (x === null)
return x
else {
_atd_bad_ts('null', x, context)
throw new Error('impossible')
}
}
function _atd_write_bool(x: any, context: any): boolean {
if (typeof x === 'boolean')
return x
else {
_atd_bad_ts('boolean', x, context)
throw new Error('impossible')
}
}
function _atd_write_int(x: any, context: any): number /*int*/ {
if (Number.isInteger(x))
return x
else {
_atd_bad_ts('integer', x, context)
throw new Error('impossible')
}
}
function _atd_write_float(x: any, context: any): number {
if (isFinite(x))
return x
else {
_atd_bad_ts('number', x, context)
throw new Error('impossible')
}
}
function _atd_write_string(x: any, context: any): string {
if (typeof x === 'string')
return x
else {
_atd_bad_ts('string', x, context)
throw new Error('impossible')
}
}
function _atd_write_option<T>(write_elt: (x: T, context: any) => any):
(elts: Option<T>, context: any) => any {
function write_option(x: Option<T>, context: any): any {
if (x === null)
return 'None'
else
return ['Some', write_elt(x.value, context)]
}
return write_option
}
function _atd_write_nullable<T>(write_elt: (x: T, context: any) => any):
(x: T | null, context: any) => any {
function write_option(x: T | null, context: any): any {
if (x === null)
return null
else
return write_elt(x, context)
}
return write_option
}
function _atd_write_array<T>(write_elt: (elt: T, context: any) => any):
(elts: T[], context: any) => any {
return ((elts: T[], context: any): any =>
elts.map((x) => write_elt(x, elts))
)
}
function _atd_write_assoc_map_to_array<K, V>(
write_key: (key: K, context: any) => any,
write_value: (value: V, context: any) => any
): (elts: Map<K, V>, context: any) => any {
function write_assoc(elts: Map<K, V>, context: any): any {
const res: any = []
elts.forEach((value: V, key: K) =>
res.push([write_key(key, elts), write_value(value, elts)])
)
return res
}
return write_assoc
}
function _atd_write_assoc_map_to_object<T>(
write_value: (value: T, context: any) => any
): (elts: Map<string, T>, context: any) => any {
function write_assoc(elts: Map<string, T>, context: any): any {
const res: any = {}
elts.forEach((value: T, key: string) =>
res[key] = write_value(value, elts)
)
return res
}
return write_assoc
}
function _atd_write_assoc_array_to_object<T>(
write_value: (value: T, context: any) => any
): (elts: [string, T][], context: any) => any {
function write_assoc(elts: [string, T][], context: any): any {
const res: any = {}
for (const [key, value] of elts)
res[key] = write_value(value, elts)
return res
}
return write_assoc
}
function _atd_write_required_field<T>(type_name: string,
field_name: string,
write_elt: (x: T, context: any) => any,
x: T,
context: any): any {
if (x === undefined) {
_atd_missing_ts_field(type_name, field_name)
throw new Error('impossible')
}
else
return write_elt(x, context)
}
function _atd_write_optional_field<T>(write_elt: (x: T, context: any) => any,
x: T | undefined,
context: any): any {
if (x === undefined || x === null)
return x
else
return write_elt(x, context)
}
function _atd_write_field_with_default<T>(
write_elt: (x: T, context: any) => any,
default_: T,
x: T,
context: any
): T {
const value = (x === undefined || x === null) ? default_ : x
return write_elt(value, context)
}