Skip to content

Commit 9fb31bd

Browse files
authored
[route_name] Add serverless (#2610)
* Add make target to generate serverless version of the schema * Add serverless target to auto generation action * Add serverless entry to the Go generated routes * add debug helper for routes * pleasing the linter gods
1 parent 75f876f commit 9fb31bd

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

.github/workflows/generate.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ jobs:
5050
make compile
5151
make generate
5252
make transform-to-openapi
53+
make filter-for-serverless
5354
5455
- name: Check for Changed Files
5556
id: changes

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ transform-expand-generics: ## Create a new schema with all generics expanded
5050
transform-to-openapi: ## Generate the OpenAPI definition from the compiled schema
5151
@npm run transform-to-openapi --prefix compiler
5252

53+
filter-for-serverless: ## Generate the serverless version from the compiled schema
54+
@npm run --prefix compiler filter-by-availability -- --serverless --visibility=public --input ../output/schema/schema.json --output ../output/schema/schema-serverless.json
55+
5356
dump-routes: ## Create a new schema with all generics expanded
5457
@npm run dump-routes --prefix compiler
5558

compiler/src/dump/extract-routes.ts

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ import {
2424
Model
2525
} from '../model/metamodel'
2626

27+
// use npm run dump-routes --prefix compiler -- --debug to print the Go debug map
28+
const debugRoutes: boolean = argv.debug ?? false
2729
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')
3033

3134
export class Node {
3235
path: string
@@ -69,7 +72,7 @@ export class Trees {
6972
}
7073

7174
export class Forest {
72-
byVersion: Map<number, Trees>
75+
byVersion: Map<string, Trees>
7376

7477
constructor () {
7578
this.byVersion = new Map()
@@ -279,25 +282,34 @@ function extractRoutes (inputModel: Model): Trees {
279282

280283
async function extractRoutesFromFiles (outPath: string): Promise<void> {
281284
const v8Spec = await readFile(
282-
V8,
285+
V8SchemaUrl,
283286
{ encoding: 'utf8' }
284287
)
285288

286-
const data = await fetch(V7)
287-
const v7Spec = await data.text()
289+
const v7Schema = await fetch(V7SchemaUrl)
290+
const v7Spec = await v7Schema.text()
288291

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)
292301

293302
const forest = new Forest()
294303

295304
versions.forEach(function (spec, version) {
296305
const inputModel = JSON.parse(spec)
306+
if (debugRoutes) {
307+
debugTestRoutes(version, inputModel)
308+
}
297309
const routes = extractRoutes(inputModel)
298310
forest.byVersion.set(version, routes)
299311
})
300-
forest.byVersion.set(0, defaultRoutes())
312+
forest.byVersion.set('0', defaultRoutes())
301313

302314
const str = serializeForest(forest)
303315

@@ -549,3 +561,31 @@ function defaultRoutes (): Trees {
549561

550562
return t
551563
}
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

Comments
 (0)