Updated Proto <-> JSON conversion to use base64 for 'bytes' fields.

Review: https://reviews.apache.org/r/37558


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

Branch: refs/heads/master
Commit: 584301f9913eed5c281dc574954feabc48402e80
Parents: 1e21d49
Author: Benjamin Mahler <[email protected]>
Authored: Mon Aug 17 15:12:04 2015 -0700
Committer: Benjamin Mahler <[email protected]>
Committed: Mon Aug 17 17:05:25 2015 -0700

----------------------------------------------------------------------
 .../3rdparty/stout/include/stout/protobuf.hpp   | 47 +++++++++++++-------
 .../3rdparty/stout/tests/protobuf_tests.cpp     |  4 +-
 2 files changed, 34 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/584301f9/3rdparty/libprocess/3rdparty/stout/include/stout/protobuf.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/protobuf.hpp 
b/3rdparty/libprocess/3rdparty/stout/include/stout/protobuf.hpp
index 82eae87..57d5fdf 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/protobuf.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/protobuf.hpp
@@ -24,24 +24,21 @@
 #include <string>
 #include <vector>
 
-#include <boost/lexical_cast.hpp>
-
-#include <glog/logging.h>
-
 #include <google/protobuf/descriptor.h>
 #include <google/protobuf/message.h>
 #include <google/protobuf/repeated_field.h>
 
 #include <google/protobuf/io/zero_copy_stream_impl.h>
 
-#include "abort.hpp"
-#include "error.hpp"
-#include "json.hpp"
-#include "none.hpp"
-#include "os.hpp"
-#include "result.hpp"
-#include "stringify.hpp"
-#include "try.hpp"
+#include <stout/abort.hpp>
+#include <stout/base64.hpp>
+#include <stout/error.hpp>
+#include <stout/json.hpp>
+#include <stout/none.hpp>
+#include <stout/os.hpp>
+#include <stout/result.hpp>
+#include <stout/stringify.hpp>
+#include <stout/try.hpp>
 
 namespace protobuf {
 
@@ -345,13 +342,27 @@ struct Parser : boost::static_visitor<Try<Nothing> >
   {
     switch (field->type()) {
       case google::protobuf::FieldDescriptor::TYPE_STRING:
-      case google::protobuf::FieldDescriptor::TYPE_BYTES:
         if (field->is_repeated()) {
           reflection->AddString(message, field, string.value);
         } else {
           reflection->SetString(message, field, string.value);
         }
         break;
+      case google::protobuf::FieldDescriptor::TYPE_BYTES: {
+        Try<std::string> decode = base64::decode(string.value);
+
+        if (decode.isError()) {
+          return Error("Failed to base64 decode bytes field"
+                       " '" + field->name() + "': " + decode.error());
+        }
+
+        if (field->is_repeated()) {
+          reflection->AddString(message, field, decode.get());
+        } else {
+          reflection->SetString(message, field, decode.get());
+        }
+        break;
+      }
       case google::protobuf::FieldDescriptor::TYPE_ENUM: {
         const google::protobuf::EnumValueDescriptor* descriptor =
           field->enum_type()->FindValueByName(string.value);
@@ -638,10 +649,13 @@ struct Protobuf
               }
               break;
             case google::protobuf::FieldDescriptor::TYPE_STRING:
-            case google::protobuf::FieldDescriptor::TYPE_BYTES:
               array.values.push_back(JSON::String(
                   reflection->GetRepeatedString(message, field, i)));
               break;
+            case google::protobuf::FieldDescriptor::TYPE_BYTES:
+              array.values.push_back(JSON::String(base64::encode(
+                  reflection->GetRepeatedString(message, field, i))));
+              break;
             case google::protobuf::FieldDescriptor::TYPE_MESSAGE:
               array.values.push_back(Protobuf(
                   reflection->GetRepeatedMessage(message, field, i)));
@@ -698,10 +712,13 @@ struct Protobuf
             }
             break;
           case google::protobuf::FieldDescriptor::TYPE_STRING:
-          case google::protobuf::FieldDescriptor::TYPE_BYTES:
             object.values[field->name()] =
                 JSON::String(reflection->GetString(message, field));
             break;
+          case google::protobuf::FieldDescriptor::TYPE_BYTES:
+            object.values[field->name()] = JSON::String(
+                base64::encode(reflection->GetString(message, field)));
+            break;
           case google::protobuf::FieldDescriptor::TYPE_MESSAGE:
             object.values[field->name()] =
                 Protobuf(reflection->GetMessage(message, field));

http://git-wip-us.apache.org/repos/asf/mesos/blob/584301f9/3rdparty/libprocess/3rdparty/stout/tests/protobuf_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/tests/protobuf_tests.cpp 
b/3rdparty/libprocess/3rdparty/stout/tests/protobuf_tests.cpp
index 2b98945..c56d6a3 100644
--- a/3rdparty/libprocess/3rdparty/stout/tests/protobuf_tests.cpp
+++ b/3rdparty/libprocess/3rdparty/stout/tests/protobuf_tests.cpp
@@ -81,7 +81,7 @@ TEST(ProtobufTest, JSON)
   string expected = strings::remove(
       "{"
       "  \"b\": true,"
-      "  \"bytes\": \"bytes\","
+      "  \"bytes\": \"Ynl0ZXM=\","
       "  \"d\": 1,"
       "  \"e\": \"ONE\","
       "  \"f\": 1,"
@@ -90,7 +90,7 @@ TEST(ProtobufTest, JSON)
       "  \"nested\": { \"str\": \"nested\"},"
       "  \"optional_default\": 42,"
       "  \"repeated_bool\": [true],"
-      "  \"repeated_bytes\": [\"repeated_bytes\"],"
+      "  \"repeated_bytes\": [\"cmVwZWF0ZWRfYnl0ZXM=\"],"
       "  \"repeated_double\": [1, 2],"
       "  \"repeated_enum\": [\"TWO\"],"
       "  \"repeated_float\": [1],"

Reply via email to