Skip to content

Commit

Permalink
Allow dynamic factory to load from descriptor files
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <[email protected]>
  • Loading branch information
mjcarroll committed Mar 8, 2024
1 parent 4e73795 commit b4ac4da
Showing 1 changed file with 48 additions and 40 deletions.
88 changes: 48 additions & 40 deletions core/src/DynamicFactory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,58 +87,66 @@ void DynamicFactory::LoadDescriptors(const std::string &_paths)
std::vector<std::string> descDirs =
split(_paths, kEnvironmentVariableSeparator);


for (const std::string &descDir : descDirs)
auto loadDescFile = [this](const std::string &descFile)
{
for (auto const &dirIter : std::filesystem::directory_iterator{descDir})
std::cout << "Loading: " << descFile << std::endl;

// Ignore files without the correct extensions.
if (descFile.rfind(".desc") == std::string::npos &&
descFile.rfind(".proto") == std::string::npos &&
descFile.rfind(".proto.bin") == std::string::npos)
return;

// Parse the .desc file.
std::ifstream ifs(descFile);
if (!ifs.is_open())
{
std::cerr << "DynamicFactory(): Unable to open [" << descFile << "]"
<< std::endl;
return;

Check warning on line 106 in core/src/DynamicFactory.cc

View check run for this annotation

Codecov / codecov/patch

core/src/DynamicFactory.cc#L104-L106

Added lines #L104 - L106 were not covered by tests
}

google::protobuf::FileDescriptorSet fileDescriptorSet;
if (!fileDescriptorSet.ParseFromIstream(&ifs))
{
// Ignore files without the .desc extension.
if (dirIter.path().extension() != ".desc" &&
dirIter.path().extension() != ".gz_desc")
continue;
std::cerr << "DynamicFactory(): Unable to parse descriptor set from ["
<< descFile << "]" << std::endl;
return;

Check warning on line 114 in core/src/DynamicFactory.cc

View check run for this annotation

Codecov / codecov/patch

core/src/DynamicFactory.cc#L113-L114

Added lines #L113 - L114 were not covered by tests
}

std::ifstream ifs(dirIter.path().string(), std::ifstream::in);
if (!ifs.is_open())
// Place the real descriptors in the descriptor pool.
for (const google::protobuf::FileDescriptorProto &fileDescriptorProto :
fileDescriptorSet.file())
{
if (!static_cast<bool>(this->pool.BuildFile(fileDescriptorProto)))
{
std::cerr << "DynamicFactory(): Unable to open ["
<< dirIter.path() << "]"
<< std::endl;
continue;
std::cerr << "DynamicFactory(). Unable to place descriptors from ["
<< descFile << "] in the descriptor pool" << std::endl;

Check warning on line 124 in core/src/DynamicFactory.cc

View check run for this annotation

Codecov / codecov/patch

core/src/DynamicFactory.cc#L124

Added line #L124 was not covered by tests
}

google::protobuf::FileDescriptorSet fileDescriptorSet;
if (!fileDescriptorSet.ParseFromIstream(&ifs))
else
{
std::cerr << "DynamicFactory(): Unable to parse descriptor set from ["
<< dirIter.path() << "]" << std::endl;
continue;
this->db.Add(fileDescriptorProto);
}

for (const google::protobuf::FileDescriptorProto &fileDescriptorProto :
fileDescriptorSet.file())
{
// If the descriptor already exists in the database, then skip it.
// This may happen as gz_desc files can potentially contain the
// transitive message definitions
google::protobuf::FileDescriptorProto checkDescriptorProto;
if (this->db.FindFileByName(
fileDescriptorProto.name(), &checkDescriptorProto))
{
continue;
}

if (!static_cast<bool>(pool.BuildFile(fileDescriptorProto)))
{
std::cerr << "DynamicFactory(). Unable to place descriptors from ["
<< dirIter.path()
<< "] in the descriptor pool" << std::endl;
}
}

this->db.Add(fileDescriptorProto);
};


for (const std::string &descDir : descDirs)
{
if (!std::filesystem::is_directory(descDir))
{
loadDescFile(descDir);

Check warning on line 140 in core/src/DynamicFactory.cc

View check run for this annotation

Codecov / codecov/patch

core/src/DynamicFactory.cc#L140

Added line #L140 was not covered by tests
}
else
{
for (auto const &dirIter : std::filesystem::directory_iterator{descDir})
{
loadDescFile(dirIter.path());
}
}
}

}

//////////////////////////////////////////////////
Expand Down

0 comments on commit b4ac4da

Please sign in to comment.