Skip to content

Commit

Permalink
Merge pull request #1185 from samchon/feature/enum
Browse files Browse the repository at this point in the history
Enhance `MetadataObject.isLiteral()` function.
  • Loading branch information
samchon authored Jul 30, 2024
2 parents 1668fe8 + d73a193 commit 8c34f45
Show file tree
Hide file tree
Showing 23 changed files with 221 additions and 159 deletions.
2 changes: 1 addition & 1 deletion benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@
"suppress-warnings": "^1.0.2",
"tstl": "^3.0.0",
"uuid": "^9.0.1",
"typia": "../typia-6.6.0.tgz"
"typia": "../typia-6.6.1.tgz"
}
}
2 changes: 1 addition & 1 deletion errors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
"typescript": "^5.3.2"
},
"dependencies": {
"typia": "../typia-6.6.0.tgz"
"typia": "../typia-6.6.1.tgz"
}
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typia",
"version": "6.6.0",
"version": "6.6.1",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -67,7 +67,7 @@
},
"homepage": "https://typia.io",
"dependencies": {
"@samchon/openapi": "^0.4.2",
"@samchon/openapi": "^0.4.3",
"commander": "^10.0.0",
"comment-json": "^4.2.3",
"inquirer": "^8.2.5",
Expand All @@ -92,7 +92,7 @@
"rollup": "^4.18.0",
"suppress-warnings": "^1.0.2",
"ts-node": "^10.9.2",
"typescript": "^5.5.3"
"typescript": "^5.5.4"
},
"stackblitz": {
"startCommand": "npm install && npm run test"
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": "6.6.0",
"version": "6.6.1",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -63,7 +63,7 @@
},
"homepage": "https://typia.io",
"dependencies": {
"typia": "6.6.0"
"typia": "6.6.1"
},
"peerDependencies": {
"typescript": ">=4.8.0 <5.6.0"
Expand Down
2 changes: 1 addition & 1 deletion src/programmers/IsProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export namespace IsProgrammer {
// ONLY WHEN OBJECT WITH SOME ATOMIC PROPERTIES
const obj: MetadataObject = target.objects[0]!;
if (
obj._Is_simple(explore.from === "top" ? 0 : 1) &&
obj.isPlain(explore.from === "top" ? 0 : 1) &&
(equals === false ||
OptionPredicator.undefined(project.options) === false)
)
Expand Down
2 changes: 1 addition & 1 deletion src/programmers/internal/application_escaped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const application_escaped =
const string: StringSchema<Version> | undefined = is_string(output)
? output
: is_one_of(output)
? output.oneOf.find(is_string)
? (output.oneOf.find(is_string) as StringSchema<Version>)
: undefined;
if (
string !== undefined &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const application_union_discriminator = (
if (
meta.size() === 0 ||
meta.size() !== meta.objects.length ||
meta.objects.some((o) => o._Is_literal()) === true
meta.objects.some((o) => o.isLiteral()) === true
)
return undefined;
const specialized: UnionPredicator.ISpecialized[] = UnionPredicator.object(
Expand Down
2 changes: 1 addition & 1 deletion src/programmers/internal/application_v30_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const application_v30_object =
(
nullable: boolean,
): OpenApiV3.IJsonSchema.IReference | OpenApiV3.IJsonSchema.IObject => {
if (obj._Is_literal() === true)
if (obj.isLiteral() === true)
return create_object_schema(components)(obj)(nullable);

const key: string = `${obj.name}${nullable ? ".Nullable" : ""}`;
Expand Down
3 changes: 1 addition & 2 deletions src/programmers/internal/application_v31_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ export const application_v31_object =
(
obj: MetadataObject,
): OpenApi.IJsonSchema.IReference | OpenApi.IJsonSchema.IObject => {
if (obj._Is_literal() === true)
return create_object_schema(components)(obj);
if (obj.isLiteral() === true) return create_object_schema(components)(obj);

const key: string = obj.name;
const $ref: string = `#/components/schemas/${key}`;
Expand Down
21 changes: 12 additions & 9 deletions src/programmers/internal/application_v31_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,30 @@ export const application_v31_schema =
if (meta.escaped !== null)
application_escaped(application_v31_schema(false)(components)({}))(
meta.escaped,
).forEach(insert);
).forEach(insert as any);

// ATOMIC TYPES
if (meta.templates.length && AtomicPredicator.template(meta))
application_templates(meta).map(insert);
application_templates(meta).map(insert as any);
for (const constant of meta.constants)
if (constant.type === "bigint") throw new TypeError(NO_BIGINT);
else if (AtomicPredicator.constant(meta)(constant.type) === false)
continue;
else application_v31_constant(constant).map(insert);
for (const a of meta.atomics)
if (a.type === "bigint") throw new TypeError(NO_BIGINT);
else if (a.type === "boolean") application_boolean(a).forEach(insert);
else if (a.type === "number") application_number(a).forEach(insert);
else if (a.type === "string") application_string(a).forEach(insert);
else if (a.type === "boolean")
application_boolean(a).forEach(insert as any);
else if (a.type === "number")
application_number(a).forEach(insert as any);
else if (a.type === "string")
application_string(a).forEach(insert as any);

// ARRAY
for (const array of meta.arrays)
application_array(application_v31_schema(false)(components)({}))(
components,
)(array).forEach(insert);
)(array).forEach(insert as any);

// TUPLE
for (const tuple of meta.tuples)
Expand All @@ -94,7 +97,7 @@ export const application_v31_schema =
type: "boolean",
tags: [],
}),
)[0]!,
)[0]! as any,
);
else if (type === "number")
insert(
Expand All @@ -103,7 +106,7 @@ export const application_v31_schema =
type: "number",
tags: [],
}),
)[0]!,
)[0]! as any,
);
else if (type === "string")
insert(
Expand All @@ -112,7 +115,7 @@ export const application_v31_schema =
type: "string",
tags: [],
}),
)[0]!,
)[0]! as any,
);
} else insert(application_v31_native(components)(native));
if (meta.sets.length) insert(application_v31_native(components)(`Set`));
Expand Down
2 changes: 1 addition & 1 deletion src/programmers/json/JsonStringifyProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ export namespace JsonStringifyProgrammer {
value: () =>
meta.isParentResolved() === false &&
meta.objects.length === 1 &&
meta.objects[0]!._Is_simple(explore.from === "top" ? 0 : 1)
meta.objects[0]!.isPlain(explore.from === "top" ? 0 : 1)
? (() => {
const obj: MetadataObject = meta.objects[0]!;
const entries: IExpressionEntry<ts.Expression>[] =
Expand Down
4 changes: 4 additions & 0 deletions src/schemas/metadata/MetadataConstantValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export class MetadataConstantValue {
return MetadataConstantValue.create({
value: typeof json.value === "bigint" ? BigInt(json.value) : json.value,
tags: json.tags,
description: json.description,
jsDocTags: json.jsDocTags,
});
}

Expand All @@ -40,6 +42,8 @@ export class MetadataConstantValue {
value:
typeof this.value === "bigint" ? this.value.toString() : this.value,
tags: this.tags,
description: this.description,
jsDocTags: this.jsDocTags,
};
}
}
Expand Down
35 changes: 10 additions & 25 deletions src/schemas/metadata/MetadataObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,7 @@ export class MetadataObject {
});
}

/**
* @internal
*/
public _Messagable(): boolean {
return (
this.properties.length >= 1 &&
this.properties.every((p) => p.key.isSoleLiteral())
);
}

/**
* @internal
*/
public _Is_simple(level: number = 0): boolean {
public isPlain(level: number = 0): boolean {
return (
this.recursive === false &&
this.properties.length < 10 &&
Expand All @@ -97,23 +84,21 @@ export class MetadataObject {
(property.value.atomics.length === 1 ||
(level < 1 &&
property.value.objects.length === 1 &&
property.value.objects[0]!._Is_simple(level + 1))),
property.value.objects[0]!.isPlain(level + 1))),
)
);
}

/**
* @internal
*/
public _Is_literal(): boolean {
public isLiteral(): boolean {
return (this.literal_ ??= (() => {
if (this.recursive === true) return false;
else if (this.name === "__type") return true;
else if (this.name.startsWith("__type.o") === false) return false;

const last: string = this.name.substr("__type.o".length);
const value: number = Number(last);
return isNaN(value) === false && Number.isInteger(value);
return (
this.name === "__type" ||
this.name === "__object" ||
this.name.startsWith("__type.") ||
this.name.startsWith("__object.") ||
this.name.includes("readonly [")
);
})());
}

Expand Down
2 changes: 1 addition & 1 deletion test-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@
"typescript": "^5.4.5"
},
"dependencies": {
"typia": "../typia-6.6.0.tgz"
"typia": "../typia-6.6.1.tgz"
}
}
2 changes: 1 addition & 1 deletion test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@
"suppress-warnings": "^1.0.2",
"tstl": "^3.0.0",
"uuid": "^9.0.1",
"typia": "../typia-6.6.0.tgz"
"typia": "../typia-6.6.1.tgz"
}
}
44 changes: 19 additions & 25 deletions test/schemas/json/v3_0/ObjectLiteralType.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
{
"version": "3.0",
"components": {
"schemas": {
"__object": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"age": {
"type": "number"
}
},
"nullable": false,
"required": [
"id",
"name",
"age"
]
}
}
},
"components": {},
"schemas": [
{
"$ref": "#/components/schemas/__object"
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"age": {
"type": "number"
}
},
"nullable": false,
"required": [
"id",
"name",
"age"
]
}
]
}
Loading

0 comments on commit 8c34f45

Please sign in to comment.