Reading into Protocol Buffer Basics: C++, found nothing that matches the situation:; with following .proto processed with --cpp_out
,
message A {
required int32 foo = 1;
}
message B {
optional A data = 1;
}
no obviously looking accessor/setter is generated to set custom optional field (including 'nested types' section which I'm too lazy to put here):
// accessors -------------------------------------------------------
// optional .A = 1;
inline bool has_a() const;
inline void clear_a();
static const int kAFieldNumber = 1;
inline const ::A& a() const;
inline ::A* mutable_a();
inline ::A* release_a();
So, how to set B::A to some A instance in C++?
TEST FILES: .proto, generated results: .h, .cc and some .java
Upd: in Java, nested fields are set via Builder: see link above for example (look for setData).
In proto3, all fields are "optional" (in that it is not an error if the sender fails to set them). But, fields are no longer "nullable", in that there's no way to tell the difference between a field being explicitly set to its default value vs. not having been set at all.
A repeated field is inherently optional : you just don't add any values. As for com. google. protobuf.
Yes, repeated fields retain the order of items. From Google's Protocol Buffers encoding specification: The order of the elements with respect to each other is preserved when parsing, though the ordering with respect to other fields is lost.
Proto3 is the latest version of Protocol Buffers and includes the following changes from proto2: Field presence, also known as hasField , is removed by default for primitive fields. An unset primitive field has a language-defined default value.
Solution: use mutable to modify some returned doodad.
A a;
A.set_foo(1);
B b;
B.mutable_A()->CopyFrom(a);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With