luaprotobuf-c

Why is my protobuf not working correctly with enum types?


This is the proto file:

package net;

message msgBase {
   optional string protoName = 1;
   optional SendType sendType = 2;
   //optional NetClient sendClient = 3;
   optional float roomID = 4;
   //optional NetClient tagClient = 5;
}

enum SendType
{
  SendType_None = 0;  
  SendType_ALL = 1;  
  SendType_Self = 2; 
  SendType_Other = 3; 
  SendType_Tag = 4;   
  SendType_Specific = 5;  
}

This is my Lua function:

local function msgBase()
    pb.register_file("./proto/net.pb")
    
    local msgbase = {
        protoName = "msgDemo1",
        sendType = SendType_Tag,
        roomID = 7777,
    }

    local msg = pb.encode("net.msgBase",msgbase)
    opp = msg

    local tt = pb.decode("net.msgBase",msg)
    if tt then
        print(tt.protoName)
        print(tt.sendType)
    print(tt.roomID)
    else
        print("Error")
    end
end

Result:

1

The value assigned to an enum and then decoded is always the first.

I am using PBC.

I coded C via ptotoB, where an error occurred for the enumeration type, which always shows the first value of the enumeration.


Solution

  • The example binding/lua53/test.lua shows that enums are specified by string value in Lua.

    From the example, for the protocol (a snippet of addressbook.proto):

    enum PhoneType {
      MOBILE = 0;
      HOME = 1;
      WORK = 2;
    }
    
    message PhoneNumber {
      required string number = 1;
      optional PhoneType type = 2 [default = HOME];
    }
    
    repeated PhoneNumber phone = 4;
    

    The following Lua code is used:

    phone = {
        { number = "1301234567" },
        { number = "87654321", type = "WORK" },
    }
    

    So you should use

    local msgbase = {
        protoName = "msgDemo1",
        sendType = "SendType_Tag",
        roomID = 7777,
    }
    

    otherwise the Lua variable SendType_Tag resolves to nil, and the enum defaults to a zero value.