Skip to content

Commit f8548ec

Browse files
move language examples to a separate file (#4910) (#4923)
(cherry picked from commit 1ba72cb) # Conflicts: # output/schema/schema.json # specification/_global/put_script/examples/request/PutScriptRequestExample2.yaml # specification/_global/reindex/examples/request/ReindexRequestExample1.yaml # specification/_global/reindex/examples/request/ReindexRequestExample10.yaml # specification/_global/reindex/examples/request/ReindexRequestExample11.yaml # specification/_global/reindex/examples/request/ReindexRequestExample12.yaml # specification/_global/reindex/examples/request/ReindexRequestExample13.yaml # specification/_global/reindex/examples/request/ReindexRequestExample2.yaml # specification/_global/reindex/examples/request/ReindexRequestExample3.yaml # specification/_global/reindex/examples/request/ReindexRequestExample4.yaml # specification/_global/reindex/examples/request/ReindexRequestExample5.yaml # specification/_global/reindex/examples/request/ReindexRequestExample6.yaml # specification/_global/reindex/examples/request/ReindexRequestExample7.yaml # specification/_global/reindex/examples/request/ReindexRequestExample8.yaml # specification/_global/reindex/examples/request/ReindexRequestExample9.yaml # specification/_global/scripts_painless_execute/examples/request/ExecutePainlessScriptRequestExample1.yaml # specification/_global/scripts_painless_execute/examples/request/ExecutePainlessScriptRequestExample2.yaml # specification/_global/scripts_painless_execute/examples/request/ExecutePainlessScriptRequestExample3.yaml # specification/_global/update/examples/request/UpdateRequestExample1.yaml # specification/_global/update/examples/request/UpdateRequestExample10.yaml # specification/_global/update/examples/request/UpdateRequestExample2.yaml # specification/_global/update/examples/request/UpdateRequestExample3.yaml # specification/_global/update/examples/request/UpdateRequestExample4.yaml # specification/_global/update/examples/request/UpdateRequestExample5.yaml # specification/_global/update/examples/request/UpdateRequestExample6.yaml # specification/_global/update/examples/request/UpdateRequestExample7.yaml # specification/_global/update/examples/request/UpdateRequestExample9.yaml # specification/_global/update_by_query/examples/request/UpdateByQueryRequestExample2.yaml # specification/_global/update_by_query/examples/request/UpdateByQueryRequestExample3.yaml # specification/_global/update_by_query/examples/request/UpdateByQueryRequestExample4.yaml # specification/eql/search/examples/request/EqlSearchRequestExample2.yaml # specification/esql/query/examples/request/QueryRequestExample1.yaml # specification/graph/explore/examples/request/GraphExploreRequestExample1.yaml # specification/indices/get_data_stream_settings/examples/request/IndicesGetDataStreamSettingsRequestExample1.yaml # specification/indices/put_data_stream_settings/examples/request/IndicesPutDataStreamSettingsRequestExample1.yaml # specification/indices/remove_block/examples/request/IndicesRemoveBlockRequestExample1.yaml # specification/inference/rerank/examples/request/RerankRequestExample2.yaml # specification/ml/put_job/examples/request/MlPutJobRequestExample1.yaml # specification/search_application/put/examples/request/SearchApplicationPutRequestExample1.yaml # specification/security/put_role_mapping/examples/request/SecurityPutRoleMappingRequestExample1.yaml # specification/security/put_role_mapping/examples/request/SecurityPutRoleMappingRequestExample2.yaml # specification/security/put_role_mapping/examples/request/SecurityPutRoleMappingRequestExample3.yaml # specification/security/put_role_mapping/examples/request/SecurityPutRoleMappingRequestExample5.yaml # specification/security/put_role_mapping/examples/request/SecurityPutRoleMappingRequestExample6.yaml # specification/security/put_role_mapping/examples/request/SecurityPutRoleMappingRequestExample9.yaml # specification/snapshot/repository_verify_integrity/examples/request/SnapshotRepositoryVerifyIntegrityExample1.yaml # specification/watcher/put_watch/examples/request/WatcherPutWatchRequestExample1.yaml Co-authored-by: Miguel Grinberg <[email protected]>
1 parent cc045c6 commit f8548ec

File tree

654 files changed

+16907
-37995
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

654 files changed

+16907
-37995
lines changed

compiler/src/steps/add-examples.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,18 @@ export default class ExamplesProcessor {
5454
class BaseExamplesProcessor {
5555
model: model.Model
5656
specsFolder: string
57+
static languageExamples: Record<string, model.ExampleAlternative[]> = {}
5758

5859
constructor (model: model.Model, specsFolder: string) {
5960
this.model = model
6061
this.specsFolder = specsFolder
62+
if (Object.keys(BaseExamplesProcessor.languageExamples).length === 0) {
63+
// load the language examples
64+
const examplesJson = this.specsFolder + '/../docs/examples/languageExamples.json'
65+
if (fs.existsSync(examplesJson)) {
66+
BaseExamplesProcessor.languageExamples = JSON.parse(fs.readFileSync(examplesJson, 'utf8'))
67+
}
68+
}
6169
}
6270

6371
// Log a 'warning' message.
@@ -139,6 +147,11 @@ class BaseExamplesProcessor {
139147
const exampleFileContent = fs.readFileSync(filePath, 'utf8')
140148
const exampleName = path.basename(fileName, path.extname(fileName))
141149
const example: model.Example = yaml.load(exampleFileContent)
150+
// find the language alternative examples and add them
151+
const alternativeKey = 'specification/' + filePath.split('/specification/')[1]
152+
if (BaseExamplesProcessor.languageExamples[alternativeKey] !== undefined) {
153+
example.alternatives = BaseExamplesProcessor.languageExamples[alternativeKey]
154+
}
142155
// Some of the example files set their 'value' as a JSON string,
143156
// and some files set their 'value' as an object. For consistency,
144157
// if the value is not a JSON string, convert it to a JSON string.

docs/examples/generate-language-examples.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const {parseRequest} = require("@elastic/request-converter/dist/parse");
2525
const {JavaCaller} = require("java-caller");
2626

2727
const LANGUAGES = ['Python', 'JavaScript', 'Ruby', 'PHP', 'curl'];
28+
const EXAMPLES_JSON = 'docs/examples/languageExamples.json';
29+
let languageExamples = {};
2830

2931
async function generateLanguages(example) {
3032
const doc = yamlParseDocument(await fs.promises.readFile(example, 'utf8'));
@@ -38,14 +40,18 @@ async function generateLanguages(example) {
3840
request += '\n' + JSON.stringify(data.value);
3941
}
4042
}
41-
const alternatives = [];
43+
if (data.alternatives) {
44+
languageExamples[example] = data.alternatives;
45+
delete data.alternatives;
46+
}
47+
let alternatives = [];
4248
for (const lang of LANGUAGES) {
4349
alternatives.push({
4450
language: lang,
4551
code: (await convertRequests(request, lang, {})).trim(),
4652
});
4753
}
48-
data.alternatives = alternatives.concat((data.alternatives ?? []).filter(pair => !LANGUAGES.includes(pair.language)));
54+
alternatives = alternatives.concat((languageExamples[example] ?? []).filter(pair => !LANGUAGES.includes(pair.language)));
4955

5056
// specific java example generator
5157
if (process.argv[2] === "java") {
@@ -85,13 +91,13 @@ async function generateLanguages(example) {
8591
code: stdout,
8692
});
8793
// replace old java examples
88-
data.alternatives = data.alternatives.filter(pair => pair.language !== "Java");
89-
data.alternatives = data.alternatives.concat(alternative_java);
94+
alternatives = alternatives.filter(pair => pair.language !== "Java");
95+
alternatives = alternatives.concat(alternative_java);
9096
}
9197
}
9298

9399
doc.delete('alternatives');
94-
doc.add(doc.createPair('alternatives', data.alternatives));
100+
languageExamples[example] = alternatives;
95101
await fs.promises.writeFile(example, doc.toString({lineWidth: 132}));
96102
}
97103

@@ -128,6 +134,9 @@ async function main() {
128134
let count = 0;
129135
let errors = 0;
130136
await loadSchema('output/schema/schema.json');
137+
if (fs.existsSync(EXAMPLES_JSON)) {
138+
languageExamples = JSON.parse(await fs.promises.readFile(EXAMPLES_JSON, 'utf8'));
139+
}
131140
for await (const example of walkExamples('./specification/')) {
132141
try {
133142
await generateLanguages(example);
@@ -138,6 +147,7 @@ async function main() {
138147
}
139148
count += 1;
140149
}
150+
await fs.promises.writeFile(EXAMPLES_JSON, JSON.stringify(languageExamples, null, 2));
141151
console.log(`${count} examples processed, ${errors} errors.`);
142152
}
143153

0 commit comments

Comments
 (0)