@@ -24,9 +24,12 @@ import {
24
24
Model
25
25
} from '../model/metamodel'
26
26
27
+ // use npm run dump-routes --prefix compiler -- --debug to print the Go debug map
28
+ const debugRoutes : boolean = argv . debug ?? false
27
29
const outputPath = argv . output ?? join ( __dirname , '..' , '..' , '..' , 'output' , 'schema' , 'routes.go' )
28
- const V8 = join ( __dirname , '..' , '..' , '..' , 'output' , 'schema' , 'schema.json' )
29
- const V7 = 'https://raw.githubusercontent.com/elastic/elasticsearch-specification/7.17/output/schema/schema.json'
30
+ const V8SchemaUrl = join ( __dirname , '..' , '..' , '..' , 'output' , 'schema' , 'schema.json' )
31
+ const V7SchemaUrl = 'https://raw.githubusercontent.com/elastic/elasticsearch-specification/7.17/output/schema/schema.json'
32
+ const serverlessSchemaUrl = join ( __dirname , '..' , '..' , '..' , 'output' , 'schema' , 'schema-serverless.json' )
30
33
31
34
export class Node {
32
35
path : string
@@ -69,7 +72,7 @@ export class Trees {
69
72
}
70
73
71
74
export class Forest {
72
- byVersion : Map < number , Trees >
75
+ byVersion : Map < string , Trees >
73
76
74
77
constructor ( ) {
75
78
this . byVersion = new Map ( )
@@ -279,25 +282,34 @@ function extractRoutes (inputModel: Model): Trees {
279
282
280
283
async function extractRoutesFromFiles ( outPath : string ) : Promise < void > {
281
284
const v8Spec = await readFile (
282
- V8 ,
285
+ V8SchemaUrl ,
283
286
{ encoding : 'utf8' }
284
287
)
285
288
286
- const data = await fetch ( V7 )
287
- const v7Spec = await data . text ( )
289
+ const v7Schema = await fetch ( V7SchemaUrl )
290
+ const v7Spec = await v7Schema . text ( )
288
291
289
- const versions = new Map < number , string > ( )
290
- versions . set ( 7 , v7Spec )
291
- versions . set ( 8 , v8Spec )
292
+ const serverlessSpec = await readFile (
293
+ serverlessSchemaUrl ,
294
+ { encoding : 'utf8' }
295
+ )
296
+
297
+ const versions = new Map < string , string > ( )
298
+ versions . set ( '7' , v7Spec )
299
+ versions . set ( '8' , v8Spec )
300
+ versions . set ( 'serverless' , serverlessSpec )
292
301
293
302
const forest = new Forest ( )
294
303
295
304
versions . forEach ( function ( spec , version ) {
296
305
const inputModel = JSON . parse ( spec )
306
+ if ( debugRoutes ) {
307
+ debugTestRoutes ( version , inputModel )
308
+ }
297
309
const routes = extractRoutes ( inputModel )
298
310
forest . byVersion . set ( version , routes )
299
311
} )
300
- forest . byVersion . set ( 0 , defaultRoutes ( ) )
312
+ forest . byVersion . set ( '0' , defaultRoutes ( ) )
301
313
302
314
const str = serializeForest ( forest )
303
315
@@ -549,3 +561,31 @@ function defaultRoutes (): Trees {
549
561
550
562
return t
551
563
}
564
+
565
+ function debugTestRoutes ( version : string , inputModel : Model ) : void {
566
+ console . log ( version )
567
+
568
+ const output = new Map < string , Array < Map < string , string > > > ( )
569
+
570
+ for ( const endpoint of inputModel . endpoints ) {
571
+ for ( const url of endpoint . urls ) {
572
+ for ( const method of url . methods ) {
573
+ if ( ! output . has ( method ) ) {
574
+ output . set ( method , [ ] )
575
+ }
576
+ const newPath = url . path . replace ( / \{ | \} / g, '' )
577
+ output . get ( method ) ?. push ( new Map < string , string > ( [ [ newPath , endpoint . name ] ] ) )
578
+ }
579
+ }
580
+ }
581
+
582
+ output . forEach ( ( urls , method ) => {
583
+ console . log ( '"%s": {' , method )
584
+ urls . forEach ( ( path ) => {
585
+ path . forEach ( ( name , path ) => {
586
+ console . log ( '{"%s", "%s"},' , path , name )
587
+ } )
588
+ } )
589
+ console . log ( '},' )
590
+ } )
591
+ }
0 commit comments