-
Notifications
You must be signed in to change notification settings - Fork 6k
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
common/xmlformatter: turn on underscored and add unittest #12916
Conversation
@@ -34,8 +34,8 @@ | |||
#include <boost/format.hpp> | |||
|
|||
|
|||
static char tolower_underscore(const char b) { | |||
return ' ' == b ? '_' : std::tolower(b); | |||
static char underscore(const char b) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could move this into a private const method of XMLFormatter
, so we don't need to duplicate the same pattern everywhere. like
char to_lower_underscore(char c) const {
if (c == ' ') {
if (m_underscored) {
return '_';
}
} else if (m_lowercased) {
return tolower(c);
}
return c;
}
and do the transform in a single pass
std::transform(e.begin(), e.end(), e.begin(), [this](unsigned char c) {
return this->to_lower_underscore(c);
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@@ -173,7 +173,8 @@ namespace ceph { | |||
std::stringstream m_ss, m_pending_string; | |||
std::deque<std::string> m_sections; | |||
bool m_pretty; | |||
bool m_lowercased_underscored; | |||
bool m_lowercased; | |||
bool m_underscored; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could define them as const
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
02b27a1
to
503e394
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me
I was actually thinking we should convert all Formatter users to name fields with lowercase and _ (changing the schema) so that XMLFormatter isn't special. It seems like a better convention to follow anyway (although it is a change). What do you all think? |
@liewegas I think it's not a perfect choice. There are a lot of uppercase tag name in RadosRGW, Amazon S3 is case sensitive. So it would be better to keep simple example:
|
ok!
|
@@ -588,6 +581,17 @@ std::string XMLFormatter::escape_xml_str(const char *str) | |||
return std::string(&escaped[0]); | |||
} | |||
|
|||
char XMLFormatter::to_lower_underscore(const char c) | |||
{ | |||
if (c == ' ') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we improve the readability of this block?
if(m_underscored && c == ' ') {
return '_';
else if (m_lowercased) {
return std::tolower(c);
}
return c;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, we could, but we don't need to call std::tolower()
if c is "
".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about
if (m_underscored && c == ' ') {
return '_';
} else if (m_lowercased && isupper(c)) {
return tolower(c);
}
return c;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@robbat2 updated! take a look, please
@@ -169,11 +169,13 @@ namespace ceph { | |||
void print_spaces(); | |||
static std::string escape_xml_str(const char *str); | |||
void get_attrs_str(const FormatterAttrs *attrs, std::string& attrs_str); | |||
char to_lower_underscore(const char c); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could make this a const
method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure! i would like to update it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's done, @tchaikov
I will squash those commits after review. 😄 |
return '_'; | ||
} else if (m_lowercased) { | ||
} else if (m_lowercased && std::isupper(c)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, i don't think this improves the readability. it just hurts the performance here. but yeah, probably the performance here is not a bottleneck...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so,need i remove 'isupper' ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'd recommend so, if no objections.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std::tolower
determines whether c is space or upper actually. I will update it.
6268854
to
9923f83
Compare
#include "gtest/gtest.h" | ||
|
||
#include "common/Formatter.h" | ||
#include <iostream> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is this header for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, used in debug, remove it now.
if (m_underscored && c == ' ') { | ||
return '_'; | ||
} else if (m_lowercased) { | ||
// std::tolower will datermine whether c is space or upper. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO, this comment does not help, maybe we can drop it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
test_xmlformatter.cc | ||
) | ||
add_ceph_unittest(unittest_xmlformatter ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unittest_xmlformatter) | ||
target_link_libraries(unittest_xmlformatter global) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might want to link against ceph-common instead. test_xmlformatter.cc
is not referencing any symbols from global
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi, @tchaikov , what's global
and diff with ceph-common
, i saw that all unittest use global
in this CMakeLists.txt. Need we update other test cases?
formatter.dump_string("String", "String"); | ||
formatter.close_section(); | ||
formatter.flush(sout); | ||
std::string cmp="<xml>\n <Integer>10</Integer>\n <Float>10</Float>\n <String>String</String>\n</xml>\n\n"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, spaces around =
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
formatter.dump_float("Float", 10.0); | ||
formatter.dump_string("String", "String"); | ||
formatter.flush(sout); | ||
std::string cmp="<integer>10</integer><float>10</float><string>String</string>"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
formatter.dump_float("Float Item", 10.0); | ||
formatter.dump_string("String Item", "String"); | ||
formatter.flush(sout); | ||
std::string cmp="<integer_item>10</integer_item>\n<float_item>10</float_item>\n<string_item>String</string_item>\n\n"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto. also, could reformat the line like
std::string cmp("<integer_item>10</integer_item>\n"
"<float_item>10</float_item>\n"
"<string_item>String</string_item>\n\n");
Signed-off-by: liuchang0812 <liuchang0812@gmail.com>
9923f83
to
a7efe62
Compare
As discussed in Dev-maillist, turn on
underscored
by default.@tchaikov @liewegas @jcsp
Please take a look, thanks