zhangshenghang commented on code in PR #7361:
URL: https://github.com/apache/seatunnel/pull/7361#discussion_r1728467309


##########
seatunnel-formats/seatunnel-format-protobuf/src/main/java/org/apache/seatunnel/format/protobuf/CompileDescriptor.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.seatunnel.format.protobuf;
+
+import org.apache.seatunnel.common.utils.FileUtils;
+import org.apache.seatunnel.format.protobuf.exception.ProtobufFormatErrorCode;
+import 
org.apache.seatunnel.format.protobuf.exception.SeaTunnelProtobufFormatException;
+
+import com.github.os72.protocjar.Protoc;
+import com.google.protobuf.DescriptorProtos;
+import com.google.protobuf.Descriptors;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.List;
+
+public class CompileDescriptor {
+
+    public static Descriptors.Descriptor compileDescriptorTempFile(
+            String protoContent, String messageName)
+            throws IOException, InterruptedException, 
Descriptors.DescriptorValidationException {
+        // Because Protobuf can only be dynamically parsed through the 
descriptor file, the file
+        // needs to be compiled and generated. The following method is used 
here to solve the
+        // problem: generate a temporary directory and compile .proto into a 
descriptor temporary
+        // file. The temporary file and directory are deleted after the JVM 
runs.
+        File tmpDir = createTempDirectory();
+        File protoFile = createProtoFile(tmpDir, protoContent);
+        String targetDescPath = compileProtoToDescriptor(tmpDir, protoFile);
+
+        try (FileInputStream fis = new FileInputStream(targetDescPath)) {
+            DescriptorProtos.FileDescriptorSet descriptorSet =
+                    DescriptorProtos.FileDescriptorSet.parseFrom(fis);
+            Descriptors.FileDescriptor[] descriptorsArray = 
buildFileDescriptors(descriptorSet);
+            return descriptorsArray[0].findMessageTypeByName(messageName);
+        }
+    }
+
+    private static File createTempDirectory() throws IOException {
+        File tmpDir = File.createTempFile("tmp_protobuf_", "_proto");

Review Comment:
   > I think this does not conflict when placed in the specified temporary path?
   
   File.createTempFile("tmp_protobuf_", "_proto").getPath()
   This method generates directories or files completely randomly, and its 
structure is as follows:
   
/var/folders/ys/223gp9td7dqf4nmxl9kw25lc0000gn/T/tmp_protobuf_2079053607848123529_proto



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