qingyu31 commented on code in PR #136: URL: https://github.com/apache/dubbo-spi-extensions/pull/136#discussion_r931869147
########## 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: - [x] It's always ok when an old version calls out. - [x] it's also ok when a new version calls a new version. - [ ] but it can not work perfectly when a new version calls an old version. consumer never knows old or new version that provider depends on. if a non-string value is provided and we cannot continue to throw exception, we have two choice below. 1. ignore the non-string value and pass string value through 2. provider lose all attachment without compatible in case 1, it works when all attachment values are string but works unexpected when non-string values are provided. developers may be confused and the type of attachment value is not in their control. in case 2, it doesn't work at all. the new call the old is always forbidden. Do you have a suggestion? -- 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-unsubscr...@dubbo.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org For additional commands, e-mail: notifications-h...@dubbo.apache.org