maosuhan commented on a change in pull request #14376:
URL: https://github.com/apache/flink/pull/14376#discussion_r767101531



##########
File path: 
flink-formats/flink-protobuf/src/main/java/org/apache/flink/formats/protobuf/PbFormatUtils.java
##########
@@ -0,0 +1,166 @@
+/*
+ * 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.flink.formats.protobuf;
+
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import com.google.protobuf.Descriptors;
+import org.apache.commons.lang3.StringUtils;
+
+/** Protobuf function util. */
+public class PbFormatUtils {
+
+    /**
+     * protobuf code has a bug that, f_abc_7d will be convert to fAbc7d in 
{@code
+     * com.google.protobuf.Descriptors.FileDescriptor.getJsonName()}, but 
actually we need fAbc7D.
+     */
+    public static String fieldNameToJsonName(String name) {
+        final int length = name.length();
+        StringBuilder result = new StringBuilder(length);
+        boolean isNextUpperCase = false;
+        for (int i = 0; i < length; i++) {
+            char ch = name.charAt(i);
+            if (ch == '_') {
+                isNextUpperCase = true;
+            } else if (isNextUpperCase) {
+                if ('a' <= ch && ch <= 'z') {
+                    ch = (char) (ch - 'a' + 'A');
+                    isNextUpperCase = false;
+                }
+                result.append(ch);
+            } else {
+                result.append(ch);
+            }
+        }
+        return result.toString();
+    }
+
+    private static String getJavaPackageFromProtoFile(Descriptors.Descriptor 
descriptor) {
+        boolean hasJavaPackage = 
descriptor.getFile().getOptions().hasJavaPackage();
+        if (hasJavaPackage) {
+            String javaPackage = 
descriptor.getFile().getOptions().getJavaPackage();
+            if (StringUtils.isBlank(javaPackage)) {
+                throw new FlinkRuntimeException("java_package cannot be blank 
string");
+            }
+            return javaPackage;
+        } else {
+            String packageName = descriptor.getFile().getPackage();
+            if (StringUtils.isBlank(packageName)) {
+                throw new FlinkRuntimeException("package and java_package 
cannot both be empty");
+            }
+            return packageName;
+        }
+    }
+
+    public static String getFullJavaName(Descriptors.Descriptor descriptor) {
+        String javaPackageName = getJavaPackageFromProtoFile(descriptor);
+        if (descriptor.getFile().getOptions().getJavaMultipleFiles()) {
+            // multiple_files=true
+            if (null != descriptor.getContainingType()) {
+                // nested type
+                String parentJavaFullName = 
getFullJavaName(descriptor.getContainingType());
+                return parentJavaFullName + "." + descriptor.getName();
+            } else {
+                // top level message
+                return javaPackageName + "." + descriptor.getName();
+            }
+        } else {
+            // multiple_files=false
+            if (null != descriptor.getContainingType()) {
+                // nested type
+                String parentJavaFullName = 
getFullJavaName(descriptor.getContainingType());
+                return parentJavaFullName + "." + descriptor.getName();
+            } else {
+                // top level message
+                if 
(!descriptor.getFile().getOptions().hasJavaOuterClassname()) {
+                    // user do not define outer class name in proto file
+                    return javaPackageName

Review comment:
       @MyLanPangzi Thanks for your findings. Yes, it is a bug. I found that if 
the proto file name is test.proto and the content is as below, the outermost 
class name will be TestOuterClass because the message name is the same with 
file name.
   ```
   syntax = "proto2";
   message Test {
   ....
   }
   ```
   
   It is a little tricky to get java full class name from the 
`Descriptors.Descriptor`, so I use a simpler way that I extract the outer 
prefix name from `protobuf.message-class-name`.
   ```
       public static String getOuterProtoPrefix(String name) {
           name = name.replace('$', '.');
           int index = name.lastIndexOf('.');
           if (index != -1) {
               // include dot
               return name.substring(0, index + 1);
           } else {
               return "";
           }
       }
   ``` 
   I have fixed that issue and added additional unit tests.




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to