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

extension reflection question #33

Closed
kmatzen opened this issue Sep 21, 2014 · 3 comments
Closed

extension reflection question #33

kmatzen opened this issue Sep 21, 2014 · 3 comments

Comments

@kmatzen
Copy link

kmatzen commented Sep 21, 2014

I have two message types:

message Record {
  optional string foo = 1;
  optional int32 bar = 2;

  extensions 100 to max;
}

message SpecializedRecord {
  extend Record {
    optional SpecializedRecord parent = 100;
  }

  optional uint32 special_thing = 1;
}

I'm trying to use reflection to access special_thing by name in C++. First I retrieve parent and then get the extension message to do reflection on that. I'm having trouble getting the extension from the original message.

This works:

const Reflection* record_reflection = record.GetReflection();
vector<const FieldDescriptor*> fields;
record_reflection->ListFields(record, &fields);
const FieldDescriptor* extension_field = 0;
for (int i = 0; i < fields.size(); ++i) {
  if (fields.at(i)->name() == "parent") {
    extension_field = fields.at(i);
  }   
}
CHECK_NOTNULL(extension_field);

This doesn't work:

const Descriptor* record_descriptor = record.GetDescriptor();
const FieldDescriptor* extension_field = record_descriptor->FindFieldByName("parent");
CHECK_NOTNULL(extension_field);

Why?

@xfxyjwf
Copy link
Contributor

xfxyjwf commented Sep 30, 2014

FindFieldByName() only works for regular fields. For extensions, you need to call FindExtensionByName().

@xfxyjwf xfxyjwf closed this as completed Sep 30, 2014
@kmatzen
Copy link
Author

kmatzen commented Sep 30, 2014

This also doesn't work:

const Descriptor* record_descriptor = record.GetDescriptor();
const FieldDescriptor* extension_field = record_descriptor->FindExtensionByName("parent");
CHECK_NOTNULL(extension_field);

Or did you not mean to use FindExtensionByName in this way?

@xfxyjwf
Copy link
Contributor

xfxyjwf commented Sep 30, 2014

Try this:

const DescriptorPool* pool = record.GetDescriptor()->file()->pool();
const FieldDescriptor* extension_field = pool->FindExtensionByName("SpecializedRecord.parent");

Note that the extension name must be fully qualified.

TeBoring pushed a commit to TeBoring/protobuf that referenced this issue Jan 19, 2019
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