Skip to content

refactor(data-structure): use @himenon/path-oriented-data-structure #11

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 5 commits into from
Jan 17, 2021
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"node-fetch": "2.6.1"
},
"dependencies": {
"@himenon/path-oriented-data-structure": "0.1.0",
"@types/json-schema": "7.0.6",
"ajv": "7.0.3",
"dot-prop": "6.0.1",
Expand Down
6 changes: 3 additions & 3 deletions src/Converter/v3/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const create = (entryPoint: string, store: Store.Type, factory: TypeScrip
});
if (ts.isTypeLiteralNode(typeNode)) {
store.addStatement(reference.path, {
type: "interface",
kind: "interface",
name: reference.name,
value: factory.InterfaceDeclaration.create({
export: true,
Expand All @@ -107,7 +107,7 @@ export const create = (entryPoint: string, store: Store.Type, factory: TypeScrip
});
store.addStatement(reference.path, {
name: reference.name,
type: "typeAlias",
kind: "typeAlias",
value,
});
}
Expand All @@ -123,7 +123,7 @@ export const create = (entryPoint: string, store: Store.Type, factory: TypeScrip
});
store.addStatement(reference.path, {
name: reference.name,
type: "typeAlias",
kind: "typeAlias",
value,
});
}
Expand Down
12 changes: 3 additions & 9 deletions src/Converter/v3/components/Headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,9 @@ export const generateNamespace = (
context: ToTypeNode.Context,
): void => {
store.addComponent("headers", {
type: "namespace",
kind: "namespace",
name: Name.Components.Headers,
value: factory.Namespace.create({
export: true,
name: Name.Components.Headers,
statements: [],
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#componentsObject`,
}),
statements: {},
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#componentsObject`,
});
Object.entries(headers).forEach(([name, header]) => {
if (Guard.isReference(header)) {
Expand All @@ -40,7 +34,7 @@ export const generateNamespace = (
}
} else {
store.addStatement(`components/headers/${name}`, {
type: "typeAlias",
kind: "typeAlias",
name: name,
value: Header.generateTypeNode(entryPoint, currentPoint, factory, name, header, context),
});
Expand Down
20 changes: 7 additions & 13 deletions src/Converter/v3/components/Operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,16 @@ export const generateNamespace = (
throw new Error("not setting operationId\n" + JSON.stringify(operation));
}
store.addStatement(basePath, {
type: "namespace",
kind: "namespace",
name,
value: factory.Namespace.create({
export: true,
name,
comment: ExternalDocumentation.addComment(Servers.addComment([generateComment(operation)], operation.servers), operation.externalDocs),
deprecated: operation.deprecated,
statements: [],
}),
statements: {},
comment: ExternalDocumentation.addComment(Servers.addComment([generateComment(operation)], operation.servers), operation.externalDocs),
deprecated: operation.deprecated,
});

if (operation.parameters) {
const parameterName = "Parameter";
store.addStatement(`${basePath}/Parameter`, {
type: "interface",
kind: "interface",
name: parameterName,
value: Parameter.generateInterface(entryPoint, currentPoint, factory, parameterName, operation.parameters, context),
});
Expand All @@ -79,13 +73,13 @@ export const generateNamespace = (
const contentPath = path.join(reference.path, "Content"); // requestBodyはNamespaceを形成するため
const name = "Content";
store.addStatement(contentPath, {
type: "interface",
kind: "interface",
name: name,
value: RequestBody.generateInterface(entryPoint, reference.referencePoint, factory, name, reference.data, context),
});
const typeAliasName = context.resolveReferencePath(currentPoint, contentPath).name;
store.addStatement(`${basePath}/RequestBody`, {
type: "typeAlias",
kind: "typeAlias",
name: typeAliasName,
value: factory.TypeAliasDeclaration.create({
export: true,
Expand Down Expand Up @@ -143,7 +137,7 @@ export const generateStatements = (
const contentPath = path.join(reference.path, "Content"); // requestBodyはNamespaceを形成するため
const name = "Content";
store.addStatement(contentPath, {
type: "interface",
kind: "interface",
name: name,
value: RequestBody.generateInterface(entryPoint, reference.referencePoint, factory, name, reference.data, context),
});
Expand Down
28 changes: 8 additions & 20 deletions src/Converter/v3/components/Parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,9 @@ export const generateNamespace = (
): void => {
const basePath = "components/parameters";
store.addComponent("parameters", {
type: "namespace",
kind: "namespace",
name: Name.Components.Parameters,
value: factory.Namespace.create({
export: true,
name: Name.Components.Parameters,
statements: [],
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#parameterObject`,
}),
statements: {},
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#parameterObject`,
});

Object.entries(parameters).forEach(([name, parameter]) => {
Expand All @@ -41,7 +35,7 @@ export const generateNamespace = (
}
Schema.addSchema(entryPoint, currentPoint, store, factory, reference.path, reference.name, reference.data.schema, context);
store.addStatement(`${basePath}/${name}`, {
type: "typeAlias",
kind: "typeAlias",
name: name,
value: factory.TypeAliasDeclaration.create({
export: true,
Expand All @@ -55,7 +49,7 @@ export const generateNamespace = (
} else {
const path = `${basePath}/${name}`;
store.addStatement(path, {
type: "typeAlias",
kind: "typeAlias",
name: name,
value: Paramter.generateTypeAlias(entryPoint, currentPoint, factory, name, parameter, context),
});
Expand All @@ -72,15 +66,9 @@ export const generateNamespaceWithList = (
context: ToTypeNode.Context,
): void => {
store.addComponent("parameters", {
type: "namespace",
kind: "namespace",
name: Name.Components.Parameters,
value: factory.Namespace.create({
export: true,
name: Name.Components.Parameters,
statements: [],
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#parameterObject`,
}),
statements: {},
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#parameterObject`,
});

parameters.forEach(parameter => {
Expand All @@ -91,14 +79,14 @@ export const generateNamespaceWithList = (
}
const path = `components/parameters/${reference.name}`;
return store.addStatement(path, {
type: "typeAlias",
kind: "typeAlias",
name: reference.name,
value: Paramter.generateTypeAlias(entryPoint, reference.referencePoint, factory, reference.name, reference.data, context),
});
}
const path = `components/parameters/${parameter.name}`;
return store.addStatement(path, {
type: "typeAlias",
kind: "typeAlias",
name: parameter.name,
value: Paramter.generateTypeAlias(entryPoint, currentPoint, factory, parameter.name, parameter, context),
});
Expand Down
10 changes: 2 additions & 8 deletions src/Converter/v3/components/PathItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,9 @@ export const generateNamespace = (
const basePath = `${parentPath}/${name}`;
const topComment = options && options.topComment && options.topComment;
store.addStatement(basePath, {
type: "namespace",
kind: "namespace",
name,
value: factory.Namespace.create({
export: true,
name,
statements: [],
comment: Servers.addComment([topComment, pathItem.description], pathItem.servers),
}),
statements: {},
comment: Servers.addComment([topComment, pathItem.description], pathItem.servers),
});
if (pathItem.get) {
Operation.generateNamespace(entryPoint, currentPoint, store, factory, basePath, "GET", pathItem.get, context);
Expand Down
10 changes: 2 additions & 8 deletions src/Converter/v3/components/PathItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,9 @@ export const generateNamespace = (
const basePath = "components/pathItems";

store.addComponent("pathItems", {
type: "namespace",
kind: "namespace",
name: Name.Components.PathItems,
value: factory.Namespace.create({
export: true,
name: Name.Components.PathItems,
statements: [],
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#componentsObject`,
}),
statements: {},
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#componentsObject`,
});

Object.entries(pathItems).forEach(([key, pathItem]) => {
Expand Down
10 changes: 2 additions & 8 deletions src/Converter/v3/components/RequestBodies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,9 @@ export const generateNamespace = (
): void => {
const basePath = "components/requestBodies";
store.addComponent("requestBodies", {
type: "namespace",
kind: "namespace",
name: Name.Components.RequestBodies,
value: factory.Namespace.create({
export: true,
name: Name.Components.RequestBodies,
statements: [],
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#componentsObject`,
}),
statements: {},
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#componentsObject`,
});

Object.entries(requestBodies).forEach(([name, requestBody]) => {
Expand Down
15 changes: 7 additions & 8 deletions src/Converter/v3/components/RequestBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ export const generateNamespace = (
): void => {
const basePath = `${parentName}/${name}`;
store.addStatement(basePath, {
type: "namespace",
kind: "namespace",
name,
value: factory.Namespace.create({
export: true,
name,
comment: requestBody.description,
statements: [generateInterface(entryPoint, currentPoint, factory, "Content", requestBody, context)],
}),
statements: {},
comment: requestBody.description,
});
store.addStatement(`${basePath}/Content`, {
kind: "interface",
name: "Content",
value: generateInterface(entryPoint, currentPoint, factory, "Content", requestBody, context),
});
};
49 changes: 14 additions & 35 deletions src/Converter/v3/components/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as path from "path";

import { Factory } from "../../../CodeGenerator";
import * as Name from "../Name";
import { Def, State, Store } from "../store";
import { Store } from "../store";
import * as ToTypeNode from "../toTypeNode";
import { OpenApi } from "../types";
import * as Header from "./Header";
Expand All @@ -20,28 +20,22 @@ export const generateNamespace = (
): void => {
const basePath = `${parentPath}/${name}`;
store.addStatement(basePath, {
type: "namespace",
kind: "namespace",
name,
value: factory.Namespace.create({
export: true,
name: name,
comment: response.description,
statements: [],
}),
statements: {},
comment: response.description,
});

if (response.headers) {
store.addStatement(`${basePath}/Header`, {
type: "interface",
kind: "interface",
name: Name.ComponentChild.Header,
value: Header.generateInterface(entryPoint, currentPoint, factory, Name.ComponentChild.Header, response.headers, context),
});
}

if (response.content) {
store.addStatement(`${basePath}/Content`, {
type: "interface",
kind: "interface",
name: Name.ComponentChild.Content,
value: MediaType.generateInterface(entryPoint, currentPoint, factory, Name.ComponentChild.Content, response.content, context),
});
Expand All @@ -61,42 +55,27 @@ export const generateReferenceNamespace = (
const basePath = `${parentPath}/${nameWithStatusCode}`;
const referenceNamespaceName = context.resolveReferencePath(currentPoint, responseReference.path).name;
store.addStatement(basePath, {
type: "namespace",
kind: "namespace",
name: nameWithStatusCode,
value: factory.Namespace.create({
export: true,
name: nameWithStatusCode,
statements: [],
}),
statements: {},
});
// TODO Type Guard
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

const headerNamespace = store.getStatement(path.join(responseReference.path, "Header"), "namespace") as
| Def.NamespaceStatement<State.A, State.B, State.C>
| undefined;
const headerNamespace = store.getStatement(path.join(responseReference.path, "Header"), "namespace");
if (headerNamespace) {
store.addStatement(`${basePath}/Header`, {
type: "namespace",
kind: "namespace",
name: Name.ComponentChild.Header,
value: factory.Namespace.create({
export: true,
name: Name.ComponentChild.Header,
statements: [],
}),
statements: {},
});
Object.values(headerNamespace.statements).forEach(statement => {
Object.values(headerNamespace.getChildren()).forEach(statement => {
if (!statement) {
return;
}
if (statement.type === "interface" || statement.type === "typeAlias") {
if (statement.kind === "interface" || statement.kind === "typeAlias") {
const aliasName = [referenceNamespaceName, Name.ComponentChild.Header, statement.name].join(".");
store.addStatement(`${basePath}/Header/${statement.value.name.text}`, {
type: "typeAlias",
store.addStatement(`${basePath}/Header/${statement.name}`, {
kind: "typeAlias",
name: aliasName,
value: factory.TypeAliasDeclaration.create({
export: true,
name: statement.value.name.text,
name: statement.name,
type: factory.TypeReferenceNode.create({
name: aliasName,
}),
Expand All @@ -106,7 +85,7 @@ export const generateReferenceNamespace = (
});
}
store.addStatement(`${basePath}/Content`, {
type: "typeAlias",
kind: "typeAlias",
name: Name.ComponentChild.Content,
value: factory.TypeAliasDeclaration.create({
export: true,
Expand Down
20 changes: 4 additions & 16 deletions src/Converter/v3/components/Responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,9 @@ export const generateNamespace = (
): void => {
const basePath = "components/responses";
store.addComponent("responses", {
type: "namespace",
kind: "namespace",
name: Name.Components.Responses,
value: factory.Namespace.create({
export: true,
name: Name.Components.Responses,
statements: [],
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#responsesObject`,
}),
statements: {},
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#responsesObject`,
});
Object.entries(responses).forEach(([name, response]) => {
if (Guard.isReference(response)) {
Expand Down Expand Up @@ -69,15 +63,9 @@ export const generateNamespaceWithStatusCode = (
): void => {
const basePath = `${parentPath}/responses`;
store.addStatement(basePath, {
type: "namespace",
kind: "namespace",
name: Name.ComponentChild.Response,
value: factory.Namespace.create({
export: true,
name: Name.ComponentChild.Response,
statements: [],
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#responsesObject`,
}),
statements: {},
comment: `@see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#responsesObject`,
});

Object.entries(responses).forEach(([statusCode, response]) => {
Expand Down
Loading