diff --git a/lang/csharp/src/apache/codegen/AvroGen.cs b/lang/csharp/src/apache/codegen/AvroGen.cs index d1169027269..3b07ca59fdc 100644 --- a/lang/csharp/src/apache/codegen/AvroGen.cs +++ b/lang/csharp/src/apache/codegen/AvroGen.cs @@ -53,6 +53,7 @@ public 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) { @@ -99,6 +100,10 @@ public 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]; @@ -133,7 +138,7 @@ public 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; } @@ -149,7 +154,8 @@ static void Usage() " -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 @@ public static int GenProtocol(string infile, string outdir, } public static int GenSchema(string infile, string outdir, - IEnumerable> namespaceMapping) + IEnumerable> namespaceMapping, bool skipDirectories) { try { @@ -185,7 +191,7 @@ public static int GenSchema(string infile, string outdir, 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 9176f4f2152..0aba034bb4c 100644 --- a/lang/csharp/src/apache/main/CodeGen/CodeGen.cs +++ b/lang/csharp/src/apache/main/CodeGen/CodeGen.cs @@ -1140,7 +1140,8 @@ 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) + /// skip creation of directories based on schema namespace + public virtual void WriteTypes(string outputdir, bool skipDirectories = false) { var cscp = new CSharpCodeProvider(); @@ -1155,11 +1156,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); diff --git a/lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs b/lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs index 49865c17052..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()), 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 912284d6052..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 { @@ -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) @@ -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 { @@ -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 { @@ -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"))); + } } }