You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by mg...@apache.org on 2022/04/13 18:34:10 UTC

[avro] branch master updated: AVRO-3453: Add GeneratedCodeAttribute to generated code by avrogen (#1605)

This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/master by this push:
     new 8c778c307 AVRO-3453: Add GeneratedCodeAttribute to generated code by avrogen (#1605)
8c778c307 is described below

commit 8c778c307ca34dce2190c2a4250ff607d5c9ab41
Author: Kyle Schoonover <ky...@minmaxcorp.com>
AuthorDate: Wed Apr 13 11:34:03 2022 -0700

    AVRO-3453: Add GeneratedCodeAttribute to generated code by avrogen (#1605)
    
    * AVRO-3360 Updated XML documentation
    
    * Revert "AVRO-3360 Updated XML documentation"
    
    This reverts commit b8601c072a5083380d30b580804dd0908b8cf4cc.
    
    * AVRO-3453 Add GeneratedCodeAttribute to generated code by avrogen
    
    * Fixed tabs
    
    * Removed using and fully qualified the attribute
    
    * Added global
    
    * Update version display
    
    * Updated way to get version.
    
    * Add assembly for netcore 3.1
    
    * Removed preprocessor
    
    * Simplified readability of GetGeneratedCodeAttribute
    
    * Added tabs
    
    * Added validation step to verify the autogenerated attribute exists
    
    Co-authored-by: Kyle T. Schoonover <Ky...@nordstrom.com>
---
 lang/csharp/src/apache/main/CodeGen/CodeGen.cs     |  5 +++
 lang/csharp/src/apache/main/CodeGen/CodeGenUtil.cs | 45 +++++++++++++++++++++-
 .../src/apache/test/AvroGen/AvroGenHelper.cs       |  1 +
 .../csharp/src/apache/test/AvroGen/AvroGenTests.cs |  9 ++++-
 4 files changed, 56 insertions(+), 4 deletions(-)

diff --git a/lang/csharp/src/apache/main/CodeGen/CodeGen.cs b/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
index 922bfe02f..e2438d980 100644
--- a/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
+++ b/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
@@ -400,6 +400,7 @@ namespace Avro
             ctd.IsPartial = true;
             ctd.Attributes = MemberAttributes.Public;
             ctd.BaseTypes.Add("SpecificFixed");
+            ctd.CustomAttributes.Add(CodeGenUtil.Instance.GeneratedCodeAttribute);
 
             if (fixedSchema.Documentation != null)
             {
@@ -463,6 +464,7 @@ namespace Avro
             CodeTypeDeclaration ctd = new CodeTypeDeclaration(CodeGenUtil.Instance.Mangle(enumschema.Name));
             ctd.IsEnum = true;
             ctd.Attributes = MemberAttributes.Public;
+            ctd.CustomAttributes.Add(CodeGenUtil.Instance.GeneratedCodeAttribute);
 
             if (enumschema.Documentation != null)
             {
@@ -505,6 +507,7 @@ namespace Avro
             ctd.TypeAttributes = TypeAttributes.Abstract | TypeAttributes.Public;
             ctd.IsClass = true;
             ctd.BaseTypes.Add("Avro.Specific.ISpecificProtocol");
+            ctd.CustomAttributes.Add(CodeGenUtil.Instance.GeneratedCodeAttribute);
 
             AddProtocolDocumentation(protocol, ctd);
 
@@ -583,6 +586,7 @@ namespace Avro
             ctd.TypeAttributes = TypeAttributes.Abstract | TypeAttributes.Public;
             ctd.IsClass = true;
             ctd.BaseTypes.Add(protocolNameMangled);
+            ctd.CustomAttributes.Add(CodeGenUtil.Instance.GeneratedCodeAttribute);
 
             // Need to override
             AddProtocolDocumentation(protocol, ctd);
@@ -729,6 +733,7 @@ namespace Avro
                 isError ? typeof(Specific.SpecificException) : typeof(Specific.ISpecificRecord),
                 CodeTypeReferenceOptions.GlobalReference);
             ctd.BaseTypes.Add(baseTypeReference);
+            ctd.CustomAttributes.Add(CodeGenUtil.Instance.GeneratedCodeAttribute);
 
             ctd.Attributes = MemberAttributes.Public;
             ctd.IsClass = true;
diff --git a/lang/csharp/src/apache/main/CodeGen/CodeGenUtil.cs b/lang/csharp/src/apache/main/CodeGen/CodeGenUtil.cs
index fd2823d17..633995998 100644
--- a/lang/csharp/src/apache/main/CodeGen/CodeGenUtil.cs
+++ b/lang/csharp/src/apache/main/CodeGen/CodeGenUtil.cs
@@ -15,9 +15,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
+using System.CodeDom;
+using System.CodeDom.Compiler;
 using System.Collections.Generic;
+using System.Reflection;
 using System.Text;
-using System.CodeDom;
 
 namespace Avro
 {
@@ -58,8 +61,17 @@ namespace Avro
         /// </value>
         public HashSet<string> ReservedKeywords { get; private set; }
 
+        /// <summary>
+        /// Gets the generated code attribute.
+        /// </summary>
+        /// <value>
+        /// The generated code attribute.
+        /// </value>
+        public CodeAttributeDeclaration GeneratedCodeAttribute { get; private set; }
+
         private const char At = '@';
         private const char Dot = '.';
+        private readonly string _assemblyInformationVersion = GetInformationalVersion();
 
         /// <summary>
         /// Fully-qualified name of a <see cref="Object" /> type.
@@ -81,7 +93,7 @@ namespace Avro
             FileComment = new CodeCommentStatement(
 @"------------------------------------------------------------------------------
  <auto-generated>
-    Generated by " + System.AppDomain.CurrentDomain.FriendlyName + ", version " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version + @"
+    Generated by " + System.AppDomain.CurrentDomain.FriendlyName + ", version " + _assemblyInformationVersion + @"
     Changes to this file may cause incorrect behavior and will be lost if code
     is regenerated
  </auto-generated>
@@ -101,6 +113,8 @@ namespace Avro
                 "string", "struct", "switch", "this", "throw", "true", "try", "typeof", "uint", "ulong",
                 "unchecked", "unsafe", "ushort", "using", "virtual", "void", "volatile", "while",
                 "__arglist", "__makeref", "__reftype", "__refvalue" };
+
+            GeneratedCodeAttribute = GetGeneratedCodeAttribute();
         }
 
         /// <summary>
@@ -136,5 +150,32 @@ namespace Avro
                     builder.Append(name[i]);
             return builder.ToString();
         }
+
+        private CodeAttributeDeclaration GetGeneratedCodeAttribute()
+        {
+            GeneratedCodeAttribute generatedCodeAttribute =
+                new GeneratedCodeAttribute(System.AppDomain.CurrentDomain.FriendlyName,
+                _assemblyInformationVersion);
+
+            CodePrimitiveExpression tool = new CodePrimitiveExpression(generatedCodeAttribute.Tool);
+            CodePrimitiveExpression version = new CodePrimitiveExpression(generatedCodeAttribute.Version);
+
+            CodeAttributeDeclaration codeAttributeDeclaration =
+                new CodeAttributeDeclaration($"global::{generatedCodeAttribute.GetType().FullName}",
+                new CodeAttributeArgument(tool),
+                new CodeAttributeArgument(version));
+
+            return codeAttributeDeclaration;
+        }
+
+        private static string GetInformationalVersion()
+        {
+            System.Reflection.AssemblyInformationalVersionAttribute attribute =
+                (System.Reflection.AssemblyInformationalVersionAttribute)
+                System.Reflection.Assembly.GetExecutingAssembly()
+                .GetCustomAttribute(typeof(System.Reflection.AssemblyInformationalVersionAttribute));
+
+            return attribute.InformationalVersion;
+        }
     }
 }
diff --git a/lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs b/lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs
index 53d7d4e03..1d265af7c 100644
--- a/lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs
+++ b/lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs
@@ -93,6 +93,7 @@ namespace Avro.Test.AvroGen
                 {
                     typeof(object).Assembly.Location,
                     typeof(Schema).Assembly.Location,
+                    typeof(System.CodeDom.Compiler.GeneratedCodeAttribute).Assembly.Location,
                     Path.Combine(assemblyPath, "System.Runtime.dll"),
                     Path.Combine(assemblyPath, "netstandard.dll")
                 };
diff --git a/lang/csharp/src/apache/test/AvroGen/AvroGenTests.cs b/lang/csharp/src/apache/test/AvroGen/AvroGenTests.cs
index f4976d7b7..9b6a3a024 100644
--- a/lang/csharp/src/apache/test/AvroGen/AvroGenTests.cs
+++ b/lang/csharp/src/apache/test/AvroGen/AvroGenTests.cs
@@ -21,8 +21,6 @@ using System.Linq;
 using System.Reflection;
 using System.Collections.Generic;
 using Microsoft.CodeAnalysis;
-using Microsoft.CodeAnalysis.CSharp;
-using Microsoft.CodeAnalysis.Emit;
 using NUnit.Framework;
 using Avro.Specific;
 
@@ -314,6 +312,13 @@ namespace Avro.Test.AvroGen
                     }
                 }
 
+                // Verify GeneratedCodeAttribute
+                foreach(System.Reflection.TypeInfo definedType in assembly.DefinedTypes)
+                {
+                    var generatedAttributes = definedType.CustomAttributes.Where(x => x.AttributeType.FullName == "System.CodeDom.Compiler.GeneratedCodeAttribute");
+                    Assert.That(generatedAttributes, Is.Not.Null);
+                }
+
                 return assembly;
             }
             finally