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



##########
File path: 
flink-formats/flink-protobuf/src/main/java/org/apache/flink/formats/protobuf/PbSchemaValidator.java
##########
@@ -0,0 +1,148 @@
+/*
+ * 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.api.ValidationException;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.RowType;
+
+import com.google.protobuf.Descriptors;
+import com.google.protobuf.Descriptors.FieldDescriptor;
+import com.google.protobuf.Descriptors.FieldDescriptor.JavaType;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class PbSchemaValidator {
+       private Descriptors.Descriptor descriptor;
+       private RowType rowType;
+       private Map<JavaType, List<LogicalTypeRoot>> typeMatchMap = new 
HashMap();
+
+       public PbSchemaValidator(Descriptors.Descriptor descriptor, RowType 
rowType) {
+               this.descriptor = descriptor;
+               this.rowType = rowType;
+               typeMatchMap.put(JavaType.BOOLEAN, 
Collections.singletonList(LogicalTypeRoot.BOOLEAN));
+               typeMatchMap.put(
+                       JavaType.BYTE_STRING,
+                       Arrays.asList(LogicalTypeRoot.BINARY, 
LogicalTypeRoot.VARBINARY));
+               typeMatchMap.put(JavaType.DOUBLE, 
Collections.singletonList(LogicalTypeRoot.DOUBLE));
+               typeMatchMap.put(JavaType.FLOAT, 
Collections.singletonList(LogicalTypeRoot.FLOAT));
+               typeMatchMap.put(
+                       JavaType.ENUM,
+                       Arrays.asList(LogicalTypeRoot.VARCHAR, 
LogicalTypeRoot.CHAR));
+               typeMatchMap.put(
+                       JavaType.STRING,
+                       Arrays.asList(LogicalTypeRoot.VARCHAR, 
LogicalTypeRoot.CHAR));
+               typeMatchMap.put(JavaType.INT, 
Collections.singletonList(LogicalTypeRoot.INTEGER));
+               typeMatchMap.put(JavaType.LONG, 
Collections.singletonList(LogicalTypeRoot.BIGINT));
+       }
+
+       public Descriptors.Descriptor getDescriptor() {
+               return descriptor;
+       }
+
+       public void setDescriptor(Descriptors.Descriptor descriptor) {
+               this.descriptor = descriptor;
+       }
+
+       public RowType getRowType() {
+               return rowType;
+       }
+
+       public void setRowType(RowType rowType) {
+               this.rowType = rowType;
+       }
+
+       public void validate() {
+               validateTypeMatch(descriptor, rowType);
+               if (!descriptor.getFile().getOptions().getJavaPackage()
+                       .equals(descriptor.getFile().getPackage())) {
+                       throw new IllegalArgumentException(
+                               "java_package and package must be the same in 
proto definition");
+               }
+               if (!descriptor.getFile().getOptions().getJavaMultipleFiles()) {

Review comment:
       The complicated code is below, you can see that "OuterClass" is hard 
coded.
   ```
       public static String getJavaFullName(Descriptors.Descriptor descriptor) {
           String javaPackageName = 
descriptor.getFile().getOptions().getJavaPackage();
           if (descriptor.getFile().getOptions().getJavaMultipleFiles()) {
               //multiple_files=true
               if (null != descriptor.getContainingType()) {
                   //nested type
                   String parentJavaFullName = 
getJavaFullName(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 = 
getJavaFullName(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 + "." + descriptor.getName() + 
"OuterClass" + "." + descriptor.getName();
                   } else {
                       String outerName = 
descriptor.getFile().getOptions().getJavaOuterClassname();
                       //user define outer class name in proto file
                       return javaPackageName + "." + outerName + "." + 
descriptor.getName();
                   }
               }
           }
       }
   ```




----------------------------------------------------------------
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.

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


Reply via email to