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

MINIFICPP-14: Prune class names. Add option to config #643

Merged
merged 2 commits into from Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions libminifi/include/utils/ClassUtils.h
Expand Up @@ -27,11 +27,12 @@ namespace utils {
namespace ClassUtils {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cool to have, I like it!
I also think some of the tempalte functions we already have (getClassName<T> and getClassNames<T>) could also be moved to here from classloader.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. made a follow on.


/**
* Shortens class names via the canonical representation
* Shortens class names via the canonical representation ( package with name )
* @param class_name input class name
* @param out output class name that is shortened.
* @return true if out has been updated, false otherwise
*/
extern void shortenClassName(const std::string &class_name, std::string &out);
bool shortenClassName(const std::string &class_name, std::string &out);

} /* namespace ClassUtils */
} /* namespace utils */
Expand Down
15 changes: 11 additions & 4 deletions libminifi/src/utils/ClassUtils.cpp
Expand Up @@ -26,13 +26,19 @@ namespace nifi {
namespace minifi {
namespace utils {

void ClassUtils::shortenClassName(const std::string &class_name, std::string &out) {
bool ClassUtils::shortenClassName(const std::string &class_name, std::string &out) {
std::string class_delim = "::";
auto class_split = utils::StringUtils::split(class_name, class_delim);
// support . and ::
if (class_split.size() <= 1 && class_name.find(".") != std::string::npos) {
class_delim = ".";
class_split = utils::StringUtils::split(class_name, class_delim);
if (class_split.size() <= 1) {
if (class_name.find(".") != std::string::npos) {
class_delim = ".";
class_split = utils::StringUtils::split(class_name, class_delim);
} else {
// if no update can be performed, return false to let the developer know
// this. Out will have no updates
return false;
}
}
for (auto &elem : class_split) {
if (&elem != &class_split.back() && elem.size() > 1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tricky :)

Expand All @@ -41,6 +47,7 @@ void ClassUtils::shortenClassName(const std::string &class_name, std::string &ou
}

out = utils::StringUtils::join(class_delim, class_split);
return true;
}

} /* namespace utils */
Expand Down
3 changes: 1 addition & 2 deletions libminifi/test/TestBase.h
Expand Up @@ -124,8 +124,7 @@ class LogTestController {
// also support shortened classnames
if (config && config->shortenClassNames()) {
std::string adjusted = name;
utils::ClassUtils::shortenClassName(name, adjusted);
if (name != adjusted) {
if (utils::ClassUtils::shortenClassName(name, adjusted)) {
modified_loggers.insert(name);
setLevel(name, level);
}
Expand Down
22 changes: 14 additions & 8 deletions libminifi/test/unit/ClassUtilsTests.cpp
Expand Up @@ -26,30 +26,36 @@ TEST_CASE("Test ShortNames", "[testcrc1]") {
SECTION("EMPTY") {
className = "";
adjusted = "";
utils::ClassUtils::shortenClassName(className, adjusted);
REQUIRE(!utils::ClassUtils::shortenClassName(className, adjusted));
REQUIRE(adjusted.empty());
}

SECTION("SINGLE") {
className = "Class";
adjusted = "Class";
utils::ClassUtils::shortenClassName(className, adjusted);
REQUIRE(className == adjusted);
adjusted = "";
// class name not shortened
REQUIRE(!utils::ClassUtils::shortenClassName(className, adjusted));
REQUIRE(adjusted.empty());
className = "org::Test";
adjusted = "";
REQUIRE(utils::ClassUtils::shortenClassName(className, adjusted));
REQUIRE("o::Test" == adjusted);
}



SECTION("MULTIPLE") {
className = "org::apache::Test";
adjusted = "";
utils::ClassUtils::shortenClassName(className, adjusted);
REQUIRE(utils::ClassUtils::shortenClassName(className, adjusted));
REQUIRE("o::a::Test" == adjusted);
className = "org.apache.Test";
adjusted = "";
utils::ClassUtils::shortenClassName(className, adjusted);
REQUIRE(utils::ClassUtils::shortenClassName(className, adjusted));
REQUIRE("o.a.Test" == adjusted);
className = adjusted;
adjusted = "";
utils::ClassUtils::shortenClassName(className, adjusted);
REQUIRE(utils::ClassUtils::shortenClassName(className, adjusted));
REQUIRE("o.a.Test" == adjusted);
}
}