diff --git a/avrohugger-core/src/main/scala/generators/FileGenerator.scala b/avrohugger-core/src/main/scala/generators/FileGenerator.scala index 8af89486..60848661 100644 --- a/avrohugger-core/src/main/scala/generators/FileGenerator.scala +++ b/avrohugger-core/src/main/scala/generators/FileGenerator.scala @@ -56,13 +56,13 @@ private[avrohugger] class FileGenerator { typeMatcher: TypeMatcher, restrictedFields: Boolean, targetScalaPartialVersion: String): Unit = { - val schemaOrProtocols = stringParser.getSchemaOrProtocols(str, schemaStore).distinct - schemaOrProtocols.foreach { - case Left(schema) => - schemaToFile(schema, outDir, format, classStore, schemaStore, typeMatcher, restrictedFields, targetScalaPartialVersion) - case Right(protocol) => - protocolToFile(protocol, outDir, format, classStore, schemaStore, typeMatcher, restrictedFields, targetScalaPartialVersion) - } + distinctSchemaOrProtocol(stringParser.getSchemaOrProtocols(str, schemaStore)) + .foreach { + case Left(schema) => + schemaToFile(schema, outDir, format, classStore, schemaStore, typeMatcher, restrictedFields, targetScalaPartialVersion) + case Right(protocol) => + protocolToFile(protocol, outDir, format, classStore, schemaStore, typeMatcher, restrictedFields, targetScalaPartialVersion) + } } def fileToFile( @@ -76,8 +76,7 @@ private[avrohugger] class FileGenerator { classLoader: ClassLoader, restrictedFields: Boolean, targetScalaPartialVersion: String): Unit = { - fileParser.getSchemaOrProtocols(inFile, format, classStore, classLoader) - .distinct + distinctSchemaOrProtocol(fileParser.getSchemaOrProtocols(inFile, format, classStore, classLoader)) .foreach { case Left(schema) => schemaToFile(schema, outDir, format, classStore, schemaStore, typeMatcher, restrictedFields, targetScalaPartialVersion) @@ -97,8 +96,7 @@ private[avrohugger] class FileGenerator { classLoader: ClassLoader, restrictedFields: Boolean, targetScalaPartialVersion: String): Unit = { - inFiles.flatMap(fileParser.getSchemaOrProtocols(_, format, classStore, classLoader)) - .distinct + distinctSchemaOrProtocol(inFiles.flatMap(fileParser.getSchemaOrProtocols(_, format, classStore, classLoader))) .foreach { case Left(schema) => schemaToFile(schema, outDir, format, classStore, schemaStore, typeMatcher, restrictedFields, targetScalaPartialVersion) @@ -107,4 +105,26 @@ private[avrohugger] class FileGenerator { } } + private def distinctSchemaOrProtocol(schemaOrProtocols: List[Either[Schema, Protocol]]): List[Either[Schema, Protocol]] = { + var processed = Set.empty[String] + + schemaOrProtocols.flatMap { + case Left(schema) => + if (!processed.contains(schema.getFullName)) { + processed += schema.getFullName + Some(Left(schema)) + } else { + None + } + case Right(protocol) => + val fullName = Option(protocol.getNamespace).map(ns => s"$ns.${protocol.getName}").getOrElse(protocol.getName) + if (!processed.contains(fullName)) { + processed += fullName + Some(Right(protocol)) + } else { + None + } + } + } + }