starwing / lua-protobuf

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

Why does `decode` keeps every field even without value in message #157

Closed leonyu879 closed 3 years ago

leonyu879 commented 3 years ago

decode will drop field for refrerence type which value is empty in a message, that's good but when the type is repeated or map, decode will generate a empty table which will let cjson encode it to {} (or [] when use empty_array_mt),that may cause type error in upstream service. can decode drop field when the types above(repeated/map) is empty?

starwing commented 3 years ago

It seems that protobuf3 need the empty table, if you use protobuf2, the repeated table should not exists. But you can use pb.hook() interrface to remove that table just after decodeed.

leonyu879 commented 3 years ago

how pb.hook works with repeated type (i have hundreds types defined in proto), actually i want treat empty repeated/map defferently(for json encode). and can u explain more clearly about it please?

It seems that protobuf3 need the empty tab

i have tried pb.hook("map") but it doesn't works. if empty table is required for repeated in proto3, can pb.decode remove empty map?

leonyu879 commented 3 years ago

solved by removing all empty tables from decoded obj

starwing commented 3 years ago

Sorry for late reply, you could look this line in source: https://github.com/starwing/lua-protobuf/blob/3f2d706aca7b57c3ffc27daf85c03c7d21a21f7c/pb.c#L1320

just change it to if (...) return 0 w/o your own flags will remove the empty table.

the pb.hook() interface acts on a message tyue, but "map" itself is not a message type, so pb.hook "map" is not work :-(

currently I have no way to config pb not produce the empty table other than modifying the source code, sorry for that...