You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2022/07/28 02:44:17 UTC

[GitHub] [dubbo-spi-extensions] AlbumenJ commented on a diff in pull request #136: [Dubbo-10014]fix serialization of attachments in protobuf. fix issue #10014 and #10307

AlbumenJ commented on code in PR #136:
URL: https://github.com/apache/dubbo-spi-extensions/pull/136#discussion_r931731414


##########
dubbo-serialization-extensions/dubbo-serialization-protobuf/src/main/java/org/apache/dubbo/common/serialize/protobuf/support/GenericProtobufObjectInput.java:
##########
@@ -16,22 +16,15 @@
  */
 package org.apache.dubbo.common.serialize.protobuf.support;
 
+import com.google.protobuf.*;

Review Comment:
   do not use comma import



##########
dubbo-serialization-extensions/dubbo-serialization-protobuf/src/main/java/org/apache/dubbo/common/serialize/protobuf/support/ProtobufAttachmentUtils.java:
##########
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.common.serialize.protobuf.support;
+
+import com.google.protobuf.*;
+
+import org.apache.dubbo.common.serialize.protobuf.support.wrapper.MapValue;
+
+import java.io.*;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * ProtobufAttachmentUtils
+ *
+ * @date 2022-07-27
+ */
+public class ProtobufAttachmentUtils {
+    private static Map<String, BuiltinMarshaller> marshallers = new HashMap<>();
+
+    static {
+        marshaller(String.class, new StringMarshaller());
+        marshaller(Integer.class, new IntegerMarshaller());
+        marshaller(Long.class, new LongMarshaller());
+        marshaller(Boolean.class, new BooleanMarshaller());
+        marshaller(Float.class, new FloatMarshaller());
+        marshaller(Double.class, new DoubleMarshaller());
+    }
+
+    static void marshaller(Class<?> clazz, BuiltinMarshaller marshaller) {
+        marshallers.put(clazz.getCanonicalName(), marshaller);
+    }
+
+    static MapValue.Map wrap(Map<String, Object> attachments) throws IOException {
+        Map<String, Any> genericAttachments = new HashMap<>(attachments.size());
+        for (Map.Entry<String, Object> entry : attachments.entrySet()) {
+            genericAttachments.put(entry.getKey(), marshal(entry.getValue()));
+        }
+        return MapValue.Map.newBuilder().putAllAttachmentsV2(genericAttachments).build();
+    }
+
+    static Map<String, Object> unwrap(MapValue.Map map) throws InvalidProtocolBufferException {
+        Map<String, Object> attachments = new HashMap<>();
+        //compatible with older version.
+        Map<String, String> stringAttachments = map.getAttachmentsMap();
+        if (stringAttachments != null) {
+            stringAttachments.forEach((k, v) -> attachments.put(k, v));
+        }
+
+        Map<String, Any> genericAttachments = map.getAttachmentsV2Map();
+        if (genericAttachments == null) {
+            return attachments;
+        }
+        for (Map.Entry<String, Any> entry : genericAttachments.entrySet()) {
+            attachments.put(entry.getKey(), unmarshal(entry.getValue()));
+        }
+        return attachments;
+    }
+
+    private static Any marshal(Object obj) throws IOException {
+        String className = obj.getClass().getCanonicalName();
+        BuiltinMarshaller marshaller = marshallers.get(className);

Review Comment:
   add empty protection



##########
dubbo-serialization-extensions/dubbo-serialization-protobuf/src/main/java/org/apache/dubbo/common/serialize/protobuf/support/ProtobufAttachmentUtils.java:
##########
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.common.serialize.protobuf.support;
+
+import com.google.protobuf.*;
+
+import org.apache.dubbo.common.serialize.protobuf.support.wrapper.MapValue;
+
+import java.io.*;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * ProtobufAttachmentUtils
+ *
+ * @date 2022-07-27

Review Comment:
   remove this line



##########
dubbo-serialization-extensions/dubbo-serialization-protobuf/src/main/java/org/apache/dubbo/common/serialize/protobuf/support/ProtobufAttachmentUtils.java:
##########
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.common.serialize.protobuf.support;
+
+import com.google.protobuf.*;
+
+import org.apache.dubbo.common.serialize.protobuf.support.wrapper.MapValue;
+
+import java.io.*;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * ProtobufAttachmentUtils
+ *
+ * @date 2022-07-27
+ */
+public class ProtobufAttachmentUtils {
+    private static Map<String, BuiltinMarshaller> marshallers = new HashMap<>();
+
+    static {
+        marshaller(String.class, new StringMarshaller());
+        marshaller(Integer.class, new IntegerMarshaller());
+        marshaller(Long.class, new LongMarshaller());
+        marshaller(Boolean.class, new BooleanMarshaller());
+        marshaller(Float.class, new FloatMarshaller());
+        marshaller(Double.class, new DoubleMarshaller());
+    }
+
+    static void marshaller(Class<?> clazz, BuiltinMarshaller marshaller) {
+        marshallers.put(clazz.getCanonicalName(), marshaller);
+    }
+
+    static MapValue.Map wrap(Map<String, Object> attachments) throws IOException {
+        Map<String, Any> genericAttachments = new HashMap<>(attachments.size());
+        for (Map.Entry<String, Object> entry : attachments.entrySet()) {
+            genericAttachments.put(entry.getKey(), marshal(entry.getValue()));
+        }
+        return MapValue.Map.newBuilder().putAllAttachmentsV2(genericAttachments).build();

Review Comment:
   should have version forward compatibility



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org