Each descriptor describes part of the schema (e.g. a message type, enum
type, etc.) but is unrelated to any particular instance of it. As a result,
if you have a descriptor by itself then you can't really modify anything
because you separately need an instance of the thing you want to modify.
The way to programmatically modify a message is to use the Reflection
<https://github.com/protocolbuffers/protobuf/blob/9d9d8ee18dedfb18371031cd299d1d282ddf707f/src/google/protobuf/message.h#L452>
API. You can use Reflection::ListFields() to get a list of all the fields
that are set on the message and then there are Reflection::Get* and
Reflection::Set* methods to get and set particular fields.

On Fri, Jun 18, 2021 at 9:11 AM J G <[email protected]> wrote:

> Hi,
>
> I've got some code that lets me recursively walk a protobuf variable.
>
> That part works, I can enumerate the characteristics of a variable, but
> the pointers/references returned by the API are const.
>
> My question is: From a variable's Descriptor or FieldDescriptor, is it
> possible to get a non-const pointer/reference to the field to be able to
> modify it?
>
> Here's my (simplified) code so far:
>
> void enumpb(  const google::protobuf::Descriptor * d ) {
>
>     for ( int i = 0; i < d->field_count(); i++ ) {
>
>         auto field = d->field( i );
>
>         // Modify variable code here
>         [...]
>
>         auto mt = field->message_type();
>         if ( ! mt ) {
>             continue;
>         } else if ( 0 != strcmp( d->full_name().c_str(),
> mt->full_name().c_str() ) ) {
>
>             enumpb( mt ) ;
>
>         }
>
>     }
>
>     return true;
>
> }
>
> --
> 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/dcf6bb53-24ce-4404-ab71-0fe3a94adc40n%40googlegroups.com
> <https://groups.google.com/d/msgid/protobuf/dcf6bb53-24ce-4404-ab71-0fe3a94adc40n%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/CADqAXr7-kdVh4P3Rk0-7m7UReep0unCMto3xankxbAkDfHg4TA%40mail.gmail.com.

Reply via email to