Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default values not respected #216

Open
stjepangolemac opened this issue Apr 5, 2022 · 4 comments
Open

Default values not respected #216

stjepangolemac opened this issue Apr 5, 2022 · 4 comments

Comments

@stjepangolemac
Copy link

For example:

syntax = "proto2";

...

required bool foo = 1 [default = true];

Generated rust code still defaults to false for field foo.

@nerdrew
Copy link
Collaborator

nerdrew commented Apr 9, 2022

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

@stjepangolemac
Copy link
Author

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
Copy link

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
Copy link
Collaborator

snproj commented Mar 6, 2023

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants