Skip to content

Commit 4ca0cc0

Browse files
authored
Dump routes to Go radix tree (elastic#1992)
* First radix-tree working version without variables and path names * Add comments * First working version with variables * Add names to routes * Add tests and fixes that went with them * Add names to parent of potentially optional variables * Add tests, fixes and Go output serialization * Rework parser to use variable names instead of stars as placeholder * Update Go tree serialization * Update split method for variables and naming attribution for latest edge cases * checkpoint add v7 routes to the mix * Add standard routes to the tree generation * Move code to a more explicit folder * Update default routes * Add dump routes target to gitignore * Remove code duplication * Align makefile with folder & name change * Update code to please the linting gods * Restore header padding to please both the linter and the license check
1 parent d63a0e3 commit 4ca0cc0

File tree

5 files changed

+716
-0
lines changed

5 files changed

+716
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ typescript-generator/lib
5757
report
5858
output/schema/import-*
5959
output/schema/schema-no-generics.json
60+
output/schema/routes.go
6061
.github/**/package-lock.json
6162

6263
# Editor lockfiles

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ clean-dep: ## Clean npm dependencies
4747
transform-expand-generics: ## Create a new schema with all generics expanded
4848
@npm run transform-expand-generics --prefix compiler
4949

50+
dump-routes: ## Create a new schema with all generics expanded
51+
@npm run dump-routes --prefix compiler
52+
5053
contrib: | generate license-check spec-format-fix ## Pre contribution target
5154

5255
bump:

compiler/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"format:fix": "prettier --config .prettierrc.json --write ../specification/",
1111
"generate-schema": "ts-node src/index.ts",
1212
"transform-expand-generics": "ts-node src/transform/expand-generics.ts",
13+
"dump-routes": "ts-node src/dump/extract-routes.ts",
1314
"compile:specification": "tsc --project ../specification/tsconfig.json --noEmit",
1415
"build": "rm -rf lib && tsc",
1516
"validate-ts-view": "tsc --noEmit ../output/typescript/types.ts",
+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import test from 'ava'
21+
import { insert, Node } from './extract-routes'
22+
23+
test('insertions', t => {
24+
class Args {
25+
name: string
26+
routes: Array<{name: string, path: string}>
27+
expect: Node
28+
29+
constructor (name: string, routes: Array<{name: string, path: string}>, expect: Node) {
30+
this.name = name
31+
this.routes = routes
32+
this.expect = expect
33+
}
34+
}
35+
36+
const cases: Args[] = [
37+
new Args('simple', [{ name: 'search', path: '/_search' }], new Node('/_search', 'search')),
38+
39+
new Args('with subpath', [
40+
{ name: 'search', path: '/_search' },
41+
{ name: 'test', path: '/_search/test' }
42+
], new Node('/_search', 'search', [new Node('/test', 'test')])),
43+
44+
new Args('with split subpath', [
45+
{ name: 'search', path: '/_search' },
46+
{ name: 'test', path: '/_search/test' },
47+
{ name: 'travel', path: '/_search/travel' }
48+
], new Node('/_search', 'search', [
49+
new Node('/t', '', [
50+
new Node('est', 'test'),
51+
new Node('ravel', 'travel')
52+
])
53+
])),
54+
55+
new Args('with split subpath and children', [
56+
{ name: 'search', path: '/_search' },
57+
{ name: 'test', path: '/_search/test' },
58+
{ name: 'tool', path: '/_search/test/tool' },
59+
{ name: 'travel', path: '/_search/travel' }
60+
], new Node('/_search', 'search', [
61+
new Node('/t', '', [
62+
new Node('est', 'test', [
63+
new Node('/tool', 'tool')
64+
]),
65+
66+
new Node('ravel', 'travel')
67+
])
68+
])
69+
),
70+
71+
new Args('with split subpath, 2 direct children', [
72+
{ name: 'search', path: '/_search' },
73+
{ name: 'tool', path: '/_search/test/tool' },
74+
{ name: 'travel', path: '/_search/travel/test' }
75+
], new Node('/_search', 'search', [
76+
new Node('/t', '', [
77+
new Node('est/tool', 'tool'),
78+
new Node('ravel/test', 'travel')
79+
])
80+
])
81+
),
82+
83+
new Args('with split subpath and 3 children, 2 indirect', [
84+
{ name: 'search', path: '/_search' },
85+
{ name: 'test', path: '/_search/test' },
86+
{ name: 'tool', path: '/_search/test/tool' },
87+
{ name: 'travel', path: '/_search/travel/test' }
88+
], new Node('/_search', 'search', [
89+
new Node('/t', '', [
90+
new Node('est', 'test', [
91+
new Node('/tool', 'tool')
92+
]),
93+
94+
new Node('ravel/test', 'travel')
95+
])
96+
])
97+
),
98+
99+
new Args('with root variable', [
100+
{ name: 'search', path: '/_search' },
101+
{ name: 'doc', path: '/{index}/_doc' }
102+
], new Node('/', '', [
103+
new Node('_search', 'search'),
104+
new Node('{index}', '', [
105+
new Node('/_doc', 'doc')
106+
], true)
107+
])
108+
),
109+
110+
new Args('with two consecutive variables from root', [
111+
{ name: 'search', path: '/_search' },
112+
{ name: 'doc', path: '/{a}/{b}/_doc' }
113+
], new Node('/', '', [
114+
new Node('_search', 'search'),
115+
new Node('{a}', '', [
116+
new Node('/', 'doc', [
117+
new Node('{b}', '', [
118+
new Node('/_doc', 'doc')
119+
], true)
120+
])
121+
], true)
122+
])
123+
),
124+
125+
new Args('with variable and an ending variable', [
126+
{ name: 'search', path: '/_search' },
127+
{ name: 'doc', path: '/{a}/_doc/{c}' }
128+
], new Node('/', '', [
129+
new Node('_search', 'search'),
130+
new Node('{a}', '', [
131+
new Node('/_doc/', 'doc', [
132+
new Node('{c}', 'doc', [], true)
133+
])
134+
], true)
135+
])
136+
),
137+
138+
new Args('with variable and an ending variable', [
139+
{ name: 'search', path: '/_search' },
140+
{ name: 'doc', path: '/{a}/_doc/{c}' }
141+
], new Node('/', '', [
142+
new Node('_search', 'search'),
143+
new Node('{a}', '', [
144+
new Node('/_doc/', 'doc', [
145+
new Node('{c}', 'doc', [], true)
146+
])
147+
], true)
148+
])
149+
)
150+
151+
]
152+
153+
for (const args of cases) {
154+
t.log(args.name)
155+
const root = new Node()
156+
for (const route of args.routes) {
157+
insert(root, route.path, route.name)
158+
}
159+
t.deepEqual(root, args.expect)
160+
}
161+
})

0 commit comments

Comments
 (0)