starwing / lua-protobuf

A Lua module to work with Google protobuf
MIT License
1.73k stars 387 forks source link

oneof field's all members were encoded #166

Closed haoxianhan closed 2 years ago

haoxianhan commented 2 years ago

proto define following:

message SampleMessage {
    oneof test_oneof {
        string str_name = 1;
        int32 str_int = 2;
        bool str_bool = 3;
    };
}

test example:

local data = {
    str_name = "str",
    str_int = 12,
    str_bool = false,
}

Print_r(pb.decode("SampleMessage", pb.encode("SampleMessage", data)))

result:

{
  str_bool = false,
  str_int = 12,
  str_name = "str"
} --[[table: 0x55e2e1575210]]

doesn't oneof field means just one of member would be encode/decode ?

starwing commented 2 years ago

the only data structure of Lua is table, which is a hash table. Means table has no order for it's keys. So if you set all keys of oneof, I cant find out which is the "last set", so it's not usable. lua-protobuf choose to encode all of the keys. Just don't do this if you don't like the behavior.

This library just do encode/decode for protobuf, but not validate the data. So write you own functions to validate it.

haoxianhan commented 2 years ago

the only data structure of Lua is table, which is a hash table. Means table has no order for it's keys. So if you set all keys of oneof, I cant find out which is the "last set", so it's not usable. lua-protobuf choose to encode all of the keys. Just don't do this if you don't like the behavior.

This library just do encode/decode for protobuf, but not validate the data. So write you own functions to validate it.

gotcha.