With some effort you can do this using the reflection API, but if possible
it would be even better to avoid using required fields.

On Tue, Sep 8, 2020 at 6:44 PM 童虎 <[email protected]> wrote:

> I have a protobuf like this:
>
> ```
> syntax = "proto2";
> message Foo {
> required int foo1 = 1;
> required int foo2 = 2;
> ...
> required int foo20 = 20;
> }
> ```
>
> When I use the generated Foo class in c++. I want to create a instance
> serialized it. Looks like this:
>
> ```
> Foo foo;
> foo.set_foo1(1);
> // What I want is ommit the others with them default value.
> string fb;
> foo.SerializeToString(&pb);
> ```
> But at runtime, this will throw exception says "The foo2...foo20" is not
> setted.
>
> What I want is default value 0. Setting them seems like boring
>
> Can I do this. My english is poor, If you can't understand me, please let
> me know. Thank you.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/protobuf/629f1220-8523-4d2d-a3a4-41d296113d01n%40googlegroups.com
> <https://groups.google.com/d/msgid/protobuf/629f1220-8523-4d2d-a3a4-41d296113d01n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/protobuf/CADqAXr5R-TW52Fq6_E_t61XLKDRq1cULULydbCtnN3bWQBD11w%40mail.gmail.com.

Reply via email to