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

how to get value with fieldname like protobuf in c++ #928

Closed
zhuxiufenghust opened this issue Aug 18, 2019 · 2 comments
Closed

how to get value with fieldname like protobuf in c++ #928

zhuxiufenghust opened this issue Aug 18, 2019 · 2 comments

Comments

@zhuxiufenghust
Copy link

No description provided.

@zhuxiufenghust
Copy link
Author

in c++ i can get the value with name like this:
const auto reflection = pMessage->GetReflection();
std::vector<const FieldDescriptor*> fields;
pMessage->GetReflection()->ListFields(pMessage, &fields);
const auto fieldIter = std::find_if(fields.cbegin(), fields.cend(),
[&lcFieldName](const FieldDescriptor
next) {
return boost::iequals(next->name(), lcFieldName);
});
if (fieldIter != fields.cend()) {
std::string result;
auto fieldDescriptor = *fieldIter;
if (!fieldDescriptor->is_repeated()) {
switch (fieldDescriptor->cpp_type()) {
case FieldDescriptor::CPPTYPE_INT32:
result = std::to_string(reflection->GetInt32 (
*pMessage, fieldDescriptor));
break;
case FieldDescriptor::CPPTYPE_INT64:
result = std::to_string(reflection->GetInt64 (
*pMessage, fieldDescriptor));
break;
case FieldDescriptor::CPPTYPE_UINT32:
result = std::to_string(reflection->GetUInt32 (
*pMessage, fieldDescriptor));
break;
case FieldDescriptor::CPPTYPE_UINT64:
result = std::to_string(reflection->GetUInt64 (
*pMessage, fieldDescriptor));
break;
case FieldDescriptor::CPPTYPE_DOUBLE:
result = std::to_string(reflection->GetDouble (
*pMessage, fieldDescriptor));
break;
case FieldDescriptor::CPPTYPE_FLOAT:
result = std::to_string(reflection->GetFloat (
*pMessage, fieldDescriptor));
break;
...
}
is there any api like GetFloat which can be used to get the message field value?

@dsnet
Copy link
Member

dsnet commented Aug 18, 2019

Unfortunately not. What you are asking for is called protobuf reflection, which does not exist in the current API. There is a new major re-implementation of Go protobufs underway that should be released in the upcoming months. It's flagship feature is protobuf reflection.

Closing as duplicate of #364

@dsnet dsnet closed this as completed Aug 18, 2019
@golang golang locked and limited conversation to collaborators Jun 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants