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

Bazel updates for Harmonic #397

Merged
merged 26 commits into from
Apr 5, 2024
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Allow dynamic factory to load from descriptor files
Signed-off-by: Michael Carroll <mjcarroll@intrinsic.ai>
  • Loading branch information
mjcarroll committed Mar 8, 2024
commit b4ac4da0ac1babf6c0a41f679e721479764974c6
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 @@
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
Loading