You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@avro.apache.org by GitBox <gi...@apache.org> on 2022/11/14 17:41:27 UTC

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

github-code-scanning[bot] commented on code in PR #1966:
URL: https://github.com/apache/avro/pull/1966#discussion_r1021870151


##########
lang/csharp/src/apache/msbuild/AvroGenTask.cs:
##########
@@ -0,0 +1,102 @@
+/**
+ * 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.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);
+                    }
+                }
+                if (ProtocolFiles != null)
+                {
+                    foreach (var protocolFile in ProtocolFiles)
+                    {
+                        var protocolText = System.IO.File.ReadAllText(protocolFile.ItemSpec);
+                        codegen.AddProtocol(protocolText, namespaceMapping);
+                    }
+                }
+
+                var generateCode = codegen.GenerateCode();
+                var namespaces = generateCode.Namespaces;
+                for (var i = namespaces.Count - 1; i >= 0; i--)
+                {
+                    var types = namespaces[i].Types;
+                    for (var j = types.Count - 1; j >= 0; j--)
+                    {
+                        Log.LogMessage(messageImportance, "Generating {0}.{1}", namespaces[i].Name, types[j].Name);
+                    }
+                }
+
+                codegen.WriteTypes(OutDir.ItemSpec, SkipDirectories);
+            }
+            catch(Exception ex)
+            {
+                Log.LogErrorFromException(ex);
+            }

Review Comment:
   ## Generic catch clause
   
   Generic catch clause.
   
   [Show more details](https://github.com/apache/avro/security/code-scanning/2947)



##########
lang/csharp/src/apache/msbuild/AvroGenTask.cs:
##########
@@ -0,0 +1,102 @@
+/**
+ * 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.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:
   ## Missed opportunity to use Select
   
   This foreach loop immediately [maps its iteration variable to another variable](1) - consider mapping the sequence explicitly using '.Select(...)'.
   
   [Show more details](https://github.com/apache/avro/security/code-scanning/2948)



-- 
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