Skip to content

fix import other proto bug, without changing the xxx_pb variable #22

Open
lifelongchaser wants to merge 1 commit into
sean-lin:masterfrom
lifelongchaser:import-proto-dev
Open

fix import other proto bug, without changing the xxx_pb variable #22
lifelongchaser wants to merge 1 commit into
sean-lin:masterfrom
lifelongchaser:import-proto-dev

Conversation

@lifelongchaser
Copy link
Copy Markdown

an error occurs when you want to use other message type from another proto file,

[string "protobuf.lua"]:363: attempt to index upvalue 'message_type' (a nil value)

e.g.
mail.proto:

    import "reward.proto";
    package mail;

    message Mail
    {
        optional uint32 id = 1;  
        optional reward.Reward reward = 2;
    }

reward.proto:

    package reward; 
    message Reward
    {
        optional uint32 money = 1;
    }

test.lua:

    -- sending --
    local sendMail = mail_pb.Mail()
    sendMail.id = 12
    local reward = sendMail.reward
    reward.money = 30
    printf(sendMail)

    -- receiving--
    local recvMail = mail_pb.Mail()
    recvMail:ParseFromString(sendMail:SerializeToString())
    printf(recvMail)

The same thing happens when using repeated instead of optional.

[string "containers.lua"]:27: attempt to call field '_concrete_class' (a nil value)

The reason is that mail_pb.lua generated by protoc-gen-lua doesn't consider the situation of other proto msg type. The message_type is nil.

some guy has fixed this bug by putting the REWARD table to XXX_pb variable. But I offer another method without changing XXX_pb.

you can choose whether is more fit considering your program structure.

Thank you for reading.

@kudoran
Copy link
Copy Markdown

kudoran commented May 24, 2017

This soluation leads to another bug. If there are a lot of messages or enums in proto file, it will create lots of local variables . More than 200 local variables in a chunk lead to lua error.

@mazexiang
Copy link
Copy Markdown

local player = player_pb.Player()
player.email="123456"
player.user.username = "mazexiang"
player.user.password="1004"

local msgdata = player:SerializeToString()
local msgplayer = player_pb.Player()
msgplayer:ParseFromString(msgdata)
print(msgplayer.user.username)

this is a example,and it work success

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants