songjiachao commented on code in PR #323:
URL: https://github.com/apache/dubbo-js/pull/323#discussion_r1035845469
##########
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: [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]