From 03e5c9b48e2a5b8aa49da0063828510752f323b6 Mon Sep 17 00:00:00 2001 From: Pawel Kordowski Date: Thu, 3 Mar 2022 14:23:46 +0100 Subject: [PATCH 1/5] Add new argument parameter --skip-directories. It will skip creation of directories for namespace. Just generate classes in output directory --- lang/csharp/src/apache/codegen/AvroGen.cs | 16 ++++++++++++---- lang/csharp/src/apache/main/CodeGen/CodeGen.cs | 10 ++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lang/csharp/src/apache/codegen/AvroGen.cs b/lang/csharp/src/apache/codegen/AvroGen.cs index 349911754e9..933dd90966e 100644 --- a/lang/csharp/src/apache/codegen/AvroGen.cs +++ b/lang/csharp/src/apache/codegen/AvroGen.cs @@ -43,6 +43,7 @@ static int Main(string[] args) bool? isProtocol = null; string inputFile = null; string outputDir = null; + bool skipDirectoriesCreation = false; var namespaceMapping = new Dictionary(); for (int i = 0; i < args.Length; ++i) { @@ -89,6 +90,10 @@ static int Main(string[] args) namespaceMapping[parts[0]] = parts[1]; } + else if (args[i] == "--skip-directories") + { + skipDirectoriesCreation = true; + } else if (outputDir == null) { outputDir = args[i]; @@ -123,7 +128,7 @@ static int Main(string[] args) else if (isProtocol.Value) rc = GenProtocol(inputFile, outputDir, namespaceMapping); else - rc = GenSchema(inputFile, outputDir, namespaceMapping); + rc = GenSchema(inputFile, outputDir, namespaceMapping, skipDirectoriesCreation); return rc; } @@ -138,7 +143,9 @@ static void Usage() " -h --help Show this screen.\n" + " --namespace Map an Avro schema/protocol namespace to a C# namespace.\n" + " The format is \"my.avro.namespace:my.csharp.namespace\".\n" + - " May be specified multiple times to map multiple namespaces.\n", + " May be specified multiple times to map multiple namespaces.\n" + + " --skip-directories Skip creation of namespace directories. It will generate classes right inside output directory\n", + AppDomain.CurrentDomain.FriendlyName); return; } @@ -168,7 +175,8 @@ static int GenProtocol(string infile, string outdir, return 0; } static int GenSchema(string infile, string outdir, - IEnumerable> namespaceMapping) + IEnumerable> namespaceMapping, + bool skipDirectories) { try { @@ -182,7 +190,7 @@ static int GenSchema(string infile, string outdir, codegen.NamespaceMapping[entry.Key] = entry.Value; codegen.GenerateCode(); - codegen.WriteTypes(outdir); + codegen.WriteTypes(outdir, skipDirectories); } catch (Exception ex) { diff --git a/lang/csharp/src/apache/main/CodeGen/CodeGen.cs b/lang/csharp/src/apache/main/CodeGen/CodeGen.cs index 922bfe02fda..030077f7f1f 100644 --- a/lang/csharp/src/apache/main/CodeGen/CodeGen.cs +++ b/lang/csharp/src/apache/main/CodeGen/CodeGen.cs @@ -1110,7 +1110,7 @@ public virtual void WriteCompileUnit(string outputFile) /// Writes each types in each namespaces into individual files. /// /// name of directory to write to. - public virtual void WriteTypes(string outputdir) + public virtual void WriteTypes(string outputdir, bool skipDirectories = false) { var cscp = new CSharpCodeProvider(); @@ -1125,11 +1125,13 @@ public virtual void WriteTypes(string outputdir) var ns = nsc[i]; string dir = outputdir; - foreach (string name in CodeGenUtil.Instance.UnMangle(ns.Name).Split('.')) + if (skipDirectories != true) { - dir = Path.Combine(dir, name); + foreach (string name in CodeGenUtil.Instance.UnMangle(ns.Name).Split('.')) + { + dir = Path.Combine(dir, name); + } } - Directory.CreateDirectory(dir); var new_ns = new CodeNamespace(ns.Name); From 66afbe23527fd911e448174f08e01665f1a89d6a Mon Sep 17 00:00:00 2001 From: Pawel Kordowski Date: Thu, 3 Mar 2022 15:34:53 +0100 Subject: [PATCH 2/5] Add missing doc param description --- lang/csharp/src/apache/main/CodeGen/CodeGen.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/lang/csharp/src/apache/main/CodeGen/CodeGen.cs b/lang/csharp/src/apache/main/CodeGen/CodeGen.cs index 030077f7f1f..61b473b85c4 100644 --- a/lang/csharp/src/apache/main/CodeGen/CodeGen.cs +++ b/lang/csharp/src/apache/main/CodeGen/CodeGen.cs @@ -1110,6 +1110,7 @@ public virtual void WriteCompileUnit(string outputFile) /// Writes each types in each namespaces into individual files. /// /// name of directory to write to. + /// skip creation of directories based on schema namespace public virtual void WriteTypes(string outputdir, bool skipDirectories = false) { var cscp = new CSharpCodeProvider(); From abdac2761333194f0cbbf293b306fd6c47aeb876 Mon Sep 17 00:00:00 2001 From: Pawel Kordowski Date: Wed, 20 Apr 2022 10:05:17 +0200 Subject: [PATCH 3/5] Fix Unit tests after merge with master --- lang/csharp/src/apache/test/AvroGen/AvroGenTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lang/csharp/src/apache/test/AvroGen/AvroGenTests.cs b/lang/csharp/src/apache/test/AvroGen/AvroGenTests.cs index 1f61fc547dc..bf54a3a8027 100644 --- a/lang/csharp/src/apache/test/AvroGen/AvroGenTests.cs +++ b/lang/csharp/src/apache/test/AvroGen/AvroGenTests.cs @@ -325,7 +325,7 @@ private Assembly TestSchema( System.IO.File.WriteAllText(schemaFileName, schema); // Generate from schema file - Assert.That(AvroGenTool.GenSchema(schemaFileName, outputDir, namespaceMapping ?? new Dictionary()), Is.EqualTo(0)); + Assert.That(AvroGenTool.GenSchema(schemaFileName, outputDir, namespaceMapping ?? new Dictionary(), false), Is.EqualTo(0)); // Check if all generated files exist if (generatedFilesToCheck != null) @@ -619,7 +619,7 @@ public void NotSupportedSchema(string schema, Type expectedException) string schemaFileName = Path.Combine(outputDir, $"{uniqueId}.avsc"); System.IO.File.WriteAllText(schemaFileName, schema); - Assert.That(AvroGenTool.GenSchema(schemaFileName, outputDir, new Dictionary()), Is.EqualTo(1)); + Assert.That(AvroGenTool.GenSchema(schemaFileName, outputDir, new Dictionary(), false), Is.EqualTo(1)); } finally { From 2b8ce67d300615144963b92221220a958e37addd Mon Sep 17 00:00:00 2001 From: Pawel Kordowski Date: Wed, 20 Apr 2022 10:11:58 +0200 Subject: [PATCH 4/5] Fix Unit tests after merge with master --- lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs b/lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs index 49865c17052..4db6484558f 100644 --- a/lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs +++ b/lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs @@ -246,7 +246,7 @@ public static Assembly TestSchema( System.IO.File.WriteAllText(schemaFileName, schema); // Generate from schema file - Assert.That(AvroGenTool.GenSchema(schemaFileName, outputDir, namespaceMapping ?? new Dictionary()), Is.EqualTo(0)); + Assert.That(AvroGenTool.GenSchema(schemaFileName, outputDir, namespaceMapping ?? new Dictionary(), false), Is.EqualTo(0)); return CompileCSharpFilesAndCheckTypes(outputDir, uniqueId, typeNamesToCheck, generatedFilesToCheck); } From d6cd3f1f8ac80742c1e7722c8e72d5b9a0aa60e6 Mon Sep 17 00:00:00 2001 From: Pawel Kordowski Date: Mon, 25 Apr 2022 11:39:20 +0200 Subject: [PATCH 5/5] C# Add unit tests for --skip-directories option --- .../src/apache/test/AvroGen/AvroGenHelper.cs | 11 ++--- .../apache/test/AvroGen/AvroGenSchemaTests.cs | 42 ++++++++++++++++++- .../apache/test/AvroGen/AvroGenToolTests.cs | 10 +++++ 3 files changed, 56 insertions(+), 7 deletions(-) diff --git a/lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs b/lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs index 4db6484558f..04f88617a65 100644 --- a/lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs +++ b/lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs @@ -139,7 +139,7 @@ public static Assembly CompileCSharpFilesIntoLibrary(IEnumerable sourceF } } - public static string CreateEmptyTemporyFolder(out string uniqueId, string path = null) + public static string CreateEmptyTemporaryFolder(out string uniqueId, string path = null) { // Create unique id uniqueId = Guid.NewGuid().ToString(); @@ -234,10 +234,11 @@ public static Assembly TestSchema( string schema, IEnumerable typeNamesToCheck = null, IEnumerable> namespaceMapping = null, - IEnumerable generatedFilesToCheck = null) + IEnumerable generatedFilesToCheck = null, + bool skipDirectories = false) { // Create temp folder - string outputDir = CreateEmptyTemporyFolder(out string uniqueId); + string outputDir = CreateEmptyTemporaryFolder(out string uniqueId); try { @@ -246,7 +247,7 @@ public static Assembly TestSchema( System.IO.File.WriteAllText(schemaFileName, schema); // Generate from schema file - Assert.That(AvroGenTool.GenSchema(schemaFileName, outputDir, namespaceMapping ?? new Dictionary(), false), Is.EqualTo(0)); + Assert.That(AvroGenTool.GenSchema(schemaFileName, outputDir, namespaceMapping ?? new Dictionary(), skipDirectories), Is.EqualTo(0)); return CompileCSharpFilesAndCheckTypes(outputDir, uniqueId, typeNamesToCheck, generatedFilesToCheck); } @@ -263,7 +264,7 @@ public static Assembly TestProtocol( IEnumerable generatedFilesToCheck = null) { // Create temp folder - string outputDir = CreateEmptyTemporyFolder(out string uniqueId); + string outputDir = CreateEmptyTemporaryFolder(out string uniqueId); try { diff --git a/lang/csharp/src/apache/test/AvroGen/AvroGenSchemaTests.cs b/lang/csharp/src/apache/test/AvroGen/AvroGenSchemaTests.cs index 062f5832aa1..e31dc383bc4 100644 --- a/lang/csharp/src/apache/test/AvroGen/AvroGenSchemaTests.cs +++ b/lang/csharp/src/apache/test/AvroGen/AvroGenSchemaTests.cs @@ -316,7 +316,7 @@ private Assembly TestSchema( IEnumerable generatedFilesToCheck = null) { // Create temp folder - string outputDir = AvroGenHelper.CreateEmptyTemporyFolder(out string uniqueId); + string outputDir = AvroGenHelper.CreateEmptyTemporaryFolder(out string uniqueId); try { @@ -611,7 +611,7 @@ public void GenerateSchemaWithNamespaceMapping( public void NotSupportedSchema(string schema, Type expectedException) { // Create temp folder - string outputDir = AvroGenHelper.CreateEmptyTemporyFolder(out string uniqueId); + string outputDir = AvroGenHelper.CreateEmptyTemporaryFolder(out string uniqueId); try { @@ -771,5 +771,43 @@ public void GenerateSchemaCheckFields(string schema, object[] result) } } } + + [TestCase( + _nullableLogicalTypesArray, + new string[] + { + "org.apache.avro.codegentest.testdata.NullableLogicalTypesArray" + }, + new string[] + { + "NullableLogicalTypesArray.cs" + })] + [TestCase( + _nestedSomeNamespaceRecord, + new string[] + { + "org.apache.avro.codegentest.some.NestedSomeNamespaceRecord", + "org.apache.avro.codegentest.other.NestedOtherNamespaceRecord" + }, + new string[] + { + "NestedSomeNamespaceRecord.cs", + "NestedOtherNamespaceRecord.cs" + })] + [TestCase(_schema_avro_2883, + new string[] + { + "my.avro.ns.TestModel", + "my.avro.ns.EventType", + }, + new string[] + { + "TestModel.cs", + "EventType.cs" + })] + public void GenerateSchemaWithSkipDirectoriesOption(string schema, IEnumerable typeNamesToCheck, IEnumerable generatedFilesToCheck) + { + AvroGenHelper.TestSchema(schema, typeNamesToCheck, generatedFilesToCheck: generatedFilesToCheck, skipDirectories: true); + } } } diff --git a/lang/csharp/src/apache/test/AvroGen/AvroGenToolTests.cs b/lang/csharp/src/apache/test/AvroGen/AvroGenToolTests.cs index c1433bcc057..698ff468c2d 100644 --- a/lang/csharp/src/apache/test/AvroGen/AvroGenToolTests.cs +++ b/lang/csharp/src/apache/test/AvroGen/AvroGenToolTests.cs @@ -16,6 +16,7 @@ * limitations under the License. */ using System; +using System.Linq; using System.Reflection; using NUnit.Framework; @@ -89,5 +90,14 @@ public void CommandLineInvalidArgs(params string[] args) Assert.That(result.StdOut, Is.Not.Empty); Assert.That(result.StdErr, Is.Not.Empty); } + + [Theory] + public void CommandLineHelpContainsSkipDirectoriesParameter() + { + AvroGenToolResult result = AvroGenHelper.RunAvroGenTool("-h"); + + Assert.That(result.ExitCode, Is.EqualTo(0)); + Assert.IsTrue(result.StdOut.Any(s => s.Contains("--skip-directories"))); + } } }