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

common/xmlformatter: turn on underscored and add unittest #12916

Merged
merged 1 commit into from
Jan 24, 2017

Conversation

liuchang0812
Copy link
Contributor

As discussed in Dev-maillist, turn on underscored by default.

@tchaikov @liewegas @jcsp

Please take a look, thanks

@@ -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) {
Copy link
Contributor

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);
      });

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK!

Copy link
Contributor Author

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;
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

@jcsp jcsp left a 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

@tchaikov tchaikov self-assigned this Jan 13, 2017
@liewegas
Copy link
Member

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?

@liuchang0812
Copy link
Contributor Author

@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 lowercased false and underscored true.

simple example:

HTTP/1.1 200 OK
x-amz-id-2: eftixk72aD6Ap51TnqcoF8eFidJG9Z/2mkiDFu8yU9AS1ed4OpIszj7UDNEHGran
x-amz-request-id: 318BC8BC148832E5
Date: Wed, 28 Oct 2009 22:32:00 GMT
Last-Modified: Sun, 1 Jan 2006 12:00:00 GMT
x-amz-version-id: 3/L4kqtJlcpXroDTDmJ+rmSpXd3dIbrHY+MTRCxf3vjVBH40Nr8X8gdRQBpUMLUo
Content-Length: 124
Content-Type: text/plain
Connection: close
Server: AmazonS3
 
<AccessControlPolicy>
  <Owner>
    <ID>75aa57f09aa0c8caeab4f8c24e99d10f8e7faeebf76c078efc7c6caea54ba06a</ID>
    <DisplayName>mdtd@amazon.com</DisplayName>
  </Owner>
  <AccessControlList>
    <Grant>
      <Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">
        <ID>75aa57f09aa0c8caeab4f8c24e99d10f8e7faeebf76c078efc7c6caea54ba06a</ID>
        <DisplayName>mdtd@amazon.com</DisplayName>
      </Grantee>
      <Permission>FULL_CONTROL</Permission>
    </Grant>
  </AccessControlList>
</AccessControlPolicy>

@liewegas
Copy link
Member

liewegas commented Jan 13, 2017 via email

liewegas
liewegas previously approved these changes Jan 13, 2017
@@ -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 == ' ') {
Copy link
Contributor

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;

Copy link
Contributor

@tchaikov tchaikov Jan 15, 2017

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 " ".

Copy link
Contributor Author

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;

Copy link
Contributor Author

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);
Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's done, @tchaikov

@tchaikov tchaikov removed their assignment Jan 16, 2017
@liuchang0812
Copy link
Contributor Author

I will squash those commits after review. 😄

return '_';
} else if (m_lowercased) {
} else if (m_lowercased && std::isupper(c)) {
Copy link
Contributor

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...

Copy link
Contributor Author

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' ?

Copy link
Contributor

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.

Copy link
Contributor Author

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.

@liuchang0812
Copy link
Contributor Author

@robbat2 @tchaikov Updated!

#include "gtest/gtest.h"

#include "common/Formatter.h"
#include <iostream>
Copy link
Contributor

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?

Copy link
Contributor Author

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.
Copy link
Contributor

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.

Copy link
Contributor Author

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)
Copy link
Contributor

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.

Copy link
Contributor Author

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";
Copy link
Contributor

Choose a reason for hiding this comment

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

nit, spaces around =

Copy link
Contributor Author

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>";
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto.

Copy link
Contributor Author

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";
Copy link
Contributor

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>
@liuchang0812
Copy link
Contributor Author

@tchaikov @liewegas I have updated PR. TALP. 😄

@tchaikov tchaikov merged commit 47c59be into ceph:master Jan 24, 2017
@liuchang0812 liuchang0812 deleted the xmlformatter-fix branch January 24, 2017 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants