Skip to content

Commit 0321404

Browse files
committed
started on clients-flight-generator output
1 parent 085b9b5 commit 0321404

10 files changed

+5158
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Specification } from "../specification/src/api-specification";
2+
export declare class FlightRecorderJsonGenerator {
3+
private specification;
4+
constructor(specification: Specification);
5+
export(folder: string): void;
6+
private toType;
7+
private dispatchInterface;
8+
private createEnumSchema;
9+
private createTypeSchema;
10+
private createArraySchema;
11+
private createDictionarySchema;
12+
private createInterfaceProperty;
13+
private createUnionOfSchema;
14+
private dispatchInstanceOf;
15+
private toSchema;
16+
}

flight-recorder-generator/flight-recorder-json-generator.js

Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import {Specification} from "../specification/src/api-specification";
2+
import * as fs from "fs";
3+
import * as path from "path";
4+
import * as Domain from "elasticsearch-client-specification/src/domain";
5+
6+
export class FlightRecorderJsonGenerator {
7+
constructor(private specification: Specification) { }
8+
9+
export(folder: string) {
10+
11+
const f = path.join(__dirname, folder);
12+
this.specification.endpoints.forEach(api => {
13+
const pathPrefix = path.join(f, api.name);
14+
const request = this.toType(api.typeMapping.request);
15+
fs.writeFileSync(pathPrefix + "_request.json", JSON.stringify(request, null, 2));
16+
const response = this.toType(api.typeMapping.response);
17+
fs.writeFileSync(pathPrefix + "_response.json", JSON.stringify(response, null, 2));
18+
});
19+
20+
}
21+
private toType(typeName: string) {
22+
const type = this.specification.typeLookup[typeName];
23+
return this.toSchema(type);
24+
}
25+
26+
private dispatchInterface(i: Domain.Interface) {
27+
if (i.inheritsFromUnresolved.some(t => t === "String")) return "__" + i.name + "__";
28+
29+
switch (i.name) {
30+
case "Uri" : return "__uri__";
31+
case "Date" : return "__date__";
32+
case "TimeSpan" : return "__duration__";
33+
case "SourceDocument" : return "__source__";
34+
case "short" :
35+
case "byte" :
36+
case "integer" :
37+
case "long" :
38+
case "float" :
39+
case "double" :
40+
return i.name;
41+
}
42+
return i.properties.reduce((o, p) => ({...o, [p.name]: this.createInterfaceProperty(p)}), {});
43+
}
44+
45+
private createEnumSchema(enumType: Domain.Enum) {
46+
return {
47+
type: "string",
48+
description: enumType.flags ? "flags" : null,
49+
enum: enumType.members.map(e => e.name)
50+
};
51+
}
52+
53+
private createTypeSchema(type: Domain.Type) {
54+
if (type.name === "boolean") return "boolean";
55+
if (type.name === "string") return "string";
56+
57+
const i = this.specification.typeLookup[type.name];
58+
return {}
59+
// return this.toSchema(i);
60+
}
61+
62+
private createArraySchema(arr: Domain.ArrayOf) {
63+
return [this.dispatchInstanceOf(arr.of)];
64+
}
65+
66+
private createDictionarySchema(dict: Domain.Dictionary) {
67+
// todo handle additionalProperties and find out how we can type the key.
68+
return { __name__ : this.dispatchInstanceOf(dict.value) };
69+
}
70+
71+
private createInterfaceProperty(property: Domain.InterfaceProperty) {
72+
return this.dispatchInstanceOf(property.type);
73+
}
74+
75+
private createUnionOfSchema(union: Domain.UnionOf) {
76+
// union should be oneOf but open api does not support the full json-schema draft 4
77+
return { __anyOf__ : [
78+
union.items.map(i => this.dispatchInstanceOf(i))
79+
] };
80+
}
81+
82+
private dispatchInstanceOf(type: Domain.InstanceOf) {
83+
if (type instanceof Domain.Dictionary) return this.createDictionarySchema(type);
84+
if (type instanceof Domain.UnionOf) return this.createUnionOfSchema(type);
85+
if (type instanceof Domain.Type) return this.createTypeSchema(type);
86+
if (type instanceof Domain.ArrayOf) return this.createArraySchema(type);
87+
return { type: "object", description: "Unknown InstanceOf" };
88+
}
89+
private toSchema(type: Domain.TypeDeclaration) {
90+
if (type instanceof Domain.Enum) return this.createEnumSchema(type);
91+
if (type instanceof Domain.Interface) return this.dispatchInterface(type);
92+
return { type: "object", description: "Unknown TypeDeclaration" };
93+
}
94+
}

flight-recorder-generator/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {};

flight-recorder-generator/index.js

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

flight-recorder-generator/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {Specification} from "../specification/src/api-specification";
2+
import {FlightRecorderJsonGenerator} from "./flight-recorder-json-generator";
3+
// tslint:disable:no-console
4+
5+
const specification = Specification.load();
6+
7+
if (specification.endpoint_errors.length > 0) console.error("The specification contains the following endpoint mapping errors:");
8+
9+
for (const e of specification.endpoint_errors) console.error(" - " + e);
10+
11+
if (specification.domain_errors.length + specification.endpoint_errors.length === 0)
12+
console.log("The specification contains no errors in any of the " +
13+
specification.endpoints.length + " endpoints yielding " + specification.types.length + " types");
14+
15+
console.log("The specification contains " + specification.endpoints.length + " endpoints yielding " + specification.types.length + " types");
16+
17+
const swaggerGenerator = new FlightRecorderJsonGenerator(specification);
18+
swaggerGenerator.export("output");

0 commit comments

Comments
 (0)