Skip to content

Commit

Permalink
Merge pull request #1154 from samchon/feat/tags
Browse files Browse the repository at this point in the history
Fix #1149: clone mode with custom validator tag case.
  • Loading branch information
samchon authored Dec 14, 2024
2 parents cf07564 + 720c41a commit 0462414
Show file tree
Hide file tree
Showing 40 changed files with 1,246 additions and 53 deletions.
2 changes: 1 addition & 1 deletion benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"reflect-metadata": "^0.2.2",
"tgrid": "^1.1.0",
"tstl": "^3.0.0",
"typia": "^7.2.0"
"typia": "^7.3.0"
},
"devDependencies": {
"@types/autocannon": "^7.9.0",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@nestia/station",
"version": "4.2.0",
"version": "4.3.0",
"description": "Nestia station",
"scripts": {
"build": "node deploy build",
Expand Down
2 changes: 1 addition & 1 deletion packages/benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"ts-patch": "^3.3.0",
"typescript": "~5.7.2",
"typescript-transform-paths": "^3.4.7",
"typia": "^7.2.0",
"typia": "^7.3.0",
"uuid": "^10.0.0"
},
"dependencies": {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/core",
"version": "4.2.0-dev.20241211-2",
"version": "4.3.0-dev.20241215-2",
"description": "Super-fast validation decorators of NestJS",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -48,11 +48,11 @@
"reflect-metadata": ">=0.1.12",
"rxjs": ">=6.0.3",
"tgrid": "^1.1.0",
"typia": "^7.2.0",
"typia": "^7.3.0",
"ws": "^7.5.3"
},
"peerDependencies": {
"@nestia/fetcher": ">=4.2.0-dev.20241211-2",
"@nestia/fetcher": ">=4.3.0-dev.20241215-2",
"@nestjs/common": ">=7.0.1",
"@nestjs/core": ">=7.0.1",
"reflect-metadata": ">=0.1.12",
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"ts-patch": "^3.3.0",
"typescript": "~5.7.2",
"typescript-transform-paths": "^3.4.7",
"typia": "^7.2.0"
"typia": "^7.3.0"
},
"files": [
"lib",
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/editor",
"version": "4.2.0-dev.20241211-2",
"version": "4.3.0-dev.20241215-2",
"typings": "lib/index.d.ts",
"main": "lib/index.js",
"module": "lib/index.mjs",
Expand Down Expand Up @@ -40,7 +40,7 @@
"js-yaml": "^4.1.0",
"prettier": "3.3.3",
"react-mui-fileuploader": "^0.5.2",
"typia": "^7.2.0"
"typia": "^7.3.0"
},
"devDependencies": {
"@eslint/js": "^9.13.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/fetcher/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/fetcher",
"version": "4.2.0-dev.20241211-2",
"version": "4.3.0-dev.20241215-2",
"description": "Fetcher library of Nestia SDK",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand All @@ -27,7 +27,7 @@
"homepage": "https://nestia.io",
"dependencies": {
"@samchon/openapi": "^2.0.1",
"typia": "^7.2.0"
"typia": "^7.3.0"
},
"peerDependencies": {
"typescript": ">= 4.8.0"
Expand All @@ -38,7 +38,7 @@
"@typescript-eslint/parser": "^5.46.1",
"rimraf": "^3.0.2",
"typescript": "~5.7.2",
"typia": "^7.2.0"
"typia": "^7.3.0"
},
"files": [
"README.md",
Expand Down
4 changes: 2 additions & 2 deletions packages/migrate/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/migrate",
"version": "4.2.0-dev.20241211-2",
"version": "4.3.0-dev.20241215-2",
"description": "Migration program from swagger to NestJS",
"typings": "lib/index.d.ts",
"main": "lib/index.js",
Expand Down Expand Up @@ -78,7 +78,7 @@
"prettier": "^3.2.5",
"tstl": "^3.0.0",
"typescript": "~5.7.2",
"typia": "^7.2.0"
"typia": "^7.3.0"
},
"files": [
"lib",
Expand Down
8 changes: 4 additions & 4 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/sdk",
"version": "4.2.0-dev.20241211-2",
"version": "4.3.0-dev.20241215-2",
"description": "Nestia SDK and Swagger generator",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -44,11 +44,11 @@
"tsconfck": "^2.1.2",
"tsconfig-paths": "^4.1.1",
"tstl": "^3.0.0",
"typia": "^7.2.0"
"typia": "^7.3.0"
},
"peerDependencies": {
"@nestia/core": ">=4.2.0-dev.20241211-2",
"@nestia/fetcher": ">=4.2.0-dev.20241211-2",
"@nestia/core": ">=4.3.0-dev.20241215-2",
"@nestia/fetcher": ">=4.3.0-dev.20241215-2",
"@nestjs/common": ">=7.0.1",
"@nestjs/core": ">=7.0.1",
"reflect-metadata": ">=0.1.12",
Expand Down
49 changes: 16 additions & 33 deletions packages/sdk/src/generates/internal/SdkTypeProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { Escaper } from "typia/lib/utils/Escaper";
import { INestiaProject } from "../../structures/INestiaProject";
import { FilePrinter } from "./FilePrinter";
import { ImportDictionary } from "./ImportDictionary";
import { SdkTypeTagProgrammer } from "./SdkTypeTagProgrammer";

export namespace SdkTypeProgrammer {
/* -----------------------------------------------------------
Expand Down Expand Up @@ -100,7 +101,7 @@ export namespace SdkTypeProgrammer {
)
return ts.factory.createIntersectionTypeNode([
TypeFactory.keyword("string"),
writeTag(importer)({
SdkTypeTagProgrammer.write(importer, "string", {
name: "Format",
value: "date-time",
} as IMetadataTypeTag),
Expand Down Expand Up @@ -184,6 +185,7 @@ export namespace SdkTypeProgrammer {
(importer: ImportDictionary) =>
(meta: MetadataAtomic): ts.TypeNode =>
write_type_tag_matrix(importer)(
meta.type as "boolean" | "bigint" | "number" | "string",
ts.factory.createKeywordTypeNode(
meta.type === "boolean"
? ts.SyntaxKind.BooleanKeyword
Expand All @@ -204,6 +206,7 @@ export namespace SdkTypeProgrammer {
(importer: ImportDictionary) =>
(meta: MetadataArray): ts.TypeNode =>
write_type_tag_matrix(importer)(
"array",
ts.factory.createArrayTypeNode(
write(project)(importer)(meta.type.value),
),
Expand Down Expand Up @@ -298,22 +301,30 @@ export namespace SdkTypeProgrammer {
----------------------------------------------------------- */
const write_type_tag_matrix =
(importer: ImportDictionary) =>
(base: ts.TypeNode, matrix: IMetadataTypeTag[][]): ts.TypeNode => {
(
from: "array" | "boolean" | "number" | "bigint" | "string" | "object",
base: ts.TypeNode,
matrix: IMetadataTypeTag[][],
): ts.TypeNode => {
matrix = matrix.filter((row) => row.length !== 0);
if (matrix.length === 0) return base;
else if (matrix.length === 1)
return ts.factory.createIntersectionTypeNode([
base,
...matrix[0].map((tag) => writeTag(importer)(tag)),
...matrix[0].map((tag) =>
SdkTypeTagProgrammer.write(importer, from, tag),
),
]);
return ts.factory.createIntersectionTypeNode([
base,
ts.factory.createUnionTypeNode(
matrix.map((row) =>
row.length === 1
? writeTag(importer)(row[0])
? SdkTypeTagProgrammer.write(importer, from, row[0])
: ts.factory.createIntersectionTypeNode(
row.map((tag) => writeTag(importer)(tag)),
row.map((tag) =>
SdkTypeTagProgrammer.write(importer, from, tag),
),
),
),
),
Expand All @@ -322,34 +333,6 @@ export namespace SdkTypeProgrammer {
}

const writeNode = (text: string) => ts.factory.createTypeReferenceNode(text);
const writeTag = (importer: ImportDictionary) => (tag: IMetadataTypeTag) => {
const instance: string = tag.name.split("<")[0];
return ts.factory.createTypeReferenceNode(
importer.external({
type: true,
library: `typia/lib/tags/${instance}`,
instance,
}),
[
ts.factory.createLiteralTypeNode(
typeof tag.value === "boolean"
? tag.value
? ts.factory.createTrue()
: ts.factory.createFalse()
: typeof tag.value === "bigint"
? tag.value < BigInt(0)
? ts.factory.createPrefixUnaryExpression(
ts.SyntaxKind.MinusToken,
ts.factory.createBigIntLiteral((-tag.value).toString()),
)
: ts.factory.createBigIntLiteral(tag.value.toString())
: typeof tag.value === "number"
? ExpressionFactory.number(tag.value)
: ts.factory.createStringLiteral(tag.value),
),
],
);
};
const writeComment =
(atomics: MetadataAtomic[]) =>
(description: string | null, jsDocTags: IJsDocTagInfo[]): string => {
Expand Down
120 changes: 120 additions & 0 deletions packages/sdk/src/generates/internal/SdkTypeTagProgrammer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import ts from "typescript";
import { ExpressionFactory } from "typia/lib/factories/ExpressionFactory";
import { LiteralFactory } from "typia/lib/factories/LiteralFactory";
import { IMetadataTypeTag } from "typia/lib/schemas/metadata/IMetadataTypeTag";

import { ImportDictionary } from "./ImportDictionary";

export namespace SdkTypeTagProgrammer {
export const write = (
importer: ImportDictionary,
from: "object" | "array" | "boolean" | "number" | "bigint" | "string",
tag: IMetadataTypeTag,
) => {
const instance: string = tag.name.split("<")[0];
if (PREDEFINED[from]?.has(instance) === true)
return ts.factory.createTypeReferenceNode(
importer.external({
type: true,
library: `typia/lib/tags/${instance}`,
instance,
}),
instance === "Example"
? []
: [
ts.factory.createLiteralTypeNode(
typeof tag.value === "boolean"
? tag.value
? ts.factory.createTrue()
: ts.factory.createFalse()
: typeof tag.value === "bigint"
? tag.value < BigInt(0)
? ts.factory.createPrefixUnaryExpression(
ts.SyntaxKind.MinusToken,
ts.factory.createBigIntLiteral(
(-tag.value).toString(),
),
)
: ts.factory.createBigIntLiteral(tag.value.toString())
: typeof tag.value === "number"
? ExpressionFactory.number(tag.value)
: ts.factory.createStringLiteral(tag.value),
),
],
);
return ts.factory.createTypeReferenceNode(
importer.external({
type: true,
library: `typia/lib/tags/TagBase`,
instance: "TagBase",
}),
[
ts.factory.createLiteralTypeNode(
LiteralFactory.write({
target: from,
kind: tag.kind,
value: tag.value,
validate: tag.validate,
exclusive: tag.exclusive,
schema: tag.schema,
}) as any,
),
],
);
};
}

const COMMON_KINDS = ["Default", "Example", "Examples", "Sequence"];
const PREDEFINED = {
object: new Set([...COMMON_KINDS]),
array: new Set([...COMMON_KINDS, "MinItems", "MaxItems", "UniqueItems"]),
boolean: new Set([...COMMON_KINDS]),
number: new Set([
...COMMON_KINDS,
"Minimum",
"Maximum",
"ExclusiveMinimum",
"ExclusiveMaximum",
"MultipleOf",
"Type",
]),
bigint: new Set([
...COMMON_KINDS,
"Minimum",
"Maximum",
"ExclusiveMinimum",
"ExclusiveMaximum",
"MultipleOf",
"Type",
]),
string: new Set([
...COMMON_KINDS,
"ContentMediaType",
"Format",
"MaxLength",
"MinLength",
"Pattern",
]),
};

// export * from "./Constant";
// export * from "./ContentMediaType";
// export * from "./Default";
// export * from "./Example";
// export * from "./Examples";
// export * from "./ExclusiveMaximum";
// export * from "./ExclusiveMinimum";
// export * from "./Format";
// export * from "./JsonSchemaPlugin";
// export * from "./Maximum";
// export * from "./MaxItems";
// export * from "./MaxLength";
// export * from "./Minimum";
// export * from "./MinItems";
// export * from "./MinLength";
// export * from "./MultipleOf";
// export * from "./Pattern";
// export * from "./Sequence";
// export * from "./TagBase";
// export * from "./Type";
// export * from "./UniqueItems";
Empty file.
Loading

0 comments on commit 0462414

Please sign in to comment.