Repository: thrift
Updated Branches:
  refs/heads/master 1568aef7d -> b3b7d0457


THRIFT-3174: Modify initialism code in Go compiler to check first word
Client: Go
Patch: Paul Magrath <[email protected]>

This closes #509


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/b3b7d045
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/b3b7d045
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/b3b7d045

Branch: refs/heads/master
Commit: b3b7d0457ae67e4eeafbd2137dd94116d4993870
Parents: 1568aef
Author: Jens Geyer <[email protected]>
Authored: Sat May 30 22:35:09 2015 +0200
Committer: Jens Geyer <[email protected]>
Committed: Sat May 30 22:56:01 2015 +0200

----------------------------------------------------------------------
 compiler/cpp/src/generate/t_go_generator.cc | 24 +++++++++++++++++-------
 lib/go/test/InitialismsTest.thrift          |  1 +
 lib/go/test/tests/initialisms_test.go       |  4 ++++
 3 files changed, 22 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/b3b7d045/compiler/cpp/src/generate/t_go_generator.cc
----------------------------------------------------------------------
diff --git a/compiler/cpp/src/generate/t_go_generator.cc 
b/compiler/cpp/src/generate/t_go_generator.cc
index a59224d..1584e91 100644
--- a/compiler/cpp/src/generate/t_go_generator.cc
+++ b/compiler/cpp/src/generate/t_go_generator.cc
@@ -302,6 +302,7 @@ private:
   std::set<std::string> commonInitialisms;
 
   std::string camelcase(const std::string& value) const;
+  void fix_common_initialism(std::string& value, int i) const;
   std::string publicize(const std::string& value, bool is_args_or_result = 
false) const;
   std::string privatize(const std::string& value) const;
   std::string new_prefix(const std::string& value) const;
@@ -419,25 +420,34 @@ bool t_go_generator::is_pointer_field(t_field* tfield, 
bool in_container_value)
 std::string t_go_generator::camelcase(const std::string& value) const {
   std::string value2(value);
   std::setlocale(LC_ALL, "C"); // set locale to classic
+  
+  // Fix common initialism in first word
+  fix_common_initialism(value2, 0);
 
-  // as long as we are changing things, let's change _ followed by lowercase 
to capital and fix
-  // common initialisms
+  // as long as we are changing things, let's change _ followed by lowercase 
to 
+  // capital and fix common initialisms
   for (std::string::size_type i = 1; i < value2.size() - 1; ++i) {
     if (value2[i] == '_') {
       if (islower(value2[i + 1])) {
         value2.replace(i, 2, 1, toupper(value2[i + 1]));
       }
-      std::string word = value2.substr(i, value2.find('_', i));
-      std::transform(word.begin(), word.end(), word.begin(), ::toupper);
-      if (commonInitialisms.find(word) != commonInitialisms.end()) {
-        value2.replace(i, word.length(), word);
-      }
+      fix_common_initialism(value2, i);
     }
   }
 
   return value2;
 }
 
+// Checks to see if the word starting at i in value contains a common 
initialism
+// and if so replaces it with the upper case version of the word.
+void t_go_generator::fix_common_initialism(std::string& value, int i) const {
+  std::string word = value.substr(i, value.find('_', i));
+  std::transform(word.begin(), word.end(), word.begin(), ::toupper);
+  if (commonInitialisms.find(word) != commonInitialisms.end()) {
+    value.replace(i, word.length(), word);
+  }
+}
+
 std::string t_go_generator::publicize(const std::string& value, bool 
is_args_or_result) const {
   if (value.size() <= 0) {
     return value;

http://git-wip-us.apache.org/repos/asf/thrift/blob/b3b7d045/lib/go/test/InitialismsTest.thrift
----------------------------------------------------------------------
diff --git a/lib/go/test/InitialismsTest.thrift 
b/lib/go/test/InitialismsTest.thrift
index 68b6350..ad86ac1 100644
--- a/lib/go/test/InitialismsTest.thrift
+++ b/lib/go/test/InitialismsTest.thrift
@@ -20,4 +20,5 @@
 struct InitialismsTest {
     1: string user_id,
     2: string server_url,
+    3: string id, 
 }

http://git-wip-us.apache.org/repos/asf/thrift/blob/b3b7d045/lib/go/test/tests/initialisms_test.go
----------------------------------------------------------------------
diff --git a/lib/go/test/tests/initialisms_test.go 
b/lib/go/test/tests/initialisms_test.go
index 1d13b97..40923d2 100644
--- a/lib/go/test/tests/initialisms_test.go
+++ b/lib/go/test/tests/initialisms_test.go
@@ -36,4 +36,8 @@ func TestThatCommonInitialismsAreFixed(t *testing.T) {
        if !ok {
                t.Error("ServerURL attribute is missing!")
        }
+       _, ok = st.FieldByName("ID")
+       if !ok {
+               t.Error("ID attribute is missing!")
+       }
 }

Reply via email to