Skip to content

[8.19] move language examples to a separate file (#4910) #4923

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

Merged
merged 1 commit into from
Jul 15, 2025
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
13 changes: 13 additions & 0 deletions compiler/src/steps/add-examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,18 @@ export default class ExamplesProcessor {
class BaseExamplesProcessor {
model: model.Model
specsFolder: string
static languageExamples: Record<string, model.ExampleAlternative[]> = {}

constructor (model: model.Model, specsFolder: string) {
this.model = model
this.specsFolder = specsFolder
if (Object.keys(BaseExamplesProcessor.languageExamples).length === 0) {
// load the language examples
const examplesJson = this.specsFolder + '/../docs/examples/languageExamples.json'
if (fs.existsSync(examplesJson)) {
BaseExamplesProcessor.languageExamples = JSON.parse(fs.readFileSync(examplesJson, 'utf8'))
}
}
}

// Log a 'warning' message.
Expand Down Expand Up @@ -139,6 +147,11 @@ class BaseExamplesProcessor {
const exampleFileContent = fs.readFileSync(filePath, 'utf8')
const exampleName = path.basename(fileName, path.extname(fileName))
const example: model.Example = yaml.load(exampleFileContent)
// find the language alternative examples and add them
const alternativeKey = 'specification/' + filePath.split('/specification/')[1]
if (BaseExamplesProcessor.languageExamples[alternativeKey] !== undefined) {
example.alternatives = BaseExamplesProcessor.languageExamples[alternativeKey]
}
// Some of the example files set their 'value' as a JSON string,
// and some files set their 'value' as an object. For consistency,
// if the value is not a JSON string, convert it to a JSON string.
Expand Down
20 changes: 15 additions & 5 deletions docs/examples/generate-language-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const {parseRequest} = require("@elastic/request-converter/dist/parse");
const {JavaCaller} = require("java-caller");

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

async function generateLanguages(example) {
const doc = yamlParseDocument(await fs.promises.readFile(example, 'utf8'));
Expand All @@ -38,14 +40,18 @@ async function generateLanguages(example) {
request += '\n' + JSON.stringify(data.value);
}
}
const alternatives = [];
if (data.alternatives) {
languageExamples[example] = data.alternatives;
delete data.alternatives;
}
let alternatives = [];
for (const lang of LANGUAGES) {
alternatives.push({
language: lang,
code: (await convertRequests(request, lang, {})).trim(),
});
}
data.alternatives = alternatives.concat((data.alternatives ?? []).filter(pair => !LANGUAGES.includes(pair.language)));
alternatives = alternatives.concat((languageExamples[example] ?? []).filter(pair => !LANGUAGES.includes(pair.language)));

// specific java example generator
if (process.argv[2] === "java") {
Expand Down Expand Up @@ -85,13 +91,13 @@ async function generateLanguages(example) {
code: stdout,
});
// replace old java examples
data.alternatives = data.alternatives.filter(pair => pair.language !== "Java");
data.alternatives = data.alternatives.concat(alternative_java);
alternatives = alternatives.filter(pair => pair.language !== "Java");
alternatives = alternatives.concat(alternative_java);
}
}

doc.delete('alternatives');
doc.add(doc.createPair('alternatives', data.alternatives));
languageExamples[example] = alternatives;
await fs.promises.writeFile(example, doc.toString({lineWidth: 132}));
}

Expand Down Expand Up @@ -128,6 +134,9 @@ async function main() {
let count = 0;
let errors = 0;
await loadSchema('output/schema/schema.json');
if (fs.existsSync(EXAMPLES_JSON)) {
languageExamples = JSON.parse(await fs.promises.readFile(EXAMPLES_JSON, 'utf8'));
}
for await (const example of walkExamples('./specification/')) {
try {
await generateLanguages(example);
Expand All @@ -138,6 +147,7 @@ async function main() {
}
count += 1;
}
await fs.promises.writeFile(EXAMPLES_JSON, JSON.stringify(languageExamples, null, 2));
console.log(`${count} examples processed, ${errors} errors.`);
}

Expand Down
Loading