You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2022/11/30 09:08:29 UTC

[GitHub] [dubbo-js] hufeng commented on a diff in pull request #323: Serialization 实现

hufeng commented on code in PR #323:
URL: https://github.com/apache/dubbo-js/pull/323#discussion_r1034717576


##########
packages/dubbo-serialization/src/proto.ts:
##########
@@ -0,0 +1,64 @@
+import fs from 'fs'
+import path from 'path'
+import _ from 'lodash'
+import { loadSync, Root, Type } from 'protobufjs'
+
+let _proto: Root | undefined = undefined
+
+/**
+ * 加载所有的proto文件
+ * @param dir 文件路径
+ * @returns Root namespace
+ */
+function loadProto(dir: string) {
+  const files = fs.readdirSync(dir)

Review Comment:
   readdirSync不会递归子目录,需不需要考虑递归的子目录,或者依赖glob模块进行过滤



##########
packages/dubbo-serialization/src/proto.ts:
##########
@@ -0,0 +1,64 @@
+import fs from 'fs'
+import path from 'path'
+import _ from 'lodash'
+import { loadSync, Root, Type } from 'protobufjs'
+
+let _proto: Root | undefined = undefined

Review Comment:
   rename _proto -> protoCache



##########
packages/dubbo-serialization/src/proto.ts:
##########
@@ -0,0 +1,64 @@
+import fs from 'fs'
+import path from 'path'
+import _ from 'lodash'
+import { loadSync, Root, Type } from 'protobufjs'
+
+let _proto: Root | undefined = undefined
+
+/**
+ * 加载所有的proto文件
+ * @param dir 文件路径
+ * @returns Root namespace
+ */
+function loadProto(dir: string) {
+  const files = fs.readdirSync(dir)
+  // todo 优化成flatMap
+  const protoFiles = files
+    .filter((fileName) => fileName.endsWith('.proto'))
+    .map((fileName) => path.join(dir, fileName))
+  _proto = loadSync(protoFiles)
+  return _proto
+}
+
+/**
+ * 根据typeName寻找proto
+ * @param typeName
+ * @returns Reflected message type
+ */
+function lookup(typeName: string): Type {
+  if (!_.isString(typeName)) {
+    throw new TypeError('typeName must be a string')
+  }
+  if (!_proto) {
+    throw new TypeError('Please load proto before lookup')
+  }
+  return _proto.lookupType(typeName)
+}
+
+/**
+ * Creates a new message of this type using the specified properties.
+ * @param params 参数
+ * @param protoName 名称
+ * @returns 类型
+ */
+function encode<T extends { [k: string]: unknown }>(data: T, type: string) {
+  // 根据protoName找到对应的message
+  const Message = lookup(type)
+  if (!Message) {
+    throw new TypeError(`${type} not found, please check it again`)
+  }
+  return Message.encode(Message.create(data)).finish()
+}
+
+/**
+ *
+ * @param data 解码数据
+ * @param type pb类型
+ * @returns
+ */
+function decode<T = { [k: string]: unknown }>(data: Buffer, type: string): T {

Review Comment:
   type?



##########
packages/dubbo-serialization/src/proto.ts:
##########
@@ -0,0 +1,64 @@
+import fs from 'fs'
+import path from 'path'
+import _ from 'lodash'
+import { loadSync, Root, Type } from 'protobufjs'
+
+let _proto: Root | undefined = undefined
+
+/**
+ * 加载所有的proto文件
+ * @param dir 文件路径
+ * @returns Root namespace
+ */
+function loadProto(dir: string) {
+  const files = fs.readdirSync(dir)
+  // todo 优化成flatMap
+  const protoFiles = files
+    .filter((fileName) => fileName.endsWith('.proto'))
+    .map((fileName) => path.join(dir, fileName))
+  _proto = loadSync(protoFiles)
+  return _proto
+}
+
+/**
+ * 根据typeName寻找proto
+ * @param typeName
+ * @returns Reflected message type
+ */
+function lookup(typeName: string): Type {
+  if (!_.isString(typeName)) {
+    throw new TypeError('typeName must be a string')
+  }
+  if (!_proto) {
+    throw new TypeError('Please load proto before lookup')
+  }
+  return _proto.lookupType(typeName)
+}
+
+/**
+ * Creates a new message of this type using the specified properties.
+ * @param params 参数
+ * @param protoName 名称
+ * @returns 类型
+ */
+function encode<T extends { [k: string]: unknown }>(data: T, type: string) {

Review Comment:
   type?



##########
packages/dubbo-serialization/src/proto.ts:
##########
@@ -0,0 +1,64 @@
+import fs from 'fs'
+import path from 'path'
+import _ from 'lodash'
+import { loadSync, Root, Type } from 'protobufjs'
+
+let _proto: Root | undefined = undefined
+
+/**
+ * 加载所有的proto文件
+ * @param dir 文件路径
+ * @returns Root namespace
+ */
+function loadProto(dir: string) {
+  const files = fs.readdirSync(dir)
+  // todo 优化成flatMap
+  const protoFiles = files
+    .filter((fileName) => fileName.endsWith('.proto'))
+    .map((fileName) => path.join(dir, fileName))
+  _proto = loadSync(protoFiles)
+  return _proto
+}
+
+/**
+ * 根据typeName寻找proto
+ * @param typeName
+ * @returns Reflected message type
+ */
+function lookup(typeName: string): Type {
+  if (!_.isString(typeName)) {
+    throw new TypeError('typeName must be a string')
+  }
+  if (!_proto) {

Review Comment:
   先判断 😄



##########
packages/dubbo-serialization/src/proto.ts:
##########
@@ -0,0 +1,64 @@
+import fs from 'fs'
+import path from 'path'
+import _ from 'lodash'

Review Comment:
   只有一个地方使用了判断是不是字符串,可以使用node的util模块,这样减少一个依赖
   
   ![image](https://user-images.githubusercontent.com/533008/204533866-1f18d668-8d09-4453-9ce6-14d2fb781b8d.png)
   



##########
packages/dubbo-serialization/src/proto.ts:
##########
@@ -0,0 +1,64 @@
+import fs from 'fs'
+import path from 'path'
+import _ from 'lodash'
+import { loadSync, Root, Type } from 'protobufjs'
+
+let _proto: Root | undefined = undefined
+
+/**
+ * 加载所有的proto文件
+ * @param dir 文件路径
+ * @returns Root namespace
+ */
+function loadProto(dir: string) {
+  const files = fs.readdirSync(dir)
+  // todo 优化成flatMap
+  const protoFiles = files
+    .filter((fileName) => fileName.endsWith('.proto'))
+    .map((fileName) => path.join(dir, fileName))
+  _proto = loadSync(protoFiles)
+  return _proto
+}
+
+/**
+ * 根据typeName寻找proto
+ * @param typeName
+ * @returns Reflected message type
+ */
+function lookup(typeName: string): Type {
+  if (!_.isString(typeName)) {

Review Comment:
   util.types.isStringObject



##########
packages/dubbo-serialization/src/proto.ts:
##########
@@ -0,0 +1,64 @@
+import fs from 'fs'

Review Comment:
   我们是不是统一使用node前缀
   import fs from 'node:fs'
   import path from 'node:path'



##########
packages/dubbo-serialization/src/proto.ts:
##########
@@ -0,0 +1,64 @@
+import fs from 'fs'
+import path from 'path'
+import _ from 'lodash'
+import { loadSync, Root, Type } from 'protobufjs'
+
+let _proto: Root | undefined = undefined
+
+/**
+ * 加载所有的proto文件
+ * @param dir 文件路径
+ * @returns Root namespace
+ */
+function loadProto(dir: string) {
+  const files = fs.readdirSync(dir)
+  // todo 优化成flatMap
+  const protoFiles = files
+    .filter((fileName) => fileName.endsWith('.proto'))
+    .map((fileName) => path.join(dir, fileName))
+  _proto = loadSync(protoFiles)
+  return _proto
+}
+
+/**
+ * 根据typeName寻找proto
+ * @param typeName
+ * @returns Reflected message type
+ */
+function lookup(typeName: string): Type {
+  if (!_.isString(typeName)) {
+    throw new TypeError('typeName must be a string')
+  }
+  if (!_proto) {
+    throw new TypeError('Please load proto before lookup')
+  }
+  return _proto.lookupType(typeName)
+}
+
+/**
+ * Creates a new message of this type using the specified properties.
+ * @param params 参数
+ * @param protoName 名称
+ * @returns 类型
+ */
+function encode<T extends { [k: string]: unknown }>(data: T, type: string) {

Review Comment:
   =



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org