-
-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathgenerator.ts
985 lines (905 loc) · 32.6 KB
/
generator.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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
/* eslint-disable no-cond-assign */
// noinspection JSAssignmentUsedAsCondition
import { YTNode } from './helpers.js';
import * as Parser from './parser.js';
import { InnertubeError } from '../utils/Utils.js';
import Author from './classes/misc/Author.js';
import Text from './classes/misc/Text.js';
import Thumbnail from './classes/misc/Thumbnail.js';
import NavigationEndpoint from './classes/NavigationEndpoint.js';
import type { YTNodeConstructor } from './helpers.js';
export type MiscInferenceType = {
type: 'misc',
misc_type: 'NavigationEndpoint',
optional: boolean,
endpoint: NavigationEndpoint
} | {
type: 'misc',
misc_type: 'Text',
optional: boolean,
text: string,
endpoint?: NavigationEndpoint
} | {
type: 'misc',
misc_type: 'Thumbnail',
optional: boolean,
} | {
type: 'misc',
misc_type: 'Author',
optional: boolean,
params: [string, string?],
}
export interface ObjectInferenceType {
type: 'object',
keys: KeyInfo,
optional: boolean,
}
export interface RendererInferenceType {
type: 'renderer',
renderers: string[],
optional: boolean
}
export interface PrimitiveInferenceType {
type: 'primitive',
typeof: ('string' | 'number' | 'boolean' | 'bigint' | 'symbol' | 'undefined' | 'function' | 'never' | 'unknown')[],
optional: boolean,
}
export type ArrayInferenceType = {
type: 'array',
array_type: 'primitive',
items: PrimitiveInferenceType,
optional: boolean,
} | {
type: 'array',
array_type: 'object',
items: ObjectInferenceType,
optional: boolean,
} | {
type: 'array',
array_type: 'renderer',
renderers: string[],
optional: boolean,
};
export type InferenceType = RendererInferenceType | MiscInferenceType | ObjectInferenceType | PrimitiveInferenceType | ArrayInferenceType;
export type KeyInfo = (readonly [string, InferenceType])[];
const IGNORED_KEYS = new Set([
'trackingParams', 'accessibility', 'accessibilityData'
]);
const RENDERER_EXAMPLES: Record<string, unknown> = {};
export function camelToSnake(str: string) {
return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
}
/**
* Infer the type of key given its value
* @param key - The key to infer the type of
* @param value - The value of the key
* @returns The inferred type
*/
export function inferType(key: string, value: unknown): InferenceType {
let return_value: string | Record<string, any> | false | MiscInferenceType | ArrayInferenceType = false;
if (typeof value === 'object' && value != null) {
if (return_value = isRenderer(value)) {
RENDERER_EXAMPLES[return_value] = Reflect.get(value, Reflect.ownKeys(value)[0]);
return {
type: 'renderer',
renderers: [ return_value ],
optional: false
};
}
if (return_value = isRendererList(value)) {
for (const [ key, value ] of Object.entries(return_value)) {
RENDERER_EXAMPLES[key] = value;
}
return {
type: 'array',
array_type: 'renderer',
renderers: Object.keys(return_value),
optional: false
};
}
if (return_value = isMiscType(key, value)) {
return return_value as MiscInferenceType;
}
if (return_value = isArrayType(value)) {
return return_value as ArrayInferenceType;
}
}
const primitive_type = typeof value;
if (primitive_type === 'object')
return {
type: 'object',
keys: Object.entries(value as object).map(([ key, value ]) => [ key, inferType(key, value) ]),
optional: false
};
return {
type: 'primitive',
typeof: [ primitive_type ],
optional: false
};
}
/**
* Checks if the given value is an array of renderers
* @param value - The value to check
* @returns If it is a renderer list, return an object with keys being the classnames, and values being an example of that class.
* Otherwise, return false.
*/
export function isRendererList(value: unknown) {
const arr = Array.isArray(value);
if (arr && value.length === 0)
return false;
const is_list = arr && value.every((item) => isRenderer(item));
return (
is_list ?
Object.fromEntries(value.map((item) => {
const key = Reflect.ownKeys(item)[0].toString();
return [ Parser.sanitizeClassName(key), item[key] ];
})) :
false
);
}
/**
* Check if the given value is a misc type.
* @param key - The key of the value
* @param value - The value to check
* @returns If it is a misc type, return the InferenceType. Otherwise, return false.
*/
export function isMiscType(key: string, value: unknown): MiscInferenceType | false {
if (typeof value === 'object' && value !== null) {
// NavigationEndpoint
if (key.endsWith('Endpoint') || key.endsWith('Command') || key === 'endpoint') {
return {
type: 'misc',
endpoint: new NavigationEndpoint(value),
optional: false,
misc_type: 'NavigationEndpoint'
};
}
// Text
if (Reflect.has(value, 'simpleText') || Reflect.has(value, 'runs')) {
const textNode = new Text(value);
return {
type: 'misc',
misc_type: 'Text',
optional: false,
endpoint: textNode.endpoint,
text: textNode.toString()
};
}
// Thumbnail
if (Reflect.has(value, 'thumbnails') && Array.isArray(Reflect.get(value, 'thumbnails'))) {
return {
type: 'misc',
misc_type: 'Thumbnail',
optional: false
};
}
}
return false;
}
/**
* Check if the given value is a renderer
* @param value - The value to check
* @returns If it is a renderer, return the class name. Otherwise, return false.
*/
export function isRenderer(value: unknown) {
const is_object = typeof value === 'object';
if (!is_object) return false;
const keys = Reflect.ownKeys(value as object);
if (keys.length === 1) {
const first_key = keys[0].toString();
if (first_key.endsWith('Renderer') || first_key.endsWith('Model')) {
return Parser.sanitizeClassName(first_key);
}
}
return false;
}
/**
* Checks if the given value is an array
* @param value - The value to check
* @returns If it is an array, return the InferenceType. Otherwise, return false.
*/
export function isArrayType(value: unknown): false | ArrayInferenceType {
if (!Array.isArray(value))
return false;
// If the array is empty, we can't infer anything
if (value.length === 0)
return {
type: 'array',
array_type: 'primitive',
items: {
type: 'primitive',
typeof: [ 'never' ],
optional: false
},
optional: false
};
// We'll infer the primitive type of the array entries
const array_entry_types = value.map((item) => typeof item);
// We only support arrays that have the same primitive type throughout
const all_same_type = array_entry_types.every((type) => type === array_entry_types[0]);
if (!all_same_type)
return {
type: 'array',
array_type: 'primitive',
items: {
type: 'primitive',
typeof: [ 'unknown' ],
optional: false
},
optional: false
};
const type = array_entry_types[0];
if (type !== 'object')
return {
type: 'array',
array_type: 'primitive',
items: {
type: 'primitive',
typeof: [ type ],
optional: false
},
optional: false
};
let key_type: KeyInfo = [];
for (let i = 0; i < value.length; i++) {
const current_keys = Object.entries(value[i] as object).map(([ key, value ]) => [ key, inferType(key, value) ] as const);
if (i === 0) {
key_type = current_keys;
continue;
}
key_type = mergeKeyInfo(key_type, current_keys).resolved_key_info;
}
return {
type: 'array',
array_type: 'object',
items: {
type: 'object',
keys: key_type,
optional: false
},
optional: false
};
}
function introspectKeysFirstPass(classdata: unknown): KeyInfo {
if (typeof classdata !== 'object' || classdata === null) {
throw new InnertubeError('Generator: Cannot introspect non-object', {
classdata
});
}
const keys = Reflect.ownKeys(classdata)
.filter((key) => !isIgnoredKey(key))
.filter((key): key is string => typeof key === 'string');
return keys.map((key) => {
const value = Reflect.get(classdata, key) as unknown;
const inferred_type = inferType(key, value);
return [ key, inferred_type ] as const;
});
}
function introspectKeysSecondPass(key_info: KeyInfo) {
// The second pass will detect Author
const channel_nav = key_info.filter(([ , value ]) => {
if (value.type !== 'misc') return false;
if (!(value.misc_type === 'NavigationEndpoint' || value.misc_type === 'Text')) return false;
return value.endpoint?.metadata.page_type === 'WEB_PAGE_TYPE_CHANNEL';
});
// Whichever one has the longest text is the most probable match
const most_probable_match = channel_nav.sort(([ , a ], [ , b ]) => {
if (a.type !== 'misc' || b.type !== 'misc') return 0;
if (a.misc_type !== 'Text' || b.misc_type !== 'Text') return 0;
return b.text.length - a.text.length;
});
const excluded_keys = new Set<string>();
const canonical_channel_nave = most_probable_match[0];
let author: MiscInferenceType | undefined;
// We've found an author
if (canonical_channel_nave) {
excluded_keys.add(canonical_channel_nave[0]);
// Now to locate its metadata
// We'll first get all the keys in the classdata
const keys = key_info.map(([ key ]) => key);
// Check for anything ending in 'Badges' equals 'badges'
const badges = keys.filter((key) => key.endsWith('Badges') || key === 'badges');
// The likely candidate is the one with some prefix (owner, author)
const likely_badges = badges.filter((key) => key.startsWith('owner') || key.startsWith('author'));
// If we have a likely candidate, we'll use that
const canonical_badges = likely_badges[0] ?? badges[0];
// Now we have the author and its badges
// Verify that its actually badges
const badge_key_info = key_info.find(([ key ]) => key === canonical_badges);
const is_badges = badge_key_info ?
badge_key_info[1].type === 'array' && badge_key_info[1].array_type === 'renderer' && Reflect.has(badge_key_info[1].renderers, 'MetadataBadge') :
false;
if (is_badges && canonical_badges) excluded_keys.add(canonical_badges);
// TODO: next we check for the author's thumbnail
author = {
type: 'misc',
misc_type: 'Author',
optional: false,
params: [
canonical_channel_nave[0],
is_badges ? canonical_badges : undefined
]
};
}
if (author) {
key_info.push([ 'author', author ]);
}
return key_info.filter(([ key ]) => !excluded_keys.has(key));
}
function introspect2(classdata: unknown) {
const key_info = introspectKeysFirstPass(classdata);
return introspectKeysSecondPass(key_info);
}
/**
* Introspect an example of a class in order to determine its key info and dependencies
* @param classdata - The example of the class
* @returns The key info and any unimplemented dependencies
*/
export function introspect(classdata: unknown) {
const key_info = introspect2(classdata);
const dependencies = new Map<string, any>();
for (const [ , value ] of key_info) {
if (value.type === 'renderer' || (value.type === 'array' && value.array_type === 'renderer'))
for (const renderer of value.renderers) {
const example = RENDERER_EXAMPLES[renderer];
if (example)
dependencies.set(renderer, example);
}
}
const unimplemented_dependencies = Array.from(dependencies).filter(([ classname ]) => !Parser.hasParser(classname));
return {
key_info,
unimplemented_dependencies
};
}
/**
* Is this key ignored by the parser?
* @param key - The key to check
* @returns Whether or not the key is ignored
*/
export function isIgnoredKey(key: string | symbol) {
return typeof key === 'string' && IGNORED_KEYS.has(key);
}
/**
* Given a classname and its resolved key info, create a new class
* @param classname - The name of the class
* @param key_info - The resolved key info
* @param logger - The logger to log errors to
* @returns Class based on the key info extending YTNode
*/
export function createRuntimeClass(classname: string, key_info: KeyInfo, logger: Parser.ParserErrorHandler): YTNodeConstructor {
logger({
error_type: 'class_not_found',
classname,
key_info
});
const node = class extends YTNode {
static type = classname;
static #key_info = new Map<string, InferenceType>();
static set key_info(key_info: KeyInfo) {
this.#key_info = new Map(key_info);
}
static get key_info() {
return [ ...this.#key_info.entries() ];
}
constructor(data: unknown) {
super();
const {
key_info,
unimplemented_dependencies
} = introspect(data);
const {
resolved_key_info,
changed_keys
} = mergeKeyInfo(node.key_info, key_info);
const did_change = changed_keys.length > 0;
if (did_change) {
node.key_info = resolved_key_info;
logger({
error_type: 'class_changed',
classname,
key_info: node.key_info,
changed_keys
});
}
for (const [ name, data ] of unimplemented_dependencies)
generateRuntimeClass(name, data, logger);
for (const [ key, value ] of key_info) {
let snake_key = camelToSnake(key);
if (value.type === 'misc' && value.misc_type === 'NavigationEndpoint')
snake_key = 'endpoint';
Reflect.set(this, snake_key, parse(key, value, data));
}
}
};
node.key_info = key_info;
Object.defineProperty(node, 'name', { value: classname, writable: false });
return node;
}
/**
* Given example data for a class, introspect, implement dependencies, and create a new class
* @param classname - The name of the class
* @param classdata - The example of the class
* @param logger - The logger to log errors to
* @returns Class based on the example classdata extending YTNode
*/
export function generateRuntimeClass(classname: string, classdata: unknown, logger: Parser.ParserErrorHandler) {
const {
key_info,
unimplemented_dependencies
} = introspect(classdata);
const JITNode = createRuntimeClass(classname, key_info, logger);
Parser.addRuntimeParser(classname, JITNode);
for (const [ name, data ] of unimplemented_dependencies)
generateRuntimeClass(name, data, logger);
return JITNode;
}
/**
* Generate a typescript class based on the key info
* @param classname - The name of the class
* @param key_info - The key info, as returned by {@link introspect}
* @returns Typescript class file
*/
export function generateTypescriptClass(classname: string, key_info: KeyInfo) {
const props: string[] = [];
const constructor_lines = [
'super();'
];
for (const [ key, value ] of key_info) {
let snake_key = camelToSnake(key);
if (value.type === 'misc' && value.misc_type === 'NavigationEndpoint')
snake_key = 'endpoint';
props.push(`${snake_key}${value.optional ? '?' : ''}: ${toTypeDeclaration(value)};`);
constructor_lines.push(`this.${snake_key} = ${toParser(key, value)};`);
}
return `class ${classname} extends YTNode {\n static type = '${classname}';\n\n ${props.join('\n ')}\n\n constructor(data: RawNode) {\n ${constructor_lines.join('\n ')}\n }\n}\n`;
}
function toTypeDeclarationObject(indentation: number, keys: KeyInfo) {
return `{\n${keys.map(([ key, value ]) => `${' '.repeat((indentation + 2) * 2)}${camelToSnake(key)}${value.optional ? '?' : ''}: ${toTypeDeclaration(value, indentation + 1)}`).join(',\n')}\n${' '.repeat((indentation + 1) * 2)}}`;
}
/**
* For a given inference type, get the typescript type declaration
* @param inference_type - The inference type to get the declaration for
* @param indentation - The indentation level (used for objects)
* @returns Typescript type declaration
*/
export function toTypeDeclaration(inference_type: InferenceType, indentation = 0): string {
switch (inference_type.type) {
case 'renderer': {
return `${inference_type.renderers.map((type) => `YTNodes.${type}`).join(' | ')} | null`;
}
case 'array': {
switch (inference_type.array_type) {
case 'renderer':
return `ObservedArray<${inference_type.renderers.map((type) => `YTNodes.${type}`).join(' | ')}> | null`;
case 'primitive':
{
const items_list = inference_type.items.typeof;
if (inference_type.items.optional && !items_list.includes('undefined'))
items_list.push('undefined');
const items =
items_list.length === 1 ?
`${items_list[0]}` : `(${items_list.join(' | ')})`;
return `${items}[]`;
}
case 'object':
return `${toTypeDeclarationObject(indentation, inference_type.items.keys)}[]`;
default:
throw new Error('Unreachable code reached! Switch missing case!');
}
}
case 'object': {
return toTypeDeclarationObject(indentation, inference_type.keys);
}
case 'misc': {
switch (inference_type.misc_type) {
case 'Thumbnail':
return 'Thumbnail[]';
default:
return inference_type.misc_type;
}
}
case 'primitive': {
return inference_type.typeof.join(' | ');
}
}
}
function toParserObject(indentation: number, keys: KeyInfo, key_path: string[], key: string) {
const new_keypath = [ ...key_path, key ];
return `{\n${keys.map(([ key, value ]) => `${' '.repeat((indentation + 2) * 2)}${camelToSnake(key)}: ${toParser(key, value, new_keypath, indentation + 1)}`).join(',\n')}\n${' '.repeat((indentation + 1) * 2)}}`;
}
/**
* Generate statements to parse a given inference type
* @param key - The key to parse
* @param inference_type - The inference type to parse
* @param key_path - The path to the key (excluding the key itself)
* @param indentation - The indentation level (used for objects)
* @returns Statement to parse the given key
*/
export function toParser(key: string, inference_type: InferenceType, key_path: string[] = [ 'data' ], indentation = 1) {
let parser = 'undefined';
switch (inference_type.type) {
case 'renderer':
{
parser = `Parser.parseItem(${key_path.join('.')}.${key}, ${toParserValidTypes(inference_type.renderers)})`;
}
break;
case 'array':
{
switch (inference_type.array_type) {
case 'renderer':
parser = `Parser.parse(${key_path.join('.')}.${key}, true, ${toParserValidTypes(inference_type.renderers)})`;
break;
case 'object':
parser = `${key_path.join('.')}.${key}.map((item: any) => (${toParserObject(indentation, inference_type.items.keys, [], 'item')}))`;
break;
case 'primitive':
parser = `${key_path.join('.')}.${key}`;
break;
default:
throw new Error('Unreachable code reached! Switch missing case!');
}
}
break;
case 'object':
{
parser = toParserObject(indentation, inference_type.keys, key_path, key);
}
break;
case 'misc':
switch (inference_type.misc_type) {
case 'Thumbnail':
parser = `Thumbnail.fromResponse(${key_path.join('.')}.${key})`;
break;
case 'Author':
{
const author_parser = `new Author(${key_path.join('.')}.${inference_type.params[0]}, ${inference_type.params[1] ? `${key_path.join('.')}.${inference_type.params[1]}` : 'undefined'})`;
if (inference_type.optional)
return `Reflect.has(${key_path.join('.')}, '${inference_type.params[0]}') ? ${author_parser} : undefined`;
return author_parser;
}
default:
parser = `new ${inference_type.misc_type}(${key_path.join('.')}.${key})`;
break;
}
if (parser === 'undefined')
throw new Error('Unreachable code reached! Switch missing case!');
break;
case 'primitive':
parser = `${key_path.join('.')}.${key}`;
break;
}
if (inference_type.optional)
return `Reflect.has(${key_path.join('.')}, '${key}') ? ${parser} : undefined`;
return parser;
}
function toParserValidTypes(types: string[]) {
if (types.length === 1) {
return `YTNodes.${types[0]}`;
}
return `[ ${types.map((type) => `YTNodes.${type}`).join(', ')} ]`;
}
function accessDataFromKeyPath(root: any, key_path: string[]) {
let data = root;
for (const key of key_path)
data = data[key];
return data;
}
function hasDataFromKeyPath(root: any, key_path: string[]) {
let data = root;
for (const key of key_path)
if (!Reflect.has(data, key))
return false;
else
data = data[key];
return true;
}
function parseObject(key: string, data: unknown, key_path: string[], keys: KeyInfo, should_optional: boolean) {
const obj: any = {};
const new_key_path = [ ...key_path, key ];
for (const [ key, value ] of keys) {
obj[key] = should_optional ? parse(key, value, data, new_key_path) : undefined;
}
return obj;
}
/**
* Parse a value from a given key path using the given inference type
* @param key - The key to parse
* @param inference_type - The inference type to parse
* @param data - The data to parse from
* @param key_path - The path to the key (excluding the key itself)
* @returns The parsed value
*/
export function parse(key: string, inference_type: InferenceType, data: unknown, key_path: string[] = [ 'data' ]) {
const should_optional = !inference_type.optional || hasDataFromKeyPath({ data }, [ ...key_path, key ]);
switch (inference_type.type) {
case 'renderer': {
return should_optional ? Parser.parseItem(accessDataFromKeyPath({ data }, [ ...key_path, key ]), inference_type.renderers.map((type) => Parser.getParserByName(type))) : undefined;
}
case 'array': {
switch (inference_type.array_type) {
case 'renderer':
return should_optional ? Parser.parse(accessDataFromKeyPath({ data }, [ ...key_path, key ]), true, inference_type.renderers.map((type) => Parser.getParserByName(type))) : undefined;
case 'object':
return should_optional ? accessDataFromKeyPath({ data }, [ ...key_path, key ]).map((_: any, idx: number) => {
return parseObject(`${idx}`, data, [ ...key_path, key ], inference_type.items.keys, should_optional);
}) : undefined;
case 'primitive':
return should_optional ? accessDataFromKeyPath({ data }, [ ...key_path, key ]) : undefined;
default:
throw new Error('Unreachable code reached! Switch missing case!');
}
}
case 'object': {
return parseObject(key, data, key_path, inference_type.keys, should_optional);
}
case 'misc':
switch (inference_type.misc_type) {
case 'NavigationEndpoint':
return should_optional ? new NavigationEndpoint(accessDataFromKeyPath({ data }, [ ...key_path, key ])) : undefined;
case 'Text':
return should_optional ? new Text(accessDataFromKeyPath({ data }, [ ...key_path, key ])) : undefined;
case 'Thumbnail':
return should_optional ? Thumbnail.fromResponse(accessDataFromKeyPath({ data }, [ ...key_path, key ])) : undefined;
case 'Author': {
const author_should_optional = !inference_type.optional || hasDataFromKeyPath({ data }, [ ...key_path, inference_type.params[0] ]);
return author_should_optional ? new Author(
accessDataFromKeyPath({ data }, [ ...key_path, inference_type.params[0] ]),
inference_type.params[1] ?
accessDataFromKeyPath({ data }, [ ...key_path, inference_type.params[1] ]) : undefined
) : undefined;
}
default:
throw new Error('Unreachable code reached! Switch missing case!');
}
case 'primitive':
return accessDataFromKeyPath({ data }, [ ...key_path, key ]);
}
}
/**
* Merges two sets of key info, resolving any conflicts
* @param key_info - The current key info
* @param new_key_info - The new key info
* @returns The merged key info
*/
export function mergeKeyInfo(key_info: KeyInfo, new_key_info: KeyInfo) {
const changed_keys = new Map<string, InferenceType>();
const current_keys = new Set(key_info.map(([ key ]) => key));
const new_keys = new Set(new_key_info.map(([ key ]) => key));
const added_keys = new_key_info.filter(([ key ]) => !current_keys.has(key));
const removed_keys = key_info.filter(([ key ]) => !new_keys.has(key));
const common_keys = key_info.filter(([ key ]) => new_keys.has(key));
const new_key_map = new Map(new_key_info);
for (const [ key, type ] of common_keys) {
const new_type = new_key_map.get(key);
if (!new_type) continue;
if (type.type !== new_type.type) {
// We've got a type mismatch, this is unknown, we do not resolve unions
changed_keys.set(key, {
type: 'primitive',
typeof: [ 'unknown' ],
optional: true
});
continue;
}
// We've got the same type, so we can now resolve the changes
switch (type.type) {
case 'object':
{
if (new_type.type !== 'object') continue;
const { resolved_key_info } = mergeKeyInfo(type.keys, new_type.keys);
const resolved_key: InferenceType = {
type: 'object',
keys: resolved_key_info,
optional: type.optional || new_type.optional
};
const did_change = JSON.stringify(resolved_key) !== JSON.stringify(type);
if (did_change) changed_keys.set(key, resolved_key);
}
break;
case 'renderer':
{
if (new_type.type !== 'renderer') continue;
const union_map = {
...type.renderers,
...new_type.renderers
};
const either_optional = type.optional || new_type.optional;
const resolved_key: InferenceType = {
type: 'renderer',
renderers: union_map,
optional: either_optional
};
const did_change = JSON.stringify({
...resolved_key,
renderers: Object.keys(resolved_key.renderers)
}) !== JSON.stringify({
...type,
renderers: Object.keys(type.renderers)
});
if (did_change) changed_keys.set(key, resolved_key);
}
break;
case 'array': {
if (new_type.type !== 'array') continue;
switch (type.array_type) {
case 'renderer':
{
if (new_type.array_type !== 'renderer') {
// Type mismatch
changed_keys.set(key, {
type: 'array',
array_type: 'primitive',
items: {
type: 'primitive',
typeof: [ 'unknown' ],
optional: true
},
optional: true
});
continue;
}
const union_map = {
...type.renderers,
...new_type.renderers
};
const either_optional = type.optional || new_type.optional;
const resolved_key: InferenceType = {
type: 'array',
array_type: 'renderer',
renderers: union_map,
optional: either_optional
};
const did_change = JSON.stringify({
...resolved_key,
renderers: Object.keys(resolved_key.renderers)
}) !== JSON.stringify({
...type,
renderers: Object.keys(type.renderers)
});
if (did_change) changed_keys.set(key, resolved_key);
}
break;
case 'object':
{
if (new_type.array_type === 'primitive' && new_type.items.typeof.length == 1 && new_type.items.typeof[0] === 'never') {
// It's an empty array. We assume the type is unchanged
continue;
}
if (new_type.array_type !== 'object') {
// Type mismatch
changed_keys.set(key, {
type: 'array',
array_type: 'primitive',
items: {
type: 'primitive',
typeof: [ 'unknown' ],
optional: true
},
optional: true
});
continue;
}
const { resolved_key_info } = mergeKeyInfo(type.items.keys, new_type.items.keys);
const resolved_key: InferenceType = {
type: 'array',
array_type: 'object',
items: {
type: 'object',
keys: resolved_key_info,
optional: type.items.optional || new_type.items.optional
},
optional: type.optional || new_type.optional
};
const did_change = JSON.stringify(resolved_key) !== JSON.stringify(type);
if (did_change) changed_keys.set(key, resolved_key);
}
break;
case 'primitive':
{
if (type.items.typeof.includes('never') && new_type.array_type === 'object') {
// Type is now known from previously unknown
changed_keys.set(key, new_type);
continue;
}
if (new_type.array_type !== 'primitive') {
// Type mismatch
changed_keys.set(key, {
type: 'array',
array_type: 'primitive',
items: {
type: 'primitive',
typeof: [ 'unknown' ],
optional: true
},
optional: true
});
continue;
}
const key_types = new Set([ ...new_type.items.typeof, ...type.items.typeof ]);
if (key_types.size > 1 && key_types.has('never'))
key_types.delete('never');
const resolved_key: InferenceType = {
type: 'array',
array_type: 'primitive',
items: {
type: 'primitive',
typeof: Array.from(key_types),
optional: type.items.optional || new_type.items.optional
},
optional: type.optional || new_type.optional
};
const did_change = JSON.stringify(resolved_key) !== JSON.stringify(type);
if (did_change) changed_keys.set(key, resolved_key);
}
break;
default:
throw new Error('Unreachable code reached! Switch missing case!');
}
break;
}
case 'misc':
{
if (new_type.type !== 'misc') continue;
if (type.misc_type !== new_type.misc_type) {
// We've got a type mismatch, this is unknown, we do not resolve unions
changed_keys.set(key, {
type: 'primitive',
typeof: [ 'unknown' ],
optional: true
});
}
switch (type.misc_type) {
case 'Author':
{
if (new_type.misc_type !== 'Author') break;
const had_optional_param = type.params[1] || new_type.params[1];
const either_optional = type.optional || new_type.optional;
const resolved_key: MiscInferenceType = {
type: 'misc',
misc_type: 'Author',
optional: either_optional,
params: [ new_type.params[0], had_optional_param ]
};
const did_change = JSON.stringify(resolved_key) !== JSON.stringify(type);
if (did_change) changed_keys.set(key, resolved_key);
}
break;
// Other cases can not change
}
}
break;
case 'primitive':
{
if (new_type.type !== 'primitive') continue;
const resolved_key: InferenceType = {
type: 'primitive',
typeof: Array.from(new Set([ ...new_type.typeof, ...type.typeof ])),
optional: type.optional || new_type.optional
};
const did_change = JSON.stringify(resolved_key) !== JSON.stringify(type);
if (did_change) changed_keys.set(key, resolved_key);
}
break;
}
}
for (const [ key, type ] of added_keys) {
changed_keys.set(key, {
...type,
optional: true
});
}
for (const [ key, type ] of removed_keys) {
changed_keys.set(key, {
...type,
optional: true
});
}
const unchanged_keys = key_info.filter(([ key ]) => !changed_keys.has(key));
const resolved_key_info_map = new Map([ ...unchanged_keys, ...changed_keys ]);
const resolved_key_info = [ ...resolved_key_info_map.entries() ];
return {
resolved_key_info,
changed_keys: [ ...changed_keys.entries() ]
};
}