Skip to content

Commit 0c08832

Browse files
move language examples to a separate file
1 parent 6bf7f10 commit 0c08832

File tree

656 files changed

+72
-37905
lines changed

Some content is hidden

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

656 files changed

+72
-37905
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

output/schema/schema.json

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

specification/_global/bulk/examples/request/BulkRequestExample1.yaml

Lines changed: 0 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -15,174 +15,3 @@ value: '{ "index" : { "_index" : "test", "_id" : "1" } }
1515
{ "update" : {"_id" : "1", "_index" : "test"} }
1616
1717
{ "doc" : {"field2" : "value2"} }'
18-
alternatives:
19-
- language: Python
20-
code: |-
21-
resp = client.bulk(
22-
operations=[
23-
{
24-
"index": {
25-
"_index": "test",
26-
"_id": "1"
27-
}
28-
},
29-
{
30-
"field1": "value1"
31-
},
32-
{
33-
"delete": {
34-
"_index": "test",
35-
"_id": "2"
36-
}
37-
},
38-
{
39-
"create": {
40-
"_index": "test",
41-
"_id": "3"
42-
}
43-
},
44-
{
45-
"field1": "value3"
46-
},
47-
{
48-
"update": {
49-
"_id": "1",
50-
"_index": "test"
51-
}
52-
},
53-
{
54-
"doc": {
55-
"field2": "value2"
56-
}
57-
}
58-
],
59-
)
60-
- language: JavaScript
61-
code: |-
62-
const response = await client.bulk({
63-
operations: [
64-
{
65-
index: {
66-
_index: "test",
67-
_id: "1",
68-
},
69-
},
70-
{
71-
field1: "value1",
72-
},
73-
{
74-
delete: {
75-
_index: "test",
76-
_id: "2",
77-
},
78-
},
79-
{
80-
create: {
81-
_index: "test",
82-
_id: "3",
83-
},
84-
},
85-
{
86-
field1: "value3",
87-
},
88-
{
89-
update: {
90-
_id: "1",
91-
_index: "test",
92-
},
93-
},
94-
{
95-
doc: {
96-
field2: "value2",
97-
},
98-
},
99-
],
100-
});
101-
- language: Ruby
102-
code: |-
103-
response = client.bulk(
104-
body: [
105-
{
106-
"index": {
107-
"_index": "test",
108-
"_id": "1"
109-
}
110-
},
111-
{
112-
"field1": "value1"
113-
},
114-
{
115-
"delete": {
116-
"_index": "test",
117-
"_id": "2"
118-
}
119-
},
120-
{
121-
"create": {
122-
"_index": "test",
123-
"_id": "3"
124-
}
125-
},
126-
{
127-
"field1": "value3"
128-
},
129-
{
130-
"update": {
131-
"_id": "1",
132-
"_index": "test"
133-
}
134-
},
135-
{
136-
"doc": {
137-
"field2": "value2"
138-
}
139-
}
140-
]
141-
)
142-
- language: PHP
143-
code: |-
144-
$resp = $client->bulk([
145-
"body" => array(
146-
[
147-
"index" => [
148-
"_index" => "test",
149-
"_id" => "1",
150-
],
151-
],
152-
[
153-
"field1" => "value1",
154-
],
155-
[
156-
"delete" => [
157-
"_index" => "test",
158-
"_id" => "2",
159-
],
160-
],
161-
[
162-
"create" => [
163-
"_index" => "test",
164-
"_id" => "3",
165-
],
166-
],
167-
[
168-
"field1" => "value3",
169-
],
170-
[
171-
"update" => [
172-
"_id" => "1",
173-
"_index" => "test",
174-
],
175-
],
176-
[
177-
"doc" => [
178-
"field2" => "value2",
179-
],
180-
],
181-
),
182-
]);
183-
- language: curl
184-
code:
185-
"curl -X POST -H \"Authorization: ApiKey $ELASTIC_API_KEY\" -H \"Content-Type: application/json\" -d
186-
'[{\"index\":{\"_index\":\"test\",\"_id\":\"1\"}},{\"field1\":\"value1\"},{\"delete\":{\"_index\":\"test\",\"_id\":\"2\"}},{\
187-
\"create\":{\"_index\":\"test\",\"_id\":\"3\"}},{\"field1\":\"value3\"},{\"update\":{\"_id\":\"1\",\"_index\":\"test\"}},{\"d\
188-
oc\":{\"field2\":\"value2\"}}]' \"$ELASTICSEARCH_URL/_bulk\""

0 commit comments

Comments
 (0)