-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #860 from samchon/features/plugin
Adapt `typia`s `JsonSchemaPlugin` tag in `@nestia/editor` .
- Loading branch information
Showing
7 changed files
with
115 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import ts from "typescript"; | ||
import { Escaper } from "typia/lib/utils/Escaper"; | ||
|
||
export namespace TypeLiteralFactory { | ||
export const generate = (value: any): ts.TypeNode => | ||
typeof value === "boolean" | ||
? generateBoolean(value) | ||
: typeof value === "number" | ||
? generateNumber(value) | ||
: typeof value === "string" | ||
? generatestring(value) | ||
: typeof value === "object" | ||
? value === null | ||
? generateNull() | ||
: Array.isArray(value) | ||
? generateTuple(value) | ||
: generateObject(value) | ||
: ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword); | ||
|
||
const generatestring = (str: string) => | ||
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(str)); | ||
|
||
const generateNumber = (num: number) => | ||
ts.factory.createLiteralTypeNode(ts.factory.createNumericLiteral(num)); | ||
|
||
const generateBoolean = (bool: boolean) => | ||
ts.factory.createLiteralTypeNode( | ||
bool ? ts.factory.createTrue() : ts.factory.createFalse(), | ||
); | ||
|
||
const generateNull = () => | ||
ts.factory.createLiteralTypeNode(ts.factory.createNull()); | ||
|
||
const generateTuple = (items: any[]) => | ||
ts.factory.createTupleTypeNode(items.map(generate)); | ||
|
||
const generateObject = (obj: object) => | ||
ts.factory.createTypeLiteralNode( | ||
Object.entries(obj).map(([key, value]) => | ||
ts.factory.createPropertySignature( | ||
undefined, | ||
Escaper.variable(key) | ||
? ts.factory.createIdentifier(key) | ||
: ts.factory.createStringLiteral(key), | ||
undefined, | ||
generate(value), | ||
), | ||
), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters