Skip to content

Commit

Permalink
Provide an example of using proto3's primitive wrappers.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=250656647
  • Loading branch information
graememorgan authored and ronshapiro committed Jun 12, 2019
1 parent beef3d8 commit e2cb9fe
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions docs/bugpattern/ProtoFieldNullComparison.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ comparisons like these often indicate a nearby error.

If you need to distinguish between an unset optional value and a default value,
you have two options. In most cases, you can simply use the `hasField()` method.
proto3 however does not generate `hasField()` methods for scalar fields of type
`string` or `bytes`. In those cases you will need to wrap your field in
`google.protobuf.StringValue` or `google.protobuf.BytesValue`, respectively.
proto3 however does not generate `hasField()` methods for primitive types
(including `string` and `bytes`). In those cases you will need to wrap your
field in `google.protobuf.StringValue` or similar.

NOTE: This check applies to normal (server) protos and Lite protos. The
deprecated nano runtime does produce objects which use `null` values to indicate
Expand Down Expand Up @@ -40,3 +40,15 @@ void test(MyProto proto) {
}
}
```

If the presence of a field is required information in proto3, the field can be
wrapped. For example,

```java {.good}
message MyMessage {
google.protobuf.StringValue my_string = 1;
}
```

Presence can then be tested using `myMessage.hasMyString()`, and the value
retrieved using `myMessage.getMyString().getValue()`.

0 comments on commit e2cb9fe

Please sign in to comment.