Protocolbuf不同语言生成

2021-09-30  本文已影响0人  岑吾

protoc

Protocol Buffers可以使用Google提供的protoc来生成不同语言的版本.

protoc -I=$SRC_DIR --type_out=$DST_DIR $SRC_DIR/file.proto

type_out支持的输出类型有:

类型 语言 输出文件
cpp_out C++ file.pb.cc,file.pb.h
csharp_out C# file.cs
go_out Go file.pb.go
java_out Java file..java

js/ts

对于js/ts的生成,可以使用protobufjs插件来完成

npm install -g protobufjs
# 生成js
pbjs -t static-module -w commonjs -o  file.js file.proto

# 生成ts
pbts -o file.ts file.js

脚本实现多proto生成

自己用python 3.7写了一个proto生成的脚本.https://github.com/zngw/protocol

将所有要生成proto源文件放在proto_raw目录中,可以是多个文件

//登录请求
message GameLoginReq {
    string account = 1;     // 登录帐号
    string password = 2;    // 登录密码
}

//登录返回
message GameLoginResp {
    int32 result = 1;   // 返回结果.
}

然后使用不同的to_xx.py生成不同的语言.生成的文件在out目录中


上一篇 下一篇

猜你喜欢

热点阅读