Skip to content

Commit

Permalink
QSocModuleManager: Add Function to Fetch Module Nodes by Regex
Browse files Browse the repository at this point in the history
- Implemented `getModuleNode` function in QSocModuleManager class. This
  function retrieves YAML nodes for modules matching the given regular
  expression. It provides a mechanism to query specific module data
  within the module library based on regex patterns.
- The function defaults to fetching all modules if no regex is
  specified, returning an empty node for invalid or unmatched regex.
- Ensured the implementation checks for regex validity using
  QStaticRegex::isNameRegexValid before proceeding with data retrieval.
- Added corresponding function declaration in qsocmodulemanager.h with
  a detailed Doxygen comment explaining its purpose and usage.

This addition enhances the module data querying capabilities of
QSocModuleManager, allowing for more flexible and targeted data
retrieval based on regex patterns.

Signed-off-by: Huang Rui <[email protected]>
  • Loading branch information
vowstar committed Nov 25, 2023
1 parent 53ac585 commit 11f98ba
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/common/qsocmodulemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,3 +639,27 @@ bool QSocModuleManager::removeModule(const QRegularExpression &moduleNameRegex)

return true;
}

YAML::Node QSocModuleManager::getModuleNode(const QRegularExpression &moduleNameRegex)
{
YAML::Node result;

/* Check if the regex is valid, if not, return an empty node */
if (!QStaticRegex::isNameRegexValid(moduleNameRegex)) {
qWarning() << "Invalid regular expression provided.";
return result;
}

/* Iterate over the moduleData to find matches */
for (YAML::const_iterator it = moduleData.begin(); it != moduleData.end(); ++it) {
const QString moduleName = QString::fromStdString(it->first.as<std::string>());

/* Check if the module name matches the regex */
if (QStaticRegex::isNameExactMatch(moduleName, moduleNameRegex)) {
/* Add the module node to the result */
result[moduleName.toStdString()] = it->second;
}
}

return result;
}
14 changes: 14 additions & 0 deletions src/common/qsocmodulemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,20 @@ public slots:
*/
bool removeModule(const QRegularExpression &moduleNameRegex = QRegularExpression(".*"));

/**
* @brief Retrieve YAML node for modules matching the regex
* @details Fetches and returns the YAML node for modules whose names
* match the provided regular expression. This allows for
* specific querying and manipulation of module data within
* the module library. Defaults to fetching all module nodes
* if no regex is specified.
* @param moduleNameRegex Regex used to filter the module names.
* Default is ".*", which matches all modules.
* @return YAML::Node The YAML node(s) corresponding to the matched
* module(s). Returns an empty node if no match is found.
*/
YAML::Node getModuleNode(const QRegularExpression &moduleNameRegex = QRegularExpression(".*"));

private:
/* Internal used project manager */
QSocProjectManager *projectManager = nullptr;
Expand Down

0 comments on commit 11f98ba

Please sign in to comment.