You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by rs...@apache.org on 2021/12/22 17:39:09 UTC
[avro] branch master updated: AVRO-3265: Fix avrogen code generation for Avro namespaces (#1423)
This is an automated email from the ASF dual-hosted git repository.
rskraba 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 bbe3db4 AVRO-3265: Fix avrogen code generation for Avro namespaces (#1423)
bbe3db4 is described below
commit bbe3db4cc1ef68eaa129586beb5978e6315d598f
Author: Serhii Almazov <al...@gmail.com>
AuthorDate: Wed Dec 22 19:37:14 2021 +0200
AVRO-3265: Fix avrogen code generation for Avro namespaces (#1423)
Fixes an issue when the generated code is uncompilable if the namespace to be generated ends with ".Avro"
---
lang/csharp/src/apache/main/CodeGen/CodeGen.cs | 13 ++++++++-----
lang/csharp/src/apache/main/CodeGen/CodeGenUtil.cs | 4 ++--
lang/csharp/src/apache/test/CodGen/CodeGenTest.cs | 10 ++++++++++
3 files changed, 20 insertions(+), 7 deletions(-)
diff --git a/lang/csharp/src/apache/main/CodeGen/CodeGen.cs b/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
index 70ab5bd..93e02bf 100644
--- a/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
+++ b/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
@@ -558,7 +558,10 @@ namespace Avro
// declare the class
var ctd = new CodeTypeDeclaration(CodeGenUtil.Instance.Mangle(recordSchema.Name));
- ctd.BaseTypes.Add(isError ? "SpecificException" : "ISpecificRecord");
+ var baseTypeReference = new CodeTypeReference(
+ isError ? typeof(Specific.SpecificException) : typeof(Specific.ISpecificRecord),
+ CodeTypeReferenceOptions.GlobalReference);
+ ctd.BaseTypes.Add(baseTypeReference);
ctd.Attributes = MemberAttributes.Public;
ctd.IsClass = true;
@@ -675,14 +678,14 @@ namespace Avro
}
// end switch block for Get()
- getFieldStmt.AppendLine("\t\t\tdefault: throw new AvroRuntimeException(\"Bad index \" + fieldPos + \" in Get()\");")
+ getFieldStmt.AppendLine("\t\t\tdefault: throw new global::Avro.AvroRuntimeException(\"Bad index \" + fieldPos + \" in Get()\");")
.Append("\t\t\t}");
var cseGet = new CodeSnippetExpression(getFieldStmt.ToString());
cmmGet.Statements.Add(cseGet);
ctd.Members.Add(cmmGet);
// end switch block for Put()
- putFieldStmt.AppendLine("\t\t\tdefault: throw new AvroRuntimeException(\"Bad index \" + fieldPos + \" in Put()\");")
+ putFieldStmt.AppendLine("\t\t\tdefault: throw new global::Avro.AvroRuntimeException(\"Bad index \" + fieldPos + \" in Put()\");")
.Append("\t\t\t}");
var csePut = new CodeSnippetExpression(putFieldStmt.ToString());
cmmPut.Statements.Add(csePut);
@@ -831,14 +834,14 @@ namespace Avro
protected virtual void createSchemaField(Schema schema, CodeTypeDeclaration ctd, bool overrideFlag)
{
// create schema field
- var ctrfield = new CodeTypeReference("Schema");
+ var ctrfield = new CodeTypeReference(typeof(Schema), CodeTypeReferenceOptions.GlobalReference);
string schemaFname = "_SCHEMA";
var codeField = new CodeMemberField(ctrfield, schemaFname);
codeField.Attributes = MemberAttributes.Public | MemberAttributes.Static;
// create function call Schema.Parse(json)
var cpe = new CodePrimitiveExpression(schema.ToString());
var cmie = new CodeMethodInvokeExpression(
- new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(Schema)), "Parse"),
+ new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(ctrfield), "Parse"),
new CodeExpression[] { cpe });
codeField.InitExpression = cmie;
ctd.Members.Add(codeField);
diff --git a/lang/csharp/src/apache/main/CodeGen/CodeGenUtil.cs b/lang/csharp/src/apache/main/CodeGen/CodeGenUtil.cs
index 54de067..0b3c177 100644
--- a/lang/csharp/src/apache/main/CodeGen/CodeGenUtil.cs
+++ b/lang/csharp/src/apache/main/CodeGen/CodeGenUtil.cs
@@ -60,8 +60,8 @@ namespace Avro
new CodeNamespaceImport("System"),
new CodeNamespaceImport("System.Collections.Generic"),
new CodeNamespaceImport("System.Text"),
- new CodeNamespaceImport("Avro"),
- new CodeNamespaceImport("Avro.Specific") };
+ new CodeNamespaceImport("global::Avro"),
+ new CodeNamespaceImport("global::Avro.Specific") };
FileComment = new CodeCommentStatement(
@"------------------------------------------------------------------------------
diff --git a/lang/csharp/src/apache/test/CodGen/CodeGenTest.cs b/lang/csharp/src/apache/test/CodGen/CodeGenTest.cs
index c288989..86ca317 100644
--- a/lang/csharp/src/apache/test/CodGen/CodeGenTest.cs
+++ b/lang/csharp/src/apache/test/CodGen/CodeGenTest.cs
@@ -51,6 +51,16 @@ namespace Avro.Test
", new object[] {"com.base.ClassKeywords", typeof(int), typeof(long), typeof(bool), typeof(double), typeof(float), typeof(byte[]), typeof(string),typeof(object),"com.base.class", "com.base.static"}, TestName = "TestCodeGen0")]
[TestCase(@"{
""type"" : ""record"",
+""name"" : ""AvroNamespaceType"",
+""namespace"" : ""My.Avro"",
+""fields"" :
+ [
+ { ""name"" : ""justenum"", ""type"" : { ""type"" : ""enum"", ""name"" : ""justenumEnum"", ""symbols"" : [ ""One"", ""Two"" ] } },
+ ]
+}
+", new object[] {"My.Avro.AvroNamespaceType", "My.Avro.justenumEnum"}, TestName = "TestCodeGen3 - Avro namespace conflict")]
+ [TestCase(@"{
+""type"" : ""record"",
""name"" : ""SchemaObject"",
""namespace"" : ""schematest"",
""fields"" :