[ 
https://issues.apache.org/jira/browse/AVRO-2156?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16710154#comment-16710154
 ] 

ASF GitHub Bot commented on AVRO-2156:
--------------------------------------

dkulp closed pull request #297: AVRO-2156: Map Avro namespace to C# namespaces 
during code generation
URL: https://github.com/apache/avro/pull/297
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/lang/csharp/src/apache/codegen/AvroGen.cs 
b/lang/csharp/src/apache/codegen/AvroGen.cs
index 033b8404b..465857f60 100644
--- a/lang/csharp/src/apache/codegen/AvroGen.cs
+++ b/lang/csharp/src/apache/codegen/AvroGen.cs
@@ -25,25 +25,111 @@ class AvroGen
     {
         static void Main(string[] args)
         {
-            if (args.Length != 3)
+            // Print usage if no arguments provided or help requested
+            if (args.Length == 0 || args[0] == "-h" || args[0] == "--help")
             {
                 Usage();
                 return;
             }
-            if (args[0] == "-p")
-                GenProtocol(args[1], args[2]);
-            else if (args[0] == "-s")
-                GenSchema(args[1], args[2]);
-            else
+
+            // Parse command line arguments
+            bool? isProtocol = null;
+            string inputFile = null;
+            string outputDir = null;
+            var namespaceMapping = new Dictionary<string, string>();
+            for (int i = 0; i < args.Length; ++i)
+            {
+                if (args[i] == "-p")
+                {
+                    if (i + 1 >= args.Length)
+                    {
+                        Console.WriteLine("Missing path to protocol file");
+                        Usage();
+                        return;
+                    }
+
+                    isProtocol = true;
+                    inputFile = args[++i];
+                }
+                else if (args[i] == "-s")
+                {
+                    if (i + 1 >= args.Length)
+                    {
+                        Console.WriteLine("Missing path to schema file");
+                        Usage();
+                        return;
+                    }
+
+                    isProtocol = false;
+                    inputFile = args[++i];
+                }
+                else if (args[i] == "--namespace")
+                {
+                    if (i + 1 >= args.Length)
+                    {
+                        Console.WriteLine("Missing namespace mapping");
+                        Usage();
+                        return;
+                    }
+
+                    var parts = args[++i].Split(new char[] { ':' }, 
StringSplitOptions.RemoveEmptyEntries);
+                    if (parts.Length != 2)
+                    {
+                        Console.WriteLine("Malformed namespace mapping. 
Required format is \"avro.namespace:csharp.namespace\"");
+                        Usage();
+                        return;
+                    }
+
+                    namespaceMapping[parts[0]] = parts[1];
+                }
+                else if (outputDir == null)
+                {
+                    outputDir = args[i];
+                }
+                else
+                {
+                    Console.WriteLine("Unexpected command line argument: {0}", 
args[i]);
+                    Usage();
+                }
+            }
+
+            // Ensure we got all the command line arguments we need
+            bool isValid = true;
+            if (!isProtocol.HasValue || inputFile == null)
+            {
+                Console.WriteLine("Must provide either '-p <protocolfile>' or 
'-s <schemafile>'");
+                isValid = false;
+            }
+            else if (outputDir == null)
+            {
+                Console.WriteLine("Must provide 'outputdir'");
+                isValid = false;
+            }
+
+            if (!isValid)
                 Usage();
+            else if (isProtocol.Value)
+                GenProtocol(inputFile, outputDir, namespaceMapping);
+            else
+                GenSchema(inputFile, outputDir, namespaceMapping);
         }
 
         static void Usage()
         {
-            Console.WriteLine("Usage:\navrogen -p <protocolfile> 
<outputdir>\navrogen -s <schemafile> <outputdir>");
+            Console.WriteLine("{0}\n\n" +
+                "Usage:\n" +
+                "  avrogen -p <protocolfile> <outputdir> [--namespace 
<my.avro.ns:my.csharp.ns>]\n" +
+                "  avrogen -s <schemafile> <outputdir> [--namespace 
<my.avro.ns:my.csharp.ns>]\n\n" +
+                "Options:\n" +
+                "  -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",
+                AppDomain.CurrentDomain.FriendlyName);
             return;
         }
-        static void GenProtocol(string infile, string outdir)
+        static void GenProtocol(string infile, string outdir,
+            IEnumerable<KeyValuePair<string, string>> namespaceMapping)
         {
             try
             {
@@ -53,6 +139,9 @@ static void GenProtocol(string infile, string outdir)
                 CodeGen codegen = new CodeGen();
                 codegen.AddProtocol(protocol);
 
+                foreach (var entry in namespaceMapping)
+                    codegen.NamespaceMapping[entry.Key] = entry.Value;
+
                 codegen.GenerateCode();
                 codegen.WriteTypes(outdir);
             }
@@ -61,7 +150,8 @@ static void GenProtocol(string infile, string outdir)
                 Console.WriteLine("Exception occurred. " + ex.Message);
             }
         }
-        static void GenSchema(string infile, string outdir)
+        static void GenSchema(string infile, string outdir,
+            IEnumerable<KeyValuePair<string, string>> namespaceMapping)
         {
             try
             {
@@ -71,6 +161,9 @@ static void GenSchema(string infile, string outdir)
                 CodeGen codegen = new CodeGen();
                 codegen.AddSchema(schema);
 
+                foreach (var entry in namespaceMapping)
+                    codegen.NamespaceMapping[entry.Key] = entry.Value;
+
                 codegen.GenerateCode();
                 codegen.WriteTypes(outdir);
             }
diff --git a/lang/csharp/src/apache/main/CodeGen/CodeGen.cs 
b/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
index 7e70a02d5..294671dff 100644
--- a/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
+++ b/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
@@ -44,6 +44,11 @@ public class CodeGen
         /// </summary>
         public IList<Protocol> Protocols { get; private set; }
 
+        /// <summary>
+        /// Mapping of Avro namespaces to C# namespaces
+        /// </summary>
+        public IDictionary<string, string> NamespaceMapping { get; private 
set; }
+
         /// <summary>
         /// List of generated namespaces
         /// </summary>
@@ -56,6 +61,7 @@ public CodeGen()
         {
             this.Schemas = new List<Schema>();
             this.Protocols = new List<Protocol>();
+            this.NamespaceMapping = new Dictionary<string, string>();
         }
 
         /// <summary>
@@ -90,7 +96,11 @@ protected virtual CodeNamespace addNamespace(string name)
 
             if (!namespaceLookup.TryGetValue(name, out ns))
             {
-                ns = new CodeNamespace(CodeGenUtil.Instance.Mangle(name));
+                string csharpNamespace;
+                ns = NamespaceMapping.TryGetValue(name, out csharpNamespace)
+                    ? new CodeNamespace(csharpNamespace)
+                    : new CodeNamespace(CodeGenUtil.Instance.Mangle(name));
+
                 foreach (CodeNamespaceImport nci in 
CodeGenUtil.Instance.NamespaceImports)
                     ns.Imports.Add(nci);
 
diff --git a/lang/csharp/src/apache/test/CodGen/CodeGenTest.cs 
b/lang/csharp/src/apache/test/CodGen/CodeGenTest.cs
index c5428b31e..f1141dbb3 100644
--- a/lang/csharp/src/apache/test/CodGen/CodeGenTest.cs
+++ b/lang/csharp/src/apache/test/CodGen/CodeGenTest.cs
@@ -98,6 +98,43 @@ public static void TestCodeGen(string str, object[] result)
             }
         }
 
+        [TestCase(@"{
+""type"": ""fixed"",
+""namespace"": ""com.base"",
+""name"": ""MD5"",
+""size"": 16
+}", null, null, "com.base")]
+        [TestCase(@"{
+""type"": ""fixed"",
+""namespace"": ""com.base"",
+""name"": ""MD5"",
+""size"": 16
+}", "com.base", "SchemaTest", "SchemaTest")]
+        [TestCase(@"{
+""type"": ""fixed"",
+""namespace"": ""com.base"",
+""name"": ""MD5"",
+""size"": 16
+}", "miss", "SchemaTest", "com.base")]
+        public void TestCodeGenNamespaceMapping(string str, string 
avroNamespace, string csharpNamespace,
+            string expectedNamespace)
+        {
+            Schema schema = Schema.Parse(str);
+
+            var codegen = new CodeGen();
+            codegen.AddSchema(schema);
+
+            if (avroNamespace != null && csharpNamespace != null)
+            {
+                codegen.NamespaceMapping[avroNamespace] = csharpNamespace;
+            }
+
+            var results = GenerateAssembly(codegen);
+            foreach(var type in results.CompiledAssembly.GetTypes())
+            {
+                Assert.AreEqual(expectedNamespace, type.Namespace);
+            }
+        }
 
         private static CompilerResults GenerateSchema(Schema schema)
         {


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> Allow custom namespace for C# generated code
> --------------------------------------------
>
>                 Key: AVRO-2156
>                 URL: https://issues.apache.org/jira/browse/AVRO-2156
>             Project: Apache Avro
>          Issue Type: New Feature
>          Components: csharp
>    Affects Versions: 1.8.2
>            Reporter: Brian Lachniet
>            Priority: Major
>
> The namespace used in Avro schemas/protocols does not always map nicely to a 
> standard C# namespace. Avrogen (the C# code generator) currently uses the 
> Avro namespace as the C# namespace. I propose that we update the avrogen 
> utility to allow the user to override the namespace used in the generated 
> code via a command line parameter.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to