This is an automated email from the ASF dual-hosted git repository.
chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-fury.git
The following commit(s) were added to refs/heads/main by this push:
new 8b3fe0ed feat(javascript): add data to description util (#1609)
8b3fe0ed is described below
commit 8b3fe0ed6de30526d6dcfaa884b1212b3029a2c5
Author: 野声 <[email protected]>
AuthorDate: Wed May 8 09:33:21 2024 +0800
feat(javascript): add data to description util (#1609)
<!--
**Thanks for contributing to Fury.**
**If this is your first time opening a PR on fury, you can refer to
[CONTRIBUTING.md](https://github.com/apache/incubator-fury/blob/main/CONTRIBUTING.md).**
Contribution Checklist
- The **Apache Fury (incubating)** community has restrictions on the
naming of pr titles. You can also find instructions in
[CONTRIBUTING.md](https://github.com/apache/incubator-fury/blob/main/CONTRIBUTING.md).
- Fury has a strong focus on performance. If the PR you submit will have
an impact on performance, please benchmark it first and provide the
benchmark result here.
-->
## What does this PR do?
<!-- Describe the purpose of this PR. -->
Add a new util called `data2Description`, which can reduce the amount of
code
## Related issues
<!--
Is there any related issue? Please attach here.
- #xxxx0
- #xxxx1
- #xxxx2
-->
## Does this PR introduce any user-facing change?
<!--
If any user-facing interface changes, please [open an
issue](https://github.com/apache/incubator-fury/issues/new/choose)
describing the need to do so and update the document if necessary.
-->
- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?
## Benchmark
<!--
When the PR has an impact on performance (if you don't know whether the
PR will have an impact on performance, you can submit the PR first, and
if it will have impact on performance, the code reviewer will explain
it), be sure to attach a benchmark data here.
-->
---
javascript/benchmark/index.js | 4 +-
javascript/packages/fury/lib/util.ts | 90 ++++++++++++++++++++++++++++++
javascript/test/util.js | 104 -----------------------------------
3 files changed, 92 insertions(+), 106 deletions(-)
diff --git a/javascript/benchmark/index.js b/javascript/benchmark/index.js
index 016b7710..52d0b267 100644
--- a/javascript/benchmark/index.js
+++ b/javascript/benchmark/index.js
@@ -18,7 +18,7 @@
*/
const Fury = require("@furyjs/fury");
-const utils = require("../test/util");
+const utils = require("@furyjs/fury/dist/lib/util");
const hps = require('@furyjs/hps');
const fury = new Fury.default({ hps, refTracking: false, useSliceString: true
});
const Benchmark = require("benchmark");
@@ -108,7 +108,7 @@ const sample = {
};
-const description = utils.mockData2Description(sample, "fury.test.foo");
+const description = utils.data2Description(sample, "fury.test.foo");
const { serialize, deserialize, serializeVolatile } =
fury.registerSerializer(description);
const furyAb = serialize(sample);
diff --git a/javascript/packages/fury/lib/util.ts
b/javascript/packages/fury/lib/util.ts
index b7e45a12..39042704 100644
--- a/javascript/packages/fury/lib/util.ts
+++ b/javascript/packages/fury/lib/util.ts
@@ -17,6 +17,9 @@
* under the License.
*/
+import { ObjectTypeDescription, Type, TypeDescription } from "./description";
+import { InternalSerializerType } from "./type";
+
export const isNodeEnv: boolean
= typeof process !== "undefined"
&& process.versions != null
@@ -24,3 +27,90 @@ export const isNodeEnv: boolean
&& process.versions.node != null;
export const hasBuffer = isNodeEnv && typeof Buffer !== "undefined";
+
+export function isUint8Array(obj: any): obj is Uint8Array {
+ return obj instanceof Uint8Array || Object.prototype.toString.call(obj) ===
"[object Uint8Array]";
+}
+
+export const data2Description = (
+ data: any,
+ tag: string,
+): TypeDescription | null => {
+ if (data === null || data === undefined) {
+ return null;
+ }
+ if (Array.isArray(data)) {
+ const item = data2Description(data[0], tag);
+ if (!item) {
+ throw new Error("empty array can't convert");
+ }
+ return {
+ ...Type.array(item),
+ label: "array",
+ };
+ }
+ if (data instanceof Date) {
+ return {
+ ...Type.timestamp(),
+ label: "timestamp",
+ };
+ }
+ if (typeof data === "string") {
+ return {
+ ...Type.string(),
+ label: "string",
+ };
+ }
+ if (data instanceof Set) {
+ return {
+ ...Type.set(data2Description([...data.values()][0], tag)!),
+ label: "set",
+ };
+ }
+ if (data instanceof Map) {
+ return {
+ ...Type.map(
+ data2Description([...data.keys()][0], tag)!,
+ data2Description([...data.values()][0], tag)!,
+ ),
+ label: "map",
+ };
+ }
+ if (typeof data === "boolean") {
+ return {
+ ...Type.bool(),
+ label: "boolean",
+ };
+ }
+ if (typeof data === "number") {
+ if (data > Number.MAX_SAFE_INTEGER || data < Number.MIN_SAFE_INTEGER) {
+ return {
+ ...Type.int64(),
+ label: "int64",
+ };
+ }
+ return {
+ ...Type.int32(),
+ label: "int32",
+ };
+ }
+
+ if (typeof data === "object") {
+ if (isUint8Array(data)) {
+ return Type.binary();
+ }
+
+ return Type.object(
+ tag,
+ Object.fromEntries(
+ Object.entries(data)
+ .map(([key, value]) => {
+ return [key, data2Description(value, `${tag}.${key}`)];
+ })
+ .filter(([_, v]) => Boolean(v)),
+ ),
+ );
+ }
+
+ throw new Error(`unkonw data type ${typeof data}`);
+};
diff --git a/javascript/test/util.js b/javascript/test/util.js
deleted file mode 100644
index 12efe989..00000000
--- a/javascript/test/util.js
+++ /dev/null
@@ -1,104 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-const { InternalSerializerType } = require('@furyjs/fury')
-
-const mockData2Description = (data, tag) => {
- if (data === null || data === undefined) {
- return null;
- }
- if (Array.isArray(data)) {
- const item = mockData2Description(data[0], tag);
- if (!item) {
- throw new Error('empty array can\'t convert')
- }
- return {
- type: InternalSerializerType.ARRAY,
- label: 'array',
- options: {
- inner: item,
- }
- }
- }
- if (data instanceof Date) {
- return {
- type: InternalSerializerType.DURATION,
- label: 'timestamp'
- }
- }
- if (typeof data === 'string') {
- return {
- type: InternalSerializerType.STRING,
- label: "string",
- }
- }
- if (data instanceof Set) {
- return {
- type: InternalSerializerType.SET,
- label: "set",
- options: {
- key: mockData2Description([...data.values()][0], tag),
- }
- }
- }
- if (data instanceof Map) {
- return {
- type: InternalSerializerType.MAP,
- label: "map",
- options: {
- key: mockData2Description([...data.keys()][0], tag),
- value: mockData2Description([...data.values()][0], tag),
- }
- }
- }
- if (typeof data === 'boolean') {
- return {
- type: InternalSerializerType.BOOL,
- label: "boolean",
- }
- }
- if (typeof data === 'number') {
- if (data > Number.MAX_SAFE_INTEGER || data < Number.MIN_SAFE_INTEGER) {
- return {
- type: InternalSerializerType.INT64,
- label: "int64"
- }
- }
- return {
- type: InternalSerializerType.INT32,
- label: "int32"
- }
- }
- if (typeof data === 'object') {
- return {
- type: InternalSerializerType.OBJECT,
- label: "object",
- options: {
- props: Object.fromEntries(Object.entries(data).map(([key,
value]) => {
- return [key, mockData2Description(value, `${tag}.${key}`)]
- }).filter(([k, v]) => Boolean(v))),
- tag
- }
-
- }
- }
- throw `unkonw data type ${typeof data}`
-}
-module.exports.mockData2Description = mockData2Description;
-
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]