typescript - 如何在打字稿中使用 google.protobuf.Any 类型?
问题描述
1.protocol buffer 3文件test.proto
option go_package = ".;apps";
package apps;
message ShardKvMap{
map<string, google.protobuf.Any> data = 1;
}
2.我使用 grpc-web 构建 .ts 文件
protoc -I=$DIR test.proto \
--js_out=import_style=commonjs,binary:$OUT_DIR \
--grpc-web_out=import_style=typescript,mode=grpcwebtext:$OUT_DIR
3.如何在typescript中使用google.protobuf.Any type?
解决方案
我无法为您提供内置的 JS 和 TS 支持,但我可以告诉您如何使用protobuf-ts 插件(我是作者)来做到这一点。
// this creates an empty new message
let msg = ShardKvMap.create();
// this packs the empty new message into an Any message,
// and adds it to map under the key "foo":
msg.data["foo"] = Any.pack(msg, ShardKvMap);
据我所知,protobuf-ts 是唯一一个完全支持 google.protobuf.Any 的 JavaScript / TypeScript 库,包括 JSON 格式。这是手册中有关任何支持的部分。
推荐阅读
- javascript - 使用 Puppeteer 点击“onclick”-login-button
- javascript - 节点终端动画不起作用(它只显示数组中的前两项)
- git - Git 重命名远程和本地分支
- html - 在 Vuetify 中使用 Fill-Height 属性时移动 v-container 的中心内容
- angular - 更改全局值时更新现有时刻语言环境
- javascript - 条件渲染不起作用,因为尚未定义变量
- python - 哪些 praw 命令需要网络请求?
- javascript - DeviceOrientation Compass Android
- python-3.x - How to package a python module that imports another module within that package
- react-native - 如何在 Flatlist 中申请 onEndReached?