starwing / lua-protobuf

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

metatable defaults give results for absent "oneof" fields #80

Closed edam closed 4 years ago

edam commented 5 years ago

When the use_default_metatable option is used, values for oneof fields are returned even when they are not present.

For example,

syntax = "proto3";
message Foo {
  oneof optional_a {
    int32 a = 1;
  }
  oneof test {
    int32 b = 2;
    int32 c = 3;
  }
}

Decoding works:

lua> pb.option( "use_default_metatable" )
lua> local foo = pb.encode( 'Foo', { b = 9 } )
lua> dump( foo )
{ b = 9 }
lua> foo.b
9

However, accessing fields via the metatable yields default values for oneof fields that are not present.

lua> foo.a
0
lua> foo.c
0

These previous results should be nil, because they are not present in the oneof.

(Note: the use_default_values option works ok. It's just the metatable defaults that are wrong.)

starwing commented 5 years ago

metatale doesn't know the oneof, it used for custom default values (you have chance to set your own metatale), so in your case just don't use it :-)