tafia / quick-protobuf

A rust implementation of protobuf parser
MIT License
446 stars 82 forks source link

Default values not respected #216

Open stjepangolemac opened 2 years ago

stjepangolemac commented 2 years ago

For example:

syntax = "proto2";

...

required bool foo = 1 [default = true];

Generated rust code still defaults to false for field foo.

nerdrew commented 2 years ago

I assume you mean when doing MyMessage::default(). It looks like default values should be respected when deserializing a proto.

stjepangolemac commented 2 years ago

I don't know much about deserialization in this case but if the Default implementation doesn't respect the default value in the proto definition then the boolean will end up being false, hence it will be deserialized as false?

I guess what I'm asking is should the Default implementation follow the field definition in the proto file?

viveshok commented 2 years ago

Minimal Reproducible Example

schema.proto:

syntax = "proto2";

message Foo {
    required uint64 bar = 1;
    optional bool baz = 2 [default = true];
}

main.rs:

pub mod schema;

fn main() {
    let foo = schema::Foo{bar: 420, ..Default::default()};
    print!("foo.bar: {} foo.baz: {}\n", foo.bar, foo.baz);
}

expected result:

$ cargo run
foo.bar: 420 foo.baz: true

observed result:

$ cargo run
foo.bar: 420 foo.baz: false
snproj commented 1 year ago

Oh, by the way I think #247 should solve this (as part of our implementation of field presence, we had to fix the impl Default system).