songjiachao commented on code in PR #323:
URL: https://github.com/apache/dubbo-js/pull/323#discussion_r1035847335
##########
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要必传,不然 无法找到类型,下面写判断也有点麻烦
<img width="811" alt="image"
src="https://user-images.githubusercontent.com/70792885/204783332-7e746fdd-4f51-493c-adc9-011d6f0daebc.png">
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]