From 1b9ff46be0dc422a7a12ba66ca85337f959180e3 Mon Sep 17 00:00:00 2001 From: Mikolaj Jakubowski Date: Tue, 24 Dec 2024 11:42:25 +0100 Subject: [PATCH 1/2] review --- .../main/scala/generators/FileGenerator.scala | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/avrohugger-core/src/main/scala/generators/FileGenerator.scala b/avrohugger-core/src/main/scala/generators/FileGenerator.scala index 8af89486..26b5caad 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,14 @@ private[avrohugger] class FileGenerator { } } + private def distinctSchemaOrProtocol(schemaOrProtocols: List[Either[Schema, Protocol]]): List[Either[Schema, Protocol]] = { + schemaOrProtocols.map { + case Left(schema) => schema.getFullName -> Left(schema) + case Right(protocol) => Option(protocol.getNamespace).map(_ + ".").getOrElse("") + protocol.getName -> Right(protocol) + } + .toMap + .values + .toList + } + } From b0e0cbd122a1e233e2cd67bd7664d2fe5d8768d7 Mon Sep 17 00:00:00 2001 From: Mikolaj Jakubowski Date: Tue, 24 Dec 2024 14:13:34 +0100 Subject: [PATCH 2/2] review --- .../main/scala/generators/FileGenerator.scala | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/avrohugger-core/src/main/scala/generators/FileGenerator.scala b/avrohugger-core/src/main/scala/generators/FileGenerator.scala index 26b5caad..60848661 100644 --- a/avrohugger-core/src/main/scala/generators/FileGenerator.scala +++ b/avrohugger-core/src/main/scala/generators/FileGenerator.scala @@ -106,13 +106,25 @@ private[avrohugger] class FileGenerator { } private def distinctSchemaOrProtocol(schemaOrProtocols: List[Either[Schema, Protocol]]): List[Either[Schema, Protocol]] = { - schemaOrProtocols.map { - case Left(schema) => schema.getFullName -> Left(schema) - case Right(protocol) => Option(protocol.getNamespace).map(_ + ".").getOrElse("") + protocol.getName -> Right(protocol) - } - .toMap - .values - .toList + 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 + } + } } }