starwing / lua-protobuf

A Lua module to work with Google protobuf
MIT License
1.71k stars 388 forks source link

请问下java的.proto文件存在import引用其它文件如何加载 #263

Open sue602 opened 4 months ago

sue602 commented 4 months ago

有文件1,文件名为 a.proto 内容如下

syntax = "proto2";
option java_package = "bf.message.game"; 
option java_outer_classname = "AntiqueMessage"; 

package bf.messages.game.EngineMessage;

message MsgGetReq_55012{
    optional CommonMsgMessage.MsgResources res= 1;
    optional int64 time=2; 
}
message MsgGetResp_55012{

}

有文件2,文件名为 b.proto 内容如下

syntax = "proto2";
option java_package = "bf.message.game"; 
option java_outer_classname = "CommonMsgMessage"; 

package bf.messages.game.CommonMsgMessage;

message MsgResources {
    optional int32 id = 1; 
    optional string num = 2; 
}

加载:

local pb = require "pb"
local protoc = require "protoc"
local pbdir = "./"
local protofiles = {a.proto,b.proto}
protoc:addpath(pbdir)

for _, proto_filename in ipairs(protofiles) do
     local proto = protoc.new()
     proto:addpath(pbdir)
     proto:loadfile(proto_filename)
end

local dd = {res= {id=1,num=3}, time= 3}
local bytes = pb.encode("MsgGetReq_55012", dd)  -- 这样加载失败
-- local bytes = pb.encode("AntiqueMessage.MsgGetReq_55012", dd)  -- 这样也是失败的

请问下要如何才能正确加载这种有import引用呢, 感谢

starwing commented 4 months ago

你加载成功了的,只是要带包名……你可以用pb.types看看有哪些类型加载进来了

regards, Xavier Wang.

sue602 @.***>于2024年3月25日 周一22:40写道:

有文件1,文件名为 a.proto 内容如下

syntax = "proto2"; option java_package = "bf.message.game"; option java_outer_classname = "AntiqueMessage";

package bf.messages.game.EngineMessage;

message MsgGetReq_55012{ optional CommonMsgMessage.MsgResources res= 1; optional int64 time=2; } message MsgGetResp_55012{

}

有文件2,文件名为 b.proto 内容如下

syntax = "proto2"; option java_package = "bf.message.game"; option java_outer_classname = "CommonMsgMessage";

package bf.messages.game.CommonMsgMessage;

message MsgResources { optional int32 id = 1; optional string num = 2; }

加载:

local pb = require "pb" local protoc = require "protoc" local pbdir = "./" local protofiles = {a.proto,b.proto} protoc:addpath(pbdir)

for _, proto_filename in ipairs(protofiles) do local proto = protoc.new() proto:addpath(pbdir) proto:loadfile(proto_filename) end

local dd = {res= {id=1,num=3}, time= 3} local bytes = pb.encode("MsgGetReq_55012", dd) -- 这样加载失败 -- local bytes = pb.encode("AntiqueMessage.MsgGetReq_55012", dd) -- 这样也是失败的

请问下要如何才能正确加载这种有import引用呢, 感谢

— Reply to this email directly, view it on GitHub https://github.com/starwing/lua-protobuf/issues/263, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA36M34WMUOVBWNRAGZG33Y2AZNNAVCNFSM6AAAAABFHENSQGVHI2DSMVQWIX3LMV43ASLTON2WKOZSGIYDKOJSGA4TAMQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>