8
8
// of type 'Foo'.
9
9
10
10
11
+ export type Anything = any
12
+
11
13
export type DifferentKindsOfThings =
12
14
| { kind : 'Root' }
13
- | { kind : 'Thing' ; value : Int }
15
+ | { kind : 'Thing' ; value : number /*int*/ }
14
16
| { kind : 'WOW' /* JSON: "wow" */ }
15
17
| { kind : 'Amaze' /* JSON: "!!!" */ ; value : string [ ] }
16
18
17
- export type This = Int
19
+ export type This = number /*int*/
20
+
21
+ export type SimpleRecord = {
22
+ str : string ;
23
+ }
18
24
19
25
export type Root = {
20
26
id : string ;
21
27
this_ : This ;
22
- items : Int [ ] [ ] ;
23
- maybe ?: Int ;
24
- extras : Int [ ] ;
25
- answer : Int ;
28
+ items : number /*int*/ [ ] [ ] ;
29
+ maybe ?: number /*int*/ ;
30
+ maybe2 ?: SimpleRecord ;
31
+ extras : number /*int*/ [ ] ;
32
+ answer : number /*int*/ ;
26
33
aliased : Alias ;
27
34
point : [ number , number ] ;
28
35
kinds : DifferentKindsOfThings [ ] ;
29
- assoc1 : [ number , Int ] [ ] ;
30
- assoc2 : [ string , Int ] [ ] ;
31
- assoc3 : Map < number , Int > ;
32
- assoc4 : Map < string , Int > ;
33
- options : Option < Int > [ ] ;
34
- nullables : ( Int | null ) [ ] ;
36
+ assoc1 : [ number , number /*int*/ ] [ ] ;
37
+ assoc2 : [ string , number /*int*/ ] [ ] ;
38
+ assoc3 : Map < number , number /*int*/ > ;
39
+ assoc4 : Map < string , number /*int*/ > ;
40
+ options : Option < number /*int*/ > [ ] ;
41
+ nullables : ( number /*int*/ | null ) [ ] ;
35
42
untyped_things : any [ ] ;
36
43
foo : ( Foo | null ) ;
37
44
parametrized_record : IntFloatParametrizedRecord ;
38
45
parametrized_tuple : TupleE1a4b40 ;
46
+ anything : Anything ;
39
47
}
40
48
41
- export type Alias = Int [ ]
49
+ export type Alias = number /*int*/ [ ]
42
50
43
- export type Pair = [ string , Int ]
51
+ export type Pair = [ string , number /*int*/ ]
44
52
45
53
export type Foo = {
46
54
foo : string ;
47
55
}
48
56
49
57
export type IntFloatParametrizedRecord = {
50
- field_a : Int ;
58
+ field_a : number /*int*/ ;
51
59
field_b : number [ ] ;
52
60
}
53
61
54
- export type TupleE1a4b40 = [ DifferentKindsOfThings , DifferentKindsOfThings , Int ]
62
+ export type TupleE1a4b40 = [ DifferentKindsOfThings , DifferentKindsOfThings , number /*int*/ ]
63
+
64
+ export function writeAnything ( x : Anything , context : any = x ) : any {
65
+ return ( ( x : any , context ) : any => x ) ( x , context ) ;
66
+ }
67
+
68
+ export function readAnything ( x : any , context : any = x ) : Anything {
69
+ return ( ( x : any , context ) : any => x ) ( x , context ) ;
70
+ }
55
71
56
72
export function writeDifferentKindsOfThings ( x : DifferentKindsOfThings , context : any = x ) : any {
57
73
switch ( x . kind ) {
@@ -100,12 +116,25 @@ export function readThis(x: any, context: any = x): This {
100
116
return _atd_read_int ( x , context ) ;
101
117
}
102
118
119
+ export function writeSimpleRecord ( x : SimpleRecord , context : any = x ) : any {
120
+ return {
121
+ 'str' : _atd_write_required_field ( 'SimpleRecord' , 'str' , _atd_write_string , x . str , x ) ,
122
+ } ;
123
+ }
124
+
125
+ export function readSimpleRecord ( x : any , context : any = x ) : SimpleRecord {
126
+ return {
127
+ str : _atd_read_required_field ( 'SimpleRecord' , 'str' , _atd_read_string , x [ 'str' ] , x ) ,
128
+ } ;
129
+ }
130
+
103
131
export function writeRoot ( x : Root , context : any = x ) : any {
104
132
return {
105
133
'ID' : _atd_write_required_field ( 'Root' , 'id' , _atd_write_string , x . id , x ) ,
106
134
'this' : _atd_write_required_field ( 'Root' , 'this' , writeThis , x . this_ , x ) ,
107
135
'items' : _atd_write_required_field ( 'Root' , 'items' , _atd_write_array ( _atd_write_array ( _atd_write_int ) ) , x . items , x ) ,
108
136
'maybe' : _atd_write_optional_field ( _atd_write_int , x . maybe , x ) ,
137
+ 'maybe2' : _atd_write_optional_field ( writeSimpleRecord , x . maybe2 , x ) ,
109
138
'extras' : _atd_write_field_with_default ( _atd_write_array ( _atd_write_int ) , [ ] , x . extras , x ) ,
110
139
'answer' : _atd_write_field_with_default ( _atd_write_int , 42 , x . answer , x ) ,
111
140
'aliased' : _atd_write_required_field ( 'Root' , 'aliased' , writeAlias , x . aliased , x ) ,
@@ -117,10 +146,11 @@ export function writeRoot(x: Root, context: any = x): any {
117
146
'assoc4' : _atd_write_required_field ( 'Root' , 'assoc4' , _atd_write_assoc_map_to_object ( _atd_write_int ) , x . assoc4 , x ) ,
118
147
'options' : _atd_write_field_with_default ( _atd_write_array ( _atd_write_option ( _atd_write_int ) ) , [ ] , x . options , x ) ,
119
148
'nullables' : _atd_write_field_with_default ( _atd_write_array ( _atd_write_nullable ( _atd_write_int ) ) , [ ] , x . nullables , x ) ,
120
- 'untyped_things' : _atd_write_required_field ( 'Root' , 'untyped_things' , _atd_write_array ( ( ( x : any ) : any => x ) ) , x . untyped_things , x ) ,
149
+ 'untyped_things' : _atd_write_required_field ( 'Root' , 'untyped_things' , _atd_write_array ( ( ( x : any , context ) : any => x ) ) , x . untyped_things , x ) ,
121
150
'foo' : _atd_write_required_field ( 'Root' , 'foo' , _atd_write_nullable ( writeFoo ) , x . foo , x ) ,
122
151
'parametrized_record' : _atd_write_required_field ( 'Root' , 'parametrized_record' , writeIntFloatParametrizedRecord , x . parametrized_record , x ) ,
123
152
'parametrized_tuple' : _atd_write_required_field ( 'Root' , 'parametrized_tuple' , writeTupleE1a4b40 , x . parametrized_tuple , x ) ,
153
+ 'anything' : _atd_write_required_field ( 'Root' , 'anything' , writeAnything , x . anything , x ) ,
124
154
} ;
125
155
}
126
156
@@ -130,21 +160,23 @@ export function readRoot(x: any, context: any = x): Root {
130
160
this_ : _atd_read_required_field ( 'Root' , 'this' , readThis , x [ 'this' ] , x ) ,
131
161
items : _atd_read_required_field ( 'Root' , 'items' , _atd_read_array ( _atd_read_array ( _atd_read_int ) ) , x [ 'items' ] , x ) ,
132
162
maybe : _atd_read_optional_field ( _atd_read_int , x [ 'maybe' ] , x ) ,
163
+ maybe2 : _atd_read_optional_field ( readSimpleRecord , x [ 'maybe2' ] , x ) ,
133
164
extras : _atd_read_field_with_default ( _atd_read_array ( _atd_read_int ) , [ ] , x [ 'extras' ] , x ) ,
134
165
answer : _atd_read_field_with_default ( _atd_read_int , 42 , x [ 'answer' ] , x ) ,
135
166
aliased : _atd_read_required_field ( 'Root' , 'aliased' , readAlias , x [ 'aliased' ] , x ) ,
136
167
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 ) ,
137
168
kinds : _atd_read_required_field ( 'Root' , 'kinds' , _atd_read_array ( readDifferentKindsOfThings ) , x [ 'kinds' ] , x ) ,
138
- assoc1 : _atd_read_required_field ( 'Root' , 'assoc1' , _atd_read_array ( ( ( x , context ) : [ 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 ) ,
169
+ 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 ) ,
139
170
assoc2 : _atd_read_required_field ( 'Root' , 'assoc2' , _atd_read_assoc_object_into_array ( _atd_read_int ) , x [ 'assoc2' ] , x ) ,
140
171
assoc3 : _atd_read_required_field ( 'Root' , 'assoc3' , _atd_read_assoc_array_into_map ( _atd_read_float , _atd_read_int ) , x [ 'assoc3' ] , x ) ,
141
172
assoc4 : _atd_read_required_field ( 'Root' , 'assoc4' , _atd_read_assoc_object_into_map ( _atd_read_int ) , x [ 'assoc4' ] , x ) ,
142
173
options : _atd_read_field_with_default ( _atd_read_array ( _atd_read_option ( _atd_read_int ) ) , [ ] , x [ 'options' ] , x ) ,
143
174
nullables : _atd_read_field_with_default ( _atd_read_array ( _atd_read_nullable ( _atd_read_int ) ) , [ ] , x [ 'nullables' ] , x ) ,
144
- untyped_things : _atd_read_required_field ( 'Root' , 'untyped_things' , _atd_read_array ( ( ( x : any ) : any => x ) ) , x [ 'untyped_things' ] , x ) ,
175
+ untyped_things : _atd_read_required_field ( 'Root' , 'untyped_things' , _atd_read_array ( ( ( x : any , context ) : any => x ) ) , x [ 'untyped_things' ] , x ) ,
145
176
foo : _atd_read_required_field ( 'Root' , 'foo' , _atd_read_nullable ( readFoo ) , x [ 'foo' ] , x ) ,
146
177
parametrized_record : _atd_read_required_field ( 'Root' , 'parametrized_record' , readIntFloatParametrizedRecord , x [ 'parametrized_record' ] , x ) ,
147
178
parametrized_tuple : _atd_read_required_field ( 'Root' , 'parametrized_tuple' , readTupleE1a4b40 , x [ 'parametrized_tuple' ] , x ) ,
179
+ anything : _atd_read_required_field ( 'Root' , 'anything' , readAnything , x [ 'anything' ] , x ) ,
148
180
} ;
149
181
}
150
182
@@ -161,7 +193,7 @@ export function writePair(x: Pair, context: any = x): any {
161
193
}
162
194
163
195
export function readPair ( x : any , context : any = x ) : Pair {
164
- return ( ( x , context ) : [ string , Int ] => { _atd_check_json_tuple ( 2 , x , context ) ; return [ _atd_read_string ( x [ 0 ] , x ) , _atd_read_int ( x [ 1 ] , x ) ] } ) ( x , context ) ;
196
+ 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 ) ;
165
197
}
166
198
167
199
export function writeFoo ( x : Foo , context : any = x ) : any {
@@ -195,16 +227,14 @@ export function writeTupleE1a4b40(x: TupleE1a4b40, context: any = x): any {
195
227
}
196
228
197
229
export function readTupleE1a4b40 ( x : any , context : any = x ) : TupleE1a4b40 {
198
- return ( ( x , context ) : [ DifferentKindsOfThings , DifferentKindsOfThings , 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 ) ;
230
+ 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 ) ;
199
231
}
200
232
201
233
202
234
/////////////////////////////////////////////////////////////////////
203
235
// Runtime library
204
236
/////////////////////////////////////////////////////////////////////
205
237
206
- export type Int = number
207
-
208
238
export type Option < T > = null | { value : T }
209
239
210
240
function _atd_missing_json_field ( type_name : string , json_field_name : string ) {
@@ -237,7 +267,7 @@ function _atd_bad_ts(expected_type: string, ts_value: any, context: any) {
237
267
` Occurs in '${ JSON . stringify ( context ) } '.` )
238
268
}
239
269
240
- function _atd_check_json_tuple ( len : Int , x : any , context : any ) {
270
+ function _atd_check_json_tuple ( len : number /*int*/ , x : any , context : any ) {
241
271
if ( ! Array . isArray ( x ) || x . length !== len )
242
272
_atd_bad_json ( 'tuple of length ' + len , x , context ) ;
243
273
}
@@ -260,7 +290,7 @@ function _atd_read_bool(x: any, context: any): boolean {
260
290
}
261
291
}
262
292
263
- function _atd_read_int ( x : any , context : any ) : Int {
293
+ function _atd_read_int ( x : any , context : any ) : number /*int*/ {
264
294
if ( Number . isInteger ( x ) )
265
295
return x
266
296
else {
@@ -441,7 +471,7 @@ function _atd_write_bool(x: any, context: any): boolean {
441
471
}
442
472
}
443
473
444
- function _atd_write_int ( x : any , context : any ) : Int {
474
+ function _atd_write_int ( x : any , context : any ) : number /*int*/ {
445
475
if ( Number . isInteger ( x ) )
446
476
return x
447
477
else {
@@ -550,7 +580,7 @@ function _atd_write_required_field<T>(type_name: string,
550
580
}
551
581
552
582
function _atd_write_optional_field < T > ( write_elt : ( x : T , context : any ) => any ,
553
- x : T ,
583
+ x : T | undefined ,
554
584
context : any ) : any {
555
585
if ( x === undefined || x === null )
556
586
return x
0 commit comments