Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[route_name] Add serverless #2610

Merged
merged 5 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/generate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jobs:
make compile
make generate
make transform-to-openapi
make filter-for-serverless

- name: Check for Changed Files
id: changes
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ transform-expand-generics: ## Create a new schema with all generics expanded
transform-to-openapi: ## Generate the OpenAPI definition from the compiled schema
@npm run transform-to-openapi --prefix compiler

filter-for-serverless: ## Generate the serverless version from the compiled schema
@npm run --prefix compiler filter-by-availability -- --serverless --visibility=public --input ../output/schema/schema.json --output ../output/schema/schema-serverless.json

dump-routes: ## Create a new schema with all generics expanded
@npm run dump-routes --prefix compiler

Expand Down
60 changes: 50 additions & 10 deletions compiler/src/dump/extract-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ import {
Model
} from '../model/metamodel'

// use npm run dump-routes --prefix compiler -- --debug to print the Go debug map
const debugRoutes: boolean = argv.debug ?? false
const outputPath = argv.output ?? join(__dirname, '..', '..', '..', 'output', 'schema', 'routes.go')
const V8 = join(__dirname, '..', '..', '..', 'output', 'schema', 'schema.json')
const V7 = 'https://raw.githubusercontent.com/elastic/elasticsearch-specification/7.17/output/schema/schema.json'
const V8SchemaUrl = join(__dirname, '..', '..', '..', 'output', 'schema', 'schema.json')
const V7SchemaUrl = 'https://raw.githubusercontent.com/elastic/elasticsearch-specification/7.17/output/schema/schema.json'
const serverlessSchemaUrl = join(__dirname, '..', '..', '..', 'output', 'schema', 'schema-serverless.json')

export class Node {
path: string
Expand Down Expand Up @@ -69,7 +72,7 @@ export class Trees {
}

export class Forest {
byVersion: Map<number, Trees>
byVersion: Map<string, Trees>

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

async function extractRoutesFromFiles (outPath: string): Promise<void> {
const v8Spec = await readFile(
V8,
V8SchemaUrl,
{ encoding: 'utf8' }
)

const data = await fetch(V7)
const v7Spec = await data.text()
const v7Schema = await fetch(V7SchemaUrl)
const v7Spec = await v7Schema.text()

const versions = new Map<number, string>()
versions.set(7, v7Spec)
versions.set(8, v8Spec)
const serverlessSpec = await readFile(
serverlessSchemaUrl,
{ encoding: 'utf8' }
)

const versions = new Map<string, string>()
versions.set('7', v7Spec)
versions.set('8', v8Spec)
versions.set('serverless', serverlessSpec)

const forest = new Forest()

versions.forEach(function (spec, version) {
const inputModel = JSON.parse(spec)
if (debugRoutes) {
debugTestRoutes(version, inputModel)
}
const routes = extractRoutes(inputModel)
forest.byVersion.set(version, routes)
})
forest.byVersion.set(0, defaultRoutes())
forest.byVersion.set('0', defaultRoutes())

const str = serializeForest(forest)

Expand Down Expand Up @@ -549,3 +561,31 @@ function defaultRoutes (): Trees {

return t
}

function debugTestRoutes (version: string, inputModel: Model): void {
console.log(version)

const output = new Map<string, Array<Map<string, string>>>()

for (const endpoint of inputModel.endpoints) {
for (const url of endpoint.urls) {
for (const method of url.methods) {
if (!output.has(method)) {
output.set(method, [])
}
const newPath = url.path.replace(/\{|\}/g, '')
output.get(method)?.push(new Map<string, string>([[newPath, endpoint.name]]))
}
}
}

output.forEach((urls, method) => {
console.log('"%s": {', method)
urls.forEach((path) => {
path.forEach((name, path) => {
console.log('{"%s", "%s"},', path, name)
})
})
console.log('},')
})
}
Loading