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

Dump routes to Go radix tree #1992

Merged
merged 19 commits into from
Nov 16, 2022
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ typescript-generator/lib
report
output/schema/import-*
output/schema/schema-no-generics.json
output/schema/routes.go
.github/**/package-lock.json

# Editor lockfiles
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ clean-dep: ## Clean npm dependencies
transform-expand-generics: ## Create a new schema with all generics expanded
@npm run transform-expand-generics --prefix compiler

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

contrib: | generate license-check spec-format-fix ## Pre contribution target

bump:
Expand Down
1 change: 1 addition & 0 deletions compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"format:fix": "prettier --config .prettierrc.json --write ../specification/",
"generate-schema": "ts-node src/index.ts",
"transform-expand-generics": "ts-node src/transform/expand-generics.ts",
"dump-routes": "ts-node src/dump/extract-routes.ts",
"compile:specification": "tsc --project ../specification/tsconfig.json --noEmit",
"build": "rm -rf lib && tsc",
"validate-ts-view": "tsc --noEmit ../output/typescript/types.ts",
Expand Down
161 changes: 161 additions & 0 deletions compiler/src/dump/extract-routes-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import test from 'ava'
import { insert, Node } from './extract-routes'

test('insertions', t => {
class Args {
name: string
routes: Array<{name: string, path: string}>
expect: Node

constructor (name: string, routes: Array<{name: string, path: string}>, expect: Node) {
this.name = name
this.routes = routes
this.expect = expect
}
}

const cases: Args[] = [
new Args('simple', [{ name: 'search', path: '/_search' }], new Node('/_search', 'search')),

new Args('with subpath', [
{ name: 'search', path: '/_search' },
{ name: 'test', path: '/_search/test' }
], new Node('/_search', 'search', [new Node('/test', 'test')])),

new Args('with split subpath', [
{ name: 'search', path: '/_search' },
{ name: 'test', path: '/_search/test' },
{ name: 'travel', path: '/_search/travel' }
], new Node('/_search', 'search', [
new Node('/t', '', [
new Node('est', 'test'),
new Node('ravel', 'travel')
])
])),

new Args('with split subpath and children', [
{ name: 'search', path: '/_search' },
{ name: 'test', path: '/_search/test' },
{ name: 'tool', path: '/_search/test/tool' },
{ name: 'travel', path: '/_search/travel' }
], new Node('/_search', 'search', [
new Node('/t', '', [
new Node('est', 'test', [
new Node('/tool', 'tool')
]),

new Node('ravel', 'travel')
])
])
),

new Args('with split subpath, 2 direct children', [
{ name: 'search', path: '/_search' },
{ name: 'tool', path: '/_search/test/tool' },
{ name: 'travel', path: '/_search/travel/test' }
], new Node('/_search', 'search', [
new Node('/t', '', [
new Node('est/tool', 'tool'),
new Node('ravel/test', 'travel')
])
])
),

new Args('with split subpath and 3 children, 2 indirect', [
{ name: 'search', path: '/_search' },
{ name: 'test', path: '/_search/test' },
{ name: 'tool', path: '/_search/test/tool' },
{ name: 'travel', path: '/_search/travel/test' }
], new Node('/_search', 'search', [
new Node('/t', '', [
new Node('est', 'test', [
new Node('/tool', 'tool')
]),

new Node('ravel/test', 'travel')
])
])
),

new Args('with root variable', [
{ name: 'search', path: '/_search' },
{ name: 'doc', path: '/{index}/_doc' }
], new Node('/', '', [
new Node('_search', 'search'),
new Node('{index}', '', [
new Node('/_doc', 'doc')
], true)
])
),

new Args('with two consecutive variables from root', [
{ name: 'search', path: '/_search' },
{ name: 'doc', path: '/{a}/{b}/_doc' }
], new Node('/', '', [
new Node('_search', 'search'),
new Node('{a}', '', [
new Node('/', 'doc', [
new Node('{b}', '', [
new Node('/_doc', 'doc')
], true)
])
], true)
])
),

new Args('with variable and an ending variable', [
{ name: 'search', path: '/_search' },
{ name: 'doc', path: '/{a}/_doc/{c}' }
], new Node('/', '', [
new Node('_search', 'search'),
new Node('{a}', '', [
new Node('/_doc/', 'doc', [
new Node('{c}', 'doc', [], true)
])
], true)
])
),

new Args('with variable and an ending variable', [
{ name: 'search', path: '/_search' },
{ name: 'doc', path: '/{a}/_doc/{c}' }
], new Node('/', '', [
new Node('_search', 'search'),
new Node('{a}', '', [
new Node('/_doc/', 'doc', [
new Node('{c}', 'doc', [], true)
])
], true)
])
)

]

for (const args of cases) {
t.log(args.name)
const root = new Node()
for (const route of args.routes) {
insert(root, route.path, route.name)
}
t.deepEqual(root, args.expect)
}
})
Loading