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

Projects
None yet
5 participants
@Liuchang0812
Contributor

Liuchang0812 commented Jan 13, 2017

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) {

This comment has been minimized.

@tchaikov

tchaikov Jan 13, 2017

Contributor

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

This comment has been minimized.

@Liuchang0812

This comment has been minimized.

@Liuchang0812

Liuchang0812 Jan 13, 2017

Contributor

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;

This comment has been minimized.

@tchaikov

tchaikov Jan 13, 2017

Contributor

could define them as const.

This comment has been minimized.

@Liuchang0812

Liuchang0812 Jan 13, 2017

Contributor

Done

@tchaikov tchaikov added the common label Jan 13, 2017

@jcsp

jcsp approved these changes Jan 13, 2017

Looks good to me

@tchaikov tchaikov added the needs-qa label Jan 13, 2017

@tchaikov tchaikov self-assigned this Jan 13, 2017

@liewegas

This comment has been minimized.

Member

liewegas commented Jan 13, 2017

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

This comment has been minimized.

Contributor

Liuchang0812 commented Jan 13, 2017

@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

This comment has been minimized.

Member

liewegas commented Jan 13, 2017

asdf

@@ -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 == ' ') {

This comment has been minimized.

@robbat2

robbat2 Jan 15, 2017

Contributor

Can we improve the readability of this block?

if(m_underscored && c == ' ') {
  return '_';
else if (m_lowercased) {
  return std::tolower(c);
}
return c;

This comment has been minimized.

@tchaikov

tchaikov Jan 15, 2017

Contributor

yeah, we could, but we don't need to call std::tolower() if c is "".

This comment has been minimized.

@Liuchang0812

Liuchang0812 Jan 15, 2017

Contributor

how about

if (m_underscored && c == ' ') {
   return '_';
} else if (m_lowercased && isupper(c)) {
 return tolower(c);
}
return c;

This comment has been minimized.

@Liuchang0812

Liuchang0812 Jan 16, 2017

Contributor

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

This comment has been minimized.

@tchaikov

tchaikov Jan 16, 2017

Contributor

could make this a const method.

This comment has been minimized.

@Liuchang0812

Liuchang0812 Jan 16, 2017

Contributor

sure! i would like to update it

This comment has been minimized.

@Liuchang0812

Liuchang0812 Jan 16, 2017

Contributor

it's done, @tchaikov

@tchaikov tchaikov removed their assignment Jan 16, 2017

@Liuchang0812

This comment has been minimized.

Contributor

Liuchang0812 commented Jan 16, 2017

I will squash those commits after review. 😄

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

This comment has been minimized.

@tchaikov

tchaikov Jan 16, 2017

Contributor

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

This comment has been minimized.

@Liuchang0812

Liuchang0812 Jan 16, 2017

Contributor

so,need i remove 'isupper' ?

This comment has been minimized.

@tchaikov

tchaikov Jan 16, 2017

Contributor

i'd recommend so, if no objections.

This comment has been minimized.

@Liuchang0812

Liuchang0812 Jan 16, 2017

Contributor

std::tolower determines whether c is space or upper actually. I will update it.

@Liuchang0812

This comment has been minimized.

Contributor

Liuchang0812 commented Jan 16, 2017

@robbat2 @tchaikov Updated!

#include "gtest/gtest.h"
#include "common/Formatter.h"
#include <iostream>

This comment has been minimized.

@tchaikov

tchaikov Jan 16, 2017

Contributor

what is this header for?

This comment has been minimized.

@Liuchang0812

Liuchang0812 Jan 17, 2017

Contributor

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.

This comment has been minimized.

@tchaikov

tchaikov Jan 16, 2017

Contributor

IMHO, this comment does not help, maybe we can drop it.

This comment has been minimized.

@Liuchang0812
test_xmlformatter.cc
)
add_ceph_unittest(unittest_xmlformatter ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unittest_xmlformatter)
target_link_libraries(unittest_xmlformatter global)

This comment has been minimized.

@tchaikov

tchaikov Jan 16, 2017

Contributor

might want to link against ceph-common instead. test_xmlformatter.cc is not referencing any symbols from global.

This comment has been minimized.

@Liuchang0812

Liuchang0812 Jan 17, 2017

Contributor

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

This comment has been minimized.

@tchaikov

tchaikov Jan 16, 2017

Contributor

nit, spaces around =

This comment has been minimized.

@Liuchang0812
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>";

This comment has been minimized.

@tchaikov

tchaikov Jan 16, 2017

Contributor

ditto.

This comment has been minimized.

@Liuchang0812
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";

This comment has been minimized.

@tchaikov

tchaikov Jan 16, 2017

Contributor

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");
common/xmlformatter: turn on underscored by default, add unittest
Signed-off-by: liuchang0812 <liuchang0812@gmail.com>
@Liuchang0812

This comment has been minimized.

Contributor

Liuchang0812 commented Jan 18, 2017

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

@tchaikov tchaikov merged commit 47c59be into ceph:master Jan 24, 2017

3 checks passed

Signed-off-by all commits in this PR are signed
Details
Unmodifed Submodules submodules for project are unmodified
Details
default Build finished.
Details

@Liuchang0812 Liuchang0812 deleted the Liuchang0812:xmlformatter-fix branch Jan 24, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment