opwvhk commented on code in PR #3614:
URL: https://github.com/apache/avro/pull/3614#discussion_r3252815250


##########
lang/java/gradle-plugin/src/main/kotlin/eu/eventloopsoftware/avro/gradle/plugin/tasks/CompileAvroSchemaTask.kt:
##########
@@ -0,0 +1,86 @@
+/*
+* 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
+*
+*     https://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 eu.eventloopsoftware.avro.gradle.plugin.tasks
+
+import java.io.File
+import java.io.IOException
+import org.apache.avro.Protocol
+import org.apache.avro.SchemaParseException
+import org.apache.avro.SchemaParser
+import org.apache.avro.compiler.specific.SpecificCompiler
+import org.gradle.api.GradleException
+import org.gradle.api.file.ConfigurableFileCollection
+import org.gradle.api.tasks.InputFiles
+import org.gradle.api.tasks.SkipWhenEmpty
+import org.gradle.api.tasks.TaskAction
+
+abstract class CompileAvroSchemaTask : AbstractCompileTask() {
+
+  @get:InputFiles @get:SkipWhenEmpty abstract val schemaFiles: 
ConfigurableFileCollection
+
+  @get:InputFiles @get:SkipWhenEmpty abstract val protocolFiles: 
ConfigurableFileCollection
+
+  @TaskAction
+  fun compileSchema() {
+    logger.info("Generating Java files from ${schemaFiles.files.size} Avro 
schemas...")
+
+    compileSchemas(schemaFiles, outputDirectory.get().asFile)
+
+    logger.info("Done generating Java files from Avro schemas...")
+  }
+
+  private fun compileSchemas(schemaFileTree: ConfigurableFileCollection, 
outputDirectory: File) {
+    val sourceFileForModificationDetection: File? =
+        schemaFileTree
+            .asFileTree
+            .files
+            .filter { file: File -> file.lastModified() > 0 }
+            .maxByOrNull { it.lastModified() }
+
+    // Need to register custom logical type factories before schema 
compilation.
+    try {
+      loadLogicalTypesFactories()
+    } catch (e: IOException) {
+      throw GradleException("Error while loading logical types factories ", e)
+    }
+
+    try {
+      val parser = SchemaParser()
+      for (sourceFile in schemaFileTree.files) {
+        parser.parse(sourceFile)
+      }

Review Comment:
   > This loop assumes that a single pass of all files is sufficient to parse 
all input files, however in the case of an unresolved schema exception multiple 
passes may be required.
   
   This is incorrect: the new parser delays resolving unknown schemas until 
returning them. This is also why the parse methods return a `ParseResult`: this 
is a lazy construct, and allows you to resolve the schemas later.
   
   As a bonus, this also allows resolving circular references between files. A 
loop that parses and resolves each file cannot do this, no matter how often you 
loop.



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