Skip to content

Commit

Permalink
Merge pull request #756 from samchon/features/message
Browse files Browse the repository at this point in the history
Enhanced `typia.protobuf.message<T>()` function.
  • Loading branch information
samchon authored Aug 11, 2023
2 parents 573cea2 + 1e3baf1 commit 7a99d20
Show file tree
Hide file tree
Showing 314 changed files with 26,850 additions and 2,110 deletions.
4 changes: 1 addition & 3 deletions build/internal/TestMessageGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ export namespace TestMessageGenerator {
"",
`export const test_protobuf_message_${s.name} = _test_protobuf_message(`,
` "${s.name}",`,
`)(`,
` typia.protobuf.message<${s.name}>(),`,
`);`,
`)(typia.protobuf.message<${s.name}>());`,
];
await fs.promises.writeFile(
`${__dirname}/../../test/features/protobuf.message/test_protobuf_message_${s.name}.ts`,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typia",
"version": "5.0.0-dev.20230810",
"version": "5.0.0-dev.20230811",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -113,6 +113,7 @@
"jsdom": "^21.1.1",
"physical-cpu-count": "^2.0.0",
"prettier": "^2.6.2",
"protobufjs": "^7.2.4",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"sloc": "^0.2.1",
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": "5.0.0-dev.20230810",
"version": "5.0.0-dev.20230811",
"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": "5.0.0-dev.20230810"
"typia": "5.0.0-dev.20230811"
},
"peerDependencies": {
"typescript": ">= 4.7.4"
Expand Down
79 changes: 54 additions & 25 deletions src/factories/ProtobufFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Metadata } from "../schemas/metadata/Metadata";
import { MetadataObject } from "../schemas/metadata/MetadataObject";
import { IProtocolMessage } from "../schemas/protobuf/IProtocolMessage";

import { Escaper } from "../utils/Escaper";

import { MetadataCollection } from "./MetadataCollection";
import { MetadataFactory } from "./MetadataFactory";
import { emplace_protocol_object } from "./internal/protocols/emplace_protocol_object";
Expand Down Expand Up @@ -98,6 +100,20 @@ export namespace ProtobufFactory {
throw notSupportedError({ method })(
"object type with mixed static and dynamic key typed properties. Keep statics or dynamic only.",
);
// STATIC PROPERTY, BUT INVALID KEY NAME
else if (
meta.objects.length &&
meta.objects.some((obj) =>
obj.properties.some(
(p) =>
p.key.isSoleLiteral() &&
!Escaper.variable(p.key.getSoleLiteral()!),
),
)
)
throw notSupportedError({ method })(
`object type with invalid static key name.`,
);
// NATIVE TYPE, BUT NOT Uint8Array
else if (meta.natives.length) {
const banned = meta.natives
Expand All @@ -111,11 +127,24 @@ export namespace ProtobufFactory {
);
}
// MAP TYPE, BUT PROPERTY KEY TYPE IS NOT ATOMIC
else if (meta.maps.length && meta.maps.some((m) => !isAtomicKey(m.key)))
throw notSupportedError({ method })("");
// MAP TYPE, BUT VALUE TYPE IS ARRAY
else if (
meta.maps.length &&
meta.maps.some((m) => !is_atomic_key(m.key))
meta.maps.some((m) => !!m.value.arrays.length)
)
throw notSupportedError({ method })("");
throw notSupportedError({ method })(
"map type with array value type",
);
else if (
meta.objects.length &&
isDynamicObject(meta.objects[0]!) &&
meta.objects[0]!.properties.some((p) => !!p.value.arrays.length)
)
throw notSupportedError({ method })(
"dynamic object with array value type",
);
//----
// UNION TYPES
//----
Expand Down Expand Up @@ -156,29 +185,6 @@ export namespace ProtobufFactory {
else if (meta.maps.length && meta.maps.some((m) => isUnion(m.value)))
throw notSupportedError({ method })("union type in map");
};

const is_atomic_key = (key: Metadata) => {
if (
key.required &&
key.nullable === false &&
key.functional === false &&
key.resolved === null &&
key.size() ===
key.atomics.length +
key.constants
.map((c) => c.values.length)
.reduce((a, b) => a + b, 0) +
key.templates.length
) {
const set: Set<string> = new Set();
for (const atomic of key.atomics) set.add(atomic);
for (const constant of key.constants) set.add(constant.type);
if (key.templates.length) set.add("string");

return set.size === 1;
}
return false;
};
}

const prefix = (method: string) => `Error on typia.protobuf.${method}():`;
Expand All @@ -188,6 +194,29 @@ const notSupportedError = (p: { method: string }) => (title: string) =>
`${prefix(p.method)}: protocol buffer does not support ${title}.`,
);

const isAtomicKey = (key: Metadata) => {
if (
key.required &&
key.nullable === false &&
key.functional === false &&
key.resolved === null &&
key.size() ===
key.atomics.length +
key.constants
.map((c) => c.values.length)
.reduce((a, b) => a + b, 0) +
key.templates.length
) {
const set: Set<string> = new Set();
for (const atomic of key.atomics) set.add(atomic);
for (const constant of key.constants) set.add(constant.type);
if (key.templates.length) set.add("string");

return set.size === 1;
}
return false;
};

const isDynamicObject = (obj: MetadataObject): boolean => {
return (
obj.properties.length > 0 &&
Expand Down
10 changes: 6 additions & 4 deletions src/programmers/protobuf/ProtobufMessageProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ export namespace ProtobufMessageProgrammer {
return type.indexOf("${name}") !== -1
? type.replace("${name}", key)
: `${
prop.value.arrays.length || key.startsWith("map<")
prop.value.arrays.length || type.startsWith("map<")
? ""
: !prop.value.isRequired() || prop.value.nullable
? "optional "
: "required"
: "required "
}${type} ${key} = ${++ptr.value};`;
})
.join("\n");
Expand Down Expand Up @@ -123,8 +123,10 @@ export namespace ProtobufMessageProgrammer {
: [
"oneof ${name} {",
...[...elements].map(
(str, i) =>
`${TAB}${str} v${i + 1} = ${++ptr.value};`,
(str) =>
`${TAB}${str} v${
ptr.value + 1
} = ${++ptr.value};`,
),
"}",
].join("\n");
Expand Down
7 changes: 7 additions & 0 deletions test/features/assert/test_assert_ObjectJsonTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import typia from "../../../src";
import { _test_assert } from "../../internal/_test_assert";
import { ObjectJsonTag } from "../../structures/ObjectJsonTag";

export const test_assert_ObjectJsonTag = _test_assert<ObjectJsonTag>(
ObjectJsonTag,
)((input) => typia.assert<ObjectJsonTag>(input));
7 changes: 7 additions & 0 deletions test/features/assert/test_assert_TagDefault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import typia from "../../../src";
import { _test_assert } from "../../internal/_test_assert";
import { TagDefault } from "../../structures/TagDefault";

export const test_assert_TagDefault = _test_assert<TagDefault>(TagDefault)(
(input) => typia.assert<TagDefault>(input),
);
8 changes: 8 additions & 0 deletions test/features/assertEquals/test_assertEquals_ObjectJsonTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import typia from "../../../src";
import { _test_assertEquals } from "../../internal/_test_assertEquals";
import { ObjectJsonTag } from "../../structures/ObjectJsonTag";

export const test_assertEquals_ObjectJsonTag =
_test_assertEquals<ObjectJsonTag>(ObjectJsonTag)((input) =>
typia.assertEquals<ObjectJsonTag>(input),
);
7 changes: 7 additions & 0 deletions test/features/assertEquals/test_assertEquals_TagDefault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import typia from "../../../src";
import { _test_assertEquals } from "../../internal/_test_assertEquals";
import { TagDefault } from "../../structures/TagDefault";

export const test_assertEquals_TagDefault = _test_assertEquals<TagDefault>(
TagDefault,
)((input) => typia.assertEquals<TagDefault>(input));
7 changes: 7 additions & 0 deletions test/features/createAssert/test_createAssert_ObjectJsonTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import typia from "../../../src";
import { _test_assert } from "../../internal/_test_assert";
import { ObjectJsonTag } from "../../structures/ObjectJsonTag";

export const test_assert_ObjectJsonTag = _test_assert<ObjectJsonTag>(
ObjectJsonTag,
)(typia.createAssert<ObjectJsonTag>());
7 changes: 7 additions & 0 deletions test/features/createAssert/test_createAssert_TagDefault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import typia from "../../../src";
import { _test_assert } from "../../internal/_test_assert";
import { TagDefault } from "../../structures/TagDefault";

export const test_assert_TagDefault = _test_assert<TagDefault>(TagDefault)(
typia.createAssert<TagDefault>(),
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import typia from "../../../src";
import { _test_assertEquals } from "../../internal/_test_assertEquals";
import { ObjectJsonTag } from "../../structures/ObjectJsonTag";

export const test_assertEquals_ObjectJsonTag =
_test_assertEquals<ObjectJsonTag>(ObjectJsonTag)(
typia.createAssertEquals<ObjectJsonTag>(),
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import typia from "../../../src";
import { _test_assertEquals } from "../../internal/_test_assertEquals";
import { TagDefault } from "../../structures/TagDefault";

export const test_assertEquals_TagDefault = _test_assertEquals<TagDefault>(
TagDefault,
)(typia.createAssertEquals<TagDefault>());
7 changes: 7 additions & 0 deletions test/features/createEquals/test_createEquals_ObjectJsonTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import typia from "../../../src";
import { _test_equals } from "../../internal/_test_equals";
import { ObjectJsonTag } from "../../structures/ObjectJsonTag";

export const test_equals_ObjectJsonTag = _test_equals<ObjectJsonTag>(
ObjectJsonTag,
)(typia.createEquals<ObjectJsonTag>());
7 changes: 7 additions & 0 deletions test/features/createEquals/test_createEquals_TagDefault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import typia from "../../../src";
import { _test_equals } from "../../internal/_test_equals";
import { TagDefault } from "../../structures/TagDefault";

export const test_equals_TagDefault = _test_equals<TagDefault>(TagDefault)(
typia.createEquals<TagDefault>(),
);
7 changes: 7 additions & 0 deletions test/features/createIs/test_createIs_ObjectJsonTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import typia from "../../../src";
import { _test_is } from "../../internal/_test_is";
import { ObjectJsonTag } from "../../structures/ObjectJsonTag";

export const test_is_ObjectJsonTag = _test_is<ObjectJsonTag>(ObjectJsonTag)(
typia.createIs<ObjectJsonTag>(),
);
7 changes: 7 additions & 0 deletions test/features/createIs/test_createIs_TagDefault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import typia from "../../../src";
import { _test_is } from "../../internal/_test_is";
import { TagDefault } from "../../structures/TagDefault";

export const test_is_TagDefault = _test_is<TagDefault>(TagDefault)(
typia.createIs<TagDefault>(),
);
10 changes: 10 additions & 0 deletions test/features/createRandom/test_createRandom_ObjectJsonTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import typia from "../../../src";
import { _test_random } from "../../internal/_test_random";
import { ObjectJsonTag } from "../../structures/ObjectJsonTag";

export const test_random_ObjectJsonTag = _test_random<ObjectJsonTag>(
ObjectJsonTag,
)({
random: typia.createRandom<ObjectJsonTag>(),
assert: typia.createAssert<ObjectJsonTag>(),
});
8 changes: 8 additions & 0 deletions test/features/createRandom/test_createRandom_TagDefault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import typia from "../../../src";
import { _test_random } from "../../internal/_test_random";
import { TagDefault } from "../../structures/TagDefault";

export const test_random_TagDefault = _test_random<TagDefault>(TagDefault)({
random: typia.createRandom<TagDefault>(),
assert: typia.createAssert<TagDefault>(),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import typia from "../../../src";
import { _test_validate } from "../../internal/_test_validate";
import { ObjectJsonTag } from "../../structures/ObjectJsonTag";

export const test_validate_ObjectJsonTag = _test_validate<ObjectJsonTag>(
ObjectJsonTag,
)(typia.createValidate<ObjectJsonTag>());
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import typia from "../../../src";
import { _test_validate } from "../../internal/_test_validate";
import { TagDefault } from "../../structures/TagDefault";

export const test_validate_TagDefault = _test_validate<TagDefault>(TagDefault)(
typia.createValidate<TagDefault>(),
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import typia from "../../../src";
import { _test_validateEquals } from "../../internal/_test_validateEquals";
import { ObjectJsonTag } from "../../structures/ObjectJsonTag";

export const test_validateEquals_ObjectJsonTag =
_test_validateEquals<ObjectJsonTag>(ObjectJsonTag)(
typia.createValidateEquals<ObjectJsonTag>(),
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import typia from "../../../src";
import { _test_validateEquals } from "../../internal/_test_validateEquals";
import { TagDefault } from "../../structures/TagDefault";

export const test_validateEquals_TagDefault = _test_validateEquals<TagDefault>(
TagDefault,
)(typia.createValidateEquals<TagDefault>());
7 changes: 7 additions & 0 deletions test/features/equals/test_equals_ObjectJsonTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import typia from "../../../src";
import { _test_equals } from "../../internal/_test_equals";
import { ObjectJsonTag } from "../../structures/ObjectJsonTag";

export const test_equals_ObjectJsonTag = _test_equals<ObjectJsonTag>(
ObjectJsonTag,
)((input) => typia.equals<ObjectJsonTag>(input));
7 changes: 7 additions & 0 deletions test/features/equals/test_equals_TagDefault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import typia from "../../../src";
import { _test_equals } from "../../internal/_test_equals";
import { TagDefault } from "../../structures/TagDefault";

export const test_equals_TagDefault = _test_equals<TagDefault>(TagDefault)(
(input) => typia.equals<TagDefault>(input),
);
7 changes: 7 additions & 0 deletions test/features/is/test_is_ObjectJsonTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import typia from "../../../src";
import { _test_is } from "../../internal/_test_is";
import { ObjectJsonTag } from "../../structures/ObjectJsonTag";

export const test_is_ObjectJsonTag = _test_is<ObjectJsonTag>(ObjectJsonTag)(
(input) => typia.is<ObjectJsonTag>(input),
);
7 changes: 7 additions & 0 deletions test/features/is/test_is_TagDefault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import typia from "../../../src";
import { _test_is } from "../../internal/_test_is";
import { TagDefault } from "../../structures/TagDefault";

export const test_is_TagDefault = _test_is<TagDefault>(TagDefault)((input) =>
typia.is<TagDefault>(input),
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import typia from "../../../src";
import { _test_json_assertParse } from "../../internal/_test_json_assertParse";
import { ObjectJsonTag } from "../../structures/ObjectJsonTag";

export const test_json_assertParse_ObjectJsonTag =
_test_json_assertParse<ObjectJsonTag>(ObjectJsonTag)((input) =>
typia.json.assertParse<ObjectJsonTag>(input),
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import typia from "../../../src";
import { _test_json_assertParse } from "../../internal/_test_json_assertParse";
import { TagDefault } from "../../structures/TagDefault";

export const test_json_assertParse_TagDefault =
_test_json_assertParse<TagDefault>(TagDefault)((input) =>
typia.json.assertParse<TagDefault>(input),
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import typia from "../../../src";
import { _test_json_assertStringify } from "../../internal/_test_json_assertStringify";
import { ObjectJsonTag } from "../../structures/ObjectJsonTag";

export const test_json_assertStringify_ObjectJsonTag =
_test_json_assertStringify<ObjectJsonTag>(ObjectJsonTag)((input) =>
typia.json.assertStringify<ObjectJsonTag>(input),
);
Loading

0 comments on commit 7a99d20

Please sign in to comment.