You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by mg...@apache.org on 2022/04/25 18:37:25 UTC

[avro] branch branch-1.11 updated: AVRO-3427: skip creation of namespace directories for csharp schema (#1578)

This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch branch-1.11
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/branch-1.11 by this push:
     new 9a248d26b AVRO-3427: skip creation of namespace directories for csharp schema (#1578)
9a248d26b is described below

commit 9a248d26b1267d6d23e3ca6bde4c34b85fd355f2
Author: kordos <pk...@gmail.com>
AuthorDate: Mon Apr 25 20:36:51 2022 +0200

    AVRO-3427: skip creation of namespace directories for csharp schema (#1578)
    
    * Add new argument parameter --skip-directories. It will skip creation of directories for namespace. Just generate classes in output directory
    
    * Add missing doc param description
    
    * Fix Unit tests after merge with master
    
    * Fix Unit tests after merge with master
    
    * C# Add unit tests for --skip-directories option
    
    Co-authored-by: Pawel Kordowski <pa...@stepstone.com>
    (cherry picked from commit 5b055ac43085b11e467acd66c8630f424b1af50e)
---
 lang/csharp/src/apache/codegen/AvroGen.cs          | 14 +++++--
 lang/csharp/src/apache/main/CodeGen/CodeGen.cs     | 11 ++++--
 .../src/apache/test/AvroGen/AvroGenHelper.cs       | 11 +++---
 .../src/apache/test/AvroGen/AvroGenSchemaTests.cs  | 46 ++++++++++++++++++++--
 .../src/apache/test/AvroGen/AvroGenToolTests.cs    | 10 +++++
 5 files changed, 75 insertions(+), 17 deletions(-)

diff --git a/lang/csharp/src/apache/codegen/AvroGen.cs b/lang/csharp/src/apache/codegen/AvroGen.cs
index d11690272..3b07ca59f 100644
--- a/lang/csharp/src/apache/codegen/AvroGen.cs
+++ b/lang/csharp/src/apache/codegen/AvroGen.cs
@@ -53,6 +53,7 @@ namespace Avro
             bool? isProtocol = null;
             string inputFile = null;
             string outputDir = null;
+            bool skipDirectoriesCreation = false;
             var namespaceMapping = new Dictionary<string, string>();
             for (int i = 0; i < args.Length; ++i)
             {
@@ -99,6 +100,10 @@ namespace Avro
 
                     namespaceMapping[parts[0]] = parts[1];
                 }
+                else if (args[i] == "--skip-directories")
+                {
+                    skipDirectoriesCreation = true;
+                }
                 else if (outputDir == null)
                 {
                     outputDir = args[i];
@@ -133,7 +138,7 @@ namespace Avro
             else if (isProtocol.Value)
                 rc = GenProtocol(inputFile, outputDir, namespaceMapping);
             else
-                rc = GenSchema(inputFile, outputDir, namespaceMapping);
+                rc = GenSchema(inputFile, outputDir, namespaceMapping, skipDirectoriesCreation);
 
             return rc;
         }
@@ -149,7 +154,8 @@ namespace Avro
                 "  -V --version     Show version.\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);
         }
 
@@ -176,7 +182,7 @@ namespace Avro
         }
 
         public static int GenSchema(string infile, string outdir,
-            IEnumerable<KeyValuePair<string, string>> namespaceMapping)
+            IEnumerable<KeyValuePair<string, string>> namespaceMapping, bool skipDirectories)
         {
             try
             {
@@ -185,7 +191,7 @@ namespace Avro
                 codegen.AddSchema(text, namespaceMapping);
 
                 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 9176f4f21..0aba034bb 100644
--- a/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
+++ b/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
@@ -1140,7 +1140,8 @@ namespace Avro
         /// Writes each types in each namespaces into individual files.
         /// </summary>
         /// <param name="outputdir">name of directory to write to.</param>
-        public virtual void WriteTypes(string outputdir)
+        /// <param name="skipDirectories">skip creation of directories based on schema namespace</param>
+        public virtual void WriteTypes(string outputdir, bool skipDirectories = false)
         {
             var cscp = new CSharpCodeProvider();
 
@@ -1155,11 +1156,13 @@ namespace Avro
                 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);
diff --git a/lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs b/lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs
index 49865c170..04f88617a 100644
--- a/lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs
+++ b/lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs
@@ -139,7 +139,7 @@ namespace Avro.Test.AvroGen
             }
         }
 
-        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 @@ namespace Avro.Test.AvroGen
             string schema,
             IEnumerable<string> typeNamesToCheck = null,
             IEnumerable<KeyValuePair<string, string>> namespaceMapping = null,
-            IEnumerable<string> generatedFilesToCheck = null)
+            IEnumerable<string> 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 @@ namespace Avro.Test.AvroGen
                 System.IO.File.WriteAllText(schemaFileName, schema);
 
                 // Generate from schema file
-                Assert.That(AvroGenTool.GenSchema(schemaFileName, outputDir, namespaceMapping ?? new Dictionary<string, string>()), Is.EqualTo(0));
+                Assert.That(AvroGenTool.GenSchema(schemaFileName, outputDir, namespaceMapping ?? new Dictionary<string, string>(), skipDirectories), Is.EqualTo(0));
 
                 return CompileCSharpFilesAndCheckTypes(outputDir, uniqueId, typeNamesToCheck, generatedFilesToCheck);
             }
@@ -263,7 +264,7 @@ namespace Avro.Test.AvroGen
             IEnumerable<string> 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 912284d60..e31dc383b 100644
--- a/lang/csharp/src/apache/test/AvroGen/AvroGenSchemaTests.cs
+++ b/lang/csharp/src/apache/test/AvroGen/AvroGenSchemaTests.cs
@@ -316,7 +316,7 @@ namespace Avro.Test.AvroGen
             IEnumerable<string> generatedFilesToCheck = null)
         {
             // Create temp folder
-            string outputDir = AvroGenHelper.CreateEmptyTemporyFolder(out string uniqueId);
+            string outputDir = AvroGenHelper.CreateEmptyTemporaryFolder(out string uniqueId);
 
             try
             {
@@ -325,7 +325,7 @@ namespace Avro.Test.AvroGen
                 System.IO.File.WriteAllText(schemaFileName, schema);
 
                 // Generate from schema file
-                Assert.That(AvroGenTool.GenSchema(schemaFileName, outputDir, namespaceMapping ?? new Dictionary<string, string>()), Is.EqualTo(0));
+                Assert.That(AvroGenTool.GenSchema(schemaFileName, outputDir, namespaceMapping ?? new Dictionary<string, string>(), false), Is.EqualTo(0));
 
                 // Check if all generated files exist
                 if (generatedFilesToCheck != null)
@@ -611,7 +611,7 @@ namespace Avro.Test.AvroGen
         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
             {
@@ -619,7 +619,7 @@ namespace Avro.Test.AvroGen
                 string schemaFileName = Path.Combine(outputDir, $"{uniqueId}.avsc");
                 System.IO.File.WriteAllText(schemaFileName, schema);
 
-                Assert.That(AvroGenTool.GenSchema(schemaFileName, outputDir, new Dictionary<string, string>()), Is.EqualTo(1));
+                Assert.That(AvroGenTool.GenSchema(schemaFileName, outputDir, new Dictionary<string, string>(), false), Is.EqualTo(1));
             }
             finally
             {
@@ -771,5 +771,43 @@ namespace Avro.Test.AvroGen
                 }
             }
         }
+
+        [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<string> typeNamesToCheck, IEnumerable<string> 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 c1433bcc0..698ff468c 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 @@ namespace Avro.Test.AvroGen
             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")));
+        }
     }
 }