Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lang/csharp/src/apache/codegen/AvroGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>();
for (int i = 0; i < args.Length; ++i)
{
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}

Expand All @@ -176,7 +182,7 @@ public static int GenProtocol(string infile, string outdir,
}

public static int GenSchema(string infile, string outdir,
IEnumerable<KeyValuePair<string, string>> namespaceMapping)
IEnumerable<KeyValuePair<string, string>> namespaceMapping, bool skipDirectories)
{
try
{
Expand All @@ -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)
{
Expand Down
11 changes: 7 additions & 4 deletions lang/csharp/src/apache/main/CodeGen/CodeGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,8 @@ public virtual void WriteCompileUnit(string outputFile)
/// 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();

Expand All @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public static Assembly CompileCSharpFilesIntoLibrary(IEnumerable<string> 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();
Expand Down Expand Up @@ -234,10 +234,11 @@ public static Assembly TestSchema(
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
{
Expand All @@ -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<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);
}
Expand All @@ -263,7 +264,7 @@ public static Assembly TestProtocol(
IEnumerable<string> generatedFilesToCheck = null)
{
// Create temp folder
string outputDir = CreateEmptyTemporyFolder(out string uniqueId);
string outputDir = CreateEmptyTemporaryFolder(out string uniqueId);

try
{
Expand Down
46 changes: 42 additions & 4 deletions lang/csharp/src/apache/test/AvroGen/AvroGenSchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ private Assembly TestSchema(
IEnumerable<string> generatedFilesToCheck = null)
{
// Create temp folder
string outputDir = AvroGenHelper.CreateEmptyTemporyFolder(out string uniqueId);
string outputDir = AvroGenHelper.CreateEmptyTemporaryFolder(out string uniqueId);

try
{
Expand All @@ -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<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)
Expand Down Expand Up @@ -611,15 +611,15 @@ 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
{
// Save schema
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
{
Expand Down Expand Up @@ -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<string> typeNamesToCheck, IEnumerable<string> generatedFilesToCheck)
{
AvroGenHelper.TestSchema(schema, typeNamesToCheck, generatedFilesToCheck: generatedFilesToCheck, skipDirectories: true);
}
}
}
10 changes: 10 additions & 0 deletions lang/csharp/src/apache/test/AvroGen/AvroGenToolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* limitations under the License.
*/
using System;
using System.Linq;
using System.Reflection;
using NUnit.Framework;

Expand Down Expand Up @@ -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")));
}
}
}