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 value unexpected behaviour #100

Closed
gismoranas opened this issue Apr 14, 2016 · 2 comments
Closed

default value unexpected behaviour #100

gismoranas opened this issue Apr 14, 2016 · 2 comments

Comments

@gismoranas
Copy link

I have this protobuf code

message Foo { optional int32 schema = 1 [default = 11]; }

If I run this

val foo = new Foo(None)
println(foo.parseFrom(foo.toByteArray).getSchema) // 11
println(foo.parseFrom(foo.toByteArray).schema) // None

I suppose you set 11 in the getter instead of setting Some(11) in schema directly. Isn't this an inconsistent state?

@thesamet
Copy link
Contributor

This distinction between set and unset fields comes from the proto2 spec. A field may be either present or not in a message, which is modeled in ScalaPB as (Some(_) or None). Only when you call getX the default value is provided.

This is modeled after the official Java implementation, see "Singular value fields" at https://developers.google.com/protocol-buffers/docs/reference/java-generated#fields
which they have for each field X two methods: hasX() and getX(). We basically combine these two to a single optional field.

@thesamet
Copy link
Contributor

Exactly. We're just mapping these two fields to a single Option. You may be interested in trying proto3, where primitives are not wrapped in Options, but there are no default values either (default is 0 for numerics, empty string, etc)

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

2 participants