You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@avro.apache.org by "alexrosenfeld10 (via GitHub)" <gi...@apache.org> on 2023/04/20 14:34:45 UTC

[GitHub] [avro] alexrosenfeld10 commented on a diff in pull request #1966: AVRO-3673: Add MSBuild AvroGenTask

alexrosenfeld10 commented on code in PR #1966:
URL: https://github.com/apache/avro/pull/1966#discussion_r1172691445


##########
lang/csharp/src/apache/msbuild/AvroGenTask.cs:
##########
@@ -0,0 +1,114 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+using System.IO;
+using System.Linq;
+using System.Collections.Generic;
+using Microsoft.Build.Framework;
+using Microsoft.Build.Utilities;
+
+namespace Avro.msbuild
+{
+    public class AvroGenTask : Task
+    {
+        public override bool Execute()
+        {
+            try
+            {
+                Dictionary<string, string> namespaceMapping = new Dictionary<string, string>();
+
+                MessageImportance messageImportance = string.IsNullOrEmpty(MessageLevel) ? 
+                    MessageImportance.Normal :
+                    (MessageImportance)Enum.Parse(typeof(MessageImportance), MessageLevel);
+
+                var codegen = new CodeGen();
+
+                if (!string.IsNullOrEmpty(NamespaceMapping))
+                {
+                    foreach(var mapping in NamespaceMapping.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
+                    {
+                        var parts = mapping.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
+                        if (parts.Length != 2)
+                        {
+                            throw new ArgumentException("Malformed namespace mapping. Required format is \"avro.namespace:csharp.namespace\"");
+                        }
+                        namespaceMapping[parts[0]] = parts[1];
+                    }
+                }
+
+                if (SchemaFiles != null)
+                {
+                    foreach (var schemaFile in SchemaFiles)
+                    {
+                        var schemaText = System.IO.File.ReadAllText(schemaFile.ItemSpec);
+                        codegen.AddSchema(schemaText, namespaceMapping);

Review Comment:
   @zcsizmadia I'm building something similar myself right now using C# Source Generators. I'm having an issue where references to common shared schemas don't resolve properly, and my code is pretty much the same as yours here. Does your code work in this case? Details below:
   
   
   Top level schema I want to generate C# for:
   ```yaml
   {
       "name": "MyModel",
       "namespace": "My.Model",
       "type": "record",
       "fields": [
           {
               "name": "Planet",
               "type": "My.Model.PlanetEnum",
           }
       ]
   }
   ```
   
   The `RepositoryEnum` definition, shared between multiple schemas:
   
   ```yaml
   {
     "name": "PlanetEnum",
     "namespace": "My.Model",
     "type": "enum",
     "symbols": [
       "Earth",
       "Mars"
     ]
   }
   ```
   
   
   Here's the code I'm using to generate the C#:
   
   ```csharp
           var codeCompileUnit = new CodeGen
           {
               Schemas =
               {
                   Schema.Parse(schemaText)
               },
           }.GenerateCode();
   
           var stringWriter = new StringWriter(new StringBuilder());
           var provider = CodeDomProvider.CreateProvider("csharp");
           provider.GenerateCodeFromCompileUnit(codeCompileUnit, stringWriter, new CodeGeneratorOptions());
           var schemaCode = stringWriter.ToString();
           return schemaCode;
   ```
   
   
   This fails because it can't make the connection between the two separated schema definition files. If I add multiple calls to `Schema.Parse`, it still fails. Any thoughts? Would a fix for this be worked into your PR here?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org