Skip to content

Commit

Permalink
Support much more type comment tags.
Browse files Browse the repository at this point in the history
From v4.3 update, below type comment tags being supported. Also, in the `bigint` case, it can use `@type uint64` comment tag for `unsigned bigint` restriction.

For reference, such type comment tags are designed to prepare the next v5 update, Protocol Buffer supporting.

  - `@type {string}`
    - `int`: `Math.floor(x) === x`
    - `uint`: `Math.floor(x) === x && x >= 0`
      - `int32`: `-2^31 <= x <= 2^31 - 1`
      - `uint32`: `0 <= x <= 2^32 - 1`
      - `uint64`: `0 <= x <= 2^64 - 1`
      - `int64`: `-2^63 <= x <= 2^63 - 1`
    - `float`: `-1.175494351e38 <= x <= 3.4028235e38`
  • Loading branch information
samchon committed Aug 20, 2023
1 parent ef8b97a commit b98c489
Show file tree
Hide file tree
Showing 3,155 changed files with 27,967 additions and 21,785 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Check out the document in the [website](https://typia.io/docs/):
- [`is()` function](https://typia.io/docs/validators/is/)
- [`assert()` function](https://typia.io/docs/validators/assert/)
- [`validate()` function](https://typia.io/docs/validators/validate/)
- [Comment Tags](https://typia.io/docs/validators/comment-tags/)
- [Comment Tags](https://typia.io/docs/validators/tags/)
- Enhanced JSON
- [`stringify()` functions](https://typia.io/docs/json/stringify/)
- [`parse()` functions](https://typia.io/docs/json/parse/)
Expand Down
11 changes: 11 additions & 0 deletions build/internal/TestFeature.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface TestFeature {
module?: string;
method: string;
creatable: boolean;
spoilable: boolean;
Expand All @@ -7,6 +8,10 @@ export interface TestFeature {
explicit?: true;
primitive?: true;
random?: true;
opposite?: Array<{
name: string;
method: string;
}>;
}
export namespace TestFeature {
export const DATA: TestFeature[] = [
Expand Down Expand Up @@ -158,6 +163,12 @@ export namespace TestFeature {
spoilable: false,
primitive: true,
random: true,
opposite: [
{
name: "assert",
method: "typia.createAssert",
},
],
},
];
}
4 changes: 1 addition & 3 deletions build/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ function script(
? ` () => typia.random<${struct.name}>(${
struct.RANDOM ? `${struct.name}.RANDOM` : ""
}),`
: ` (input) => typia.${method}${
feat.explicit ? `<${struct.name}>` : ""
}(input),`,
: ` (input) => typia.${method}<${struct.name}>(input),`,
feat.spoilable && struct.SPOILERS
? ` ${struct.name}.SPOILERS,`
: null,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typia",
"version": "4.2.3",
"version": "4.3.0",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-json/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Check out the document in the [website](https://typia.io/docs/):
- [`is()` function](https://typia.io/docs/validators/is/)
- [`assert()` function](https://typia.io/docs/validators/assert/)
- [`validate()` function](https://typia.io/docs/validators/validate/)
- [Comment Tags](https://typia.io/docs/validators/comment-tags/)
- [Comment Tags](https://typia.io/docs/validators/tags/)
- Enhanced JSON
- [`stringify()` functions](https://typia.io/docs/json/stringify/)
- [`parse()` functions](https://typia.io/docs/json/parse/)
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript-json/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-json",
"version": "4.2.3",
"version": "4.3.0",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -68,7 +68,7 @@
},
"homepage": "https://typia.io",
"dependencies": {
"typia": "4.2.3"
"typia": "4.3.0"
},
"peerDependencies": {
"typescript": ">= 4.7.4"
Expand Down
7 changes: 7 additions & 0 deletions src/factories/ExpressionFactory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import ts from "typescript";

export namespace ExpressionFactory {
export const bigint = (value: number) =>
ts.factory.createCallExpression(
ts.factory.createIdentifier("BigInt"),
undefined,
[ts.factory.createNumericLiteral(value)],
);

export const isRequired = (input: ts.Expression): ts.Expression =>
ts.factory.createStrictInequality(
ts.factory.createIdentifier("undefined"),
Expand Down
14 changes: 10 additions & 4 deletions src/factories/MetadataTagFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,17 @@ export namespace MetadataTagFactory {
NUMBER
----------------------------------------------------------- */
type: (_identifier, metadata, text, _output) => {
if (text.startsWith("{") && text.endsWith("}"))
text = text.substring(1, text.length - 1);
return has_atomic("number")(new Set())(metadata) &&
(text === "int" || text === "uint")
(text === "int" ||
text === "uint" ||
text === "int32" ||
text === "uint32" ||
text === "int64" ||
text === "uint64" ||
text === "float")
? { kind: "type", value: text }
: text === "{int}" || text === "{uint}"
? { kind: "type", value: text.slice(1, -1) as "int" | "uint" }
: null;
},
minimum: (identifier, metadata, text, output) => {
Expand Down Expand Up @@ -291,7 +297,7 @@ const validate = (

// @todo: must block repeated array and tuple type
const has_atomic =
(type: "string" | "number") =>
(type: "string" | "number" | "bigint") =>
(visited: Set<Metadata>) =>
(metadata: Metadata): boolean => {
if (visited.has(metadata)) return false;
Expand Down
9 changes: 8 additions & 1 deletion src/metadata/IMetadataTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ export namespace IMetadataTag {
----------------------------------------------------------- */
export interface IType {
kind: "type";
value: "int" | "uint";
value:
| "int"
| "uint"
| "int32"
| "uint32"
| "int64"
| "uint64"
| "float";
}

export interface IMinimum {
Expand Down
5 changes: 5 additions & 0 deletions src/metadata/MetadataTuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export class MetadataTuple {
public readonly recursive: boolean;
public readonly nullables: boolean[];

/**
* @internal
*/
public of_map?: boolean;

/**
* @internal
*/
Expand Down
8 changes: 4 additions & 4 deletions src/programmers/CheckerProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,8 @@ export namespace CheckerProgrammer {
...explore,
from: "array",
},
[],
[],
metaTags,
jsDocTags,
),
);
}
Expand Down Expand Up @@ -775,8 +775,8 @@ export namespace CheckerProgrammer {
? `${explore.postfix.slice(0, -1)}[${index}]"`
: `"[${index}]"`,
},
tagList,
jsDocTags,
tuple.of_map === true && index !== 1 ? [] : tagList,
tuple.of_map === true && index !== 1 ? [] : jsDocTags,
),
);
const rest: ts.Expression | null =
Expand Down
3 changes: 2 additions & 1 deletion src/programmers/LiteralsProgrammer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ts from "typescript";

import { ExpressionFactory } from "../factories/ExpressionFactory";
import { MetadataCollection } from "../factories/MetadataCollection";
import { MetadataFactory } from "../factories/MetadataFactory";

Expand Down Expand Up @@ -47,7 +48,7 @@ export namespace LiteralsProgrammer {
: typeof v === "number"
? ts.factory.createNumericLiteral(v)
: typeof v === "bigint"
? ts.factory.createBigIntLiteral(v.toString())
? ExpressionFactory.bigint(Number(v))
: ts.factory.createStringLiteral(v),
),
true,
Expand Down
37 changes: 18 additions & 19 deletions src/programmers/RandomProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export namespace RandomProgrammer {
? ts.factory.createNumericLiteral(value)
: typeof value === "string"
? ts.factory.createStringLiteral(value)
: ts.factory.createBigIntLiteral(value.toString());
: ExpressionFactory.bigint(Number(value));

const decode_template =
(importer: FunctionImporter) =>
Expand All @@ -332,10 +332,20 @@ export namespace RandomProgrammer {
(tags: IMetadataTag[]) =>
(comments: ICommentTag[]): ts.Expression => {
const type = tags.find(
(t) => t.kind === "type" && t.value === "uint",
(t) =>
t.kind === "type" &&
(t.value === "int" ||
t.value === "int32" ||
t.value === "int64"),
)
? "int"
: tags.find((t) => t.kind === "type" && t.value === "int")
: tags.find(
(t) =>
t.kind === "type" &&
(t.value === "uint" ||
t.value === "uint32" ||
t.value === "uint64"),
)
? "uint"
: "double";
return random_custom(COALESCE(importer))("number")(comments)(
Expand Down Expand Up @@ -373,30 +383,19 @@ export namespace RandomProgrammer {
random_custom(COALESCE(importer))("bigint")(comments)(
RandomRanger.number({
type: tags.find(
(t) => t.kind === "type" && t.value === "uint",
(t) =>
t.kind === "type" &&
(t.value === "uint" || t.value === "uint64"),
)
? "uint"
: "int",
transform: (value) =>
ts.factory.createCallExpression(
ts.factory.createIdentifier("BigInt"),
undefined,
[ts.factory.createStringLiteral(value.toString())],
),
transform: (value) => ExpressionFactory.bigint(value),
setter: (args) =>
ts.factory.createCallExpression(
COALESCE(importer)("bigint"),
undefined,
args.map((value) =>
ts.factory.createCallExpression(
ts.factory.createIdentifier("BigInt"),
undefined,
[
ts.factory.createStringLiteral(
value.toString(),
),
],
),
ExpressionFactory.bigint(value),
),
),
})({
Expand Down
12 changes: 2 additions & 10 deletions src/programmers/helpers/RandomRanger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,7 @@ export namespace RandomRanger {
//----
// INT
const integer = (value: number) => value === Math.floor(value);
if (
tags.find(
(t) => t.kind === "type" && t.value.indexOf("int") !== -1,
) !== undefined
) {
if (config.type === "int") {
if (range.minimum.value !== undefined) {
if (range.minimum.exclusive) {
range.minimum.exclusive = false;
Expand All @@ -125,11 +121,7 @@ export namespace RandomRanger {
}

// UNSIGNED INT
if (
tags.find(
(t) => t.kind === "type" && t.value.indexOf("uint") === 0,
) !== undefined
) {
if (config.type === "uint") {
if (range.minimum.value === undefined) range.minimum.value = 0;
else if (range.minimum.value <= 0) {
range.minimum.value = 0;
Expand Down
18 changes: 11 additions & 7 deletions src/programmers/helpers/UnionExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,17 @@ export namespace UnionExplorer {
value: Metadata.create({
...Metadata.initialize(),
tuples: [
MetadataTuple.create({
name: `[${m.key.getName()}, ${m.value.getName()}]`,
index: null,
recursive: false,
nullables: [],
elements: [m.key, m.value],
}),
(() => {
const tuple = MetadataTuple.create({
name: `[${m.key.getName()}, ${m.value.getName()}]`,
index: null,
recursive: false,
nullables: [],
elements: [m.key, m.value],
});
tuple.of_map = true;
return tuple;
})(),
],
}),
}),
Expand Down
12 changes: 9 additions & 3 deletions src/programmers/internal/application_number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ export const application_number = (
tag.kind === "type" &&
(tag.value === "int" ||
tag.value === "uint" ||
tag.value === "{int}" ||
tag.value === "{uint}")
tag.value === "int32" ||
tag.value === "uint32" ||
tag.value === "int64" ||
tag.value === "uint64")
)
output.type = "integer";
// RANGE TAG
Expand All @@ -39,7 +41,11 @@ export const application_number = (
if (
output.type === "integer" &&
(attribute["x-typia-metaTags"] ?? []).find(
(tag) => tag.kind === "type" && tag.value === "uint",
(tag) =>
tag.kind === "type" &&
(tag.value === "uint" ||
tag.value === "uint32" ||
tag.value === "uint64"),
)
)
if (
Expand Down
Loading

0 comments on commit b98c489

Please sign in to comment.