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/01/30 21:03:04 UTC
[avro] branch master updated: AVRO-3329 Added omitted braces to CodeGen class (#1480)
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 5eff9a7 AVRO-3329 Added omitted braces to CodeGen class (#1480)
5eff9a7 is described below
commit 5eff9a7f503ab0de550c99b6b46ac628ca9d817d
Author: Kyle Schoonover <ky...@minmaxcorp.com>
AuthorDate: Sun Jan 30 13:02:58 2022 -0800
AVRO-3329 Added omitted braces to CodeGen class (#1480)
* AVRO-3329 Added omitted braces to CodeGen class
* revert names?.Contains null check
Co-authored-by: Kyle T. Schoonover <Ky...@nordstrom.com>
---
lang/csharp/src/apache/main/CodeGen/CodeGen.cs | 170 +++++++++++++++++++++----
1 file changed, 145 insertions(+), 25 deletions(-)
diff --git a/lang/csharp/src/apache/main/CodeGen/CodeGen.cs b/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
index 6d4b16d..b282570 100644
--- a/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
+++ b/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
@@ -123,7 +123,9 @@ namespace Avro
protected virtual CodeNamespace addNamespace(string name)
{
if (string.IsNullOrEmpty(name))
+ {
throw new ArgumentNullException(nameof(name), "name cannot be null.");
+ }
CodeNamespace ns = null;
@@ -135,7 +137,9 @@ namespace Avro
: new CodeNamespace(CodeGenUtil.Instance.Mangle(name));
foreach (CodeNamespaceImport nci in CodeGenUtil.Instance.NamespaceImports)
+ {
ns.Imports.Add(nci);
+ }
CompileUnit.Namespaces.Add(ns);
NamespaceLookup.Add(name, ns);
@@ -219,7 +223,10 @@ namespace Avro
{
var names = new SchemaNames();
foreach (Schema schema in protocol.Types)
+ {
addName(schema, names);
+ }
+
return names;
}
@@ -246,7 +253,10 @@ namespace Avro
protected virtual void addName(Schema schema, SchemaNames names)
{
NamedSchema ns = schema as NamedSchema;
- if (null != ns) if (names.Contains(ns.SchemaName)) return;
+ if (ns != null && names.Contains(ns.SchemaName))
+ {
+ return;
+ }
switch (schema.Tag)
{
@@ -271,7 +281,10 @@ namespace Avro
var rs = schema as RecordSchema;
names.Add(rs);
foreach (Field field in rs.Fields)
+ {
addName(field.Schema, names);
+ }
+
break;
case Schema.Type.Array:
@@ -287,7 +300,10 @@ namespace Avro
case Schema.Type.Union:
var us = schema as UnionSchema;
foreach (Schema usc in us.Schemas)
+ {
addName(usc, names);
+ }
+
break;
default:
@@ -307,7 +323,10 @@ namespace Avro
protected virtual void processFixed(Schema schema)
{
FixedSchema fixedSchema = schema as FixedSchema;
- if (null == fixedSchema) throw new CodeGenException("Unable to cast schema into a fixed");
+ if (fixedSchema == null)
+ {
+ throw new CodeGenException("Unable to cast schema into a fixed");
+ }
CodeTypeDeclaration ctd = new CodeTypeDeclaration();
ctd.Name = CodeGenUtil.Instance.Mangle(fixedSchema.Name);
@@ -348,7 +367,10 @@ namespace Avro
string nspace = fixedSchema.Namespace;
if (string.IsNullOrEmpty(nspace))
+ {
throw new CodeGenException("Namespace required for enum schema " + fixedSchema.Name);
+ }
+
CodeNamespace codens = addNamespace(nspace);
codens.Types.Add(ctd);
}
@@ -367,7 +389,10 @@ namespace Avro
protected virtual void processEnum(Schema schema)
{
EnumSchema enumschema = schema as EnumSchema;
- if (null == enumschema) throw new CodeGenException("Unable to cast schema into an enum");
+ if (enumschema == null)
+ {
+ throw new CodeGenException("Unable to cast schema into an enum");
+ }
CodeTypeDeclaration ctd = new CodeTypeDeclaration(CodeGenUtil.Instance.Mangle(enumschema.Name));
ctd.IsEnum = true;
@@ -381,14 +406,20 @@ namespace Avro
foreach (string symbol in enumschema.Symbols)
{
if (CodeGenUtil.Instance.ReservedKeywords.Contains(symbol))
+ {
throw new CodeGenException("Enum symbol " + symbol + " is a C# reserved keyword");
+ }
+
CodeMemberField field = new CodeMemberField(typeof(int), symbol);
ctd.Members.Add(field);
}
string nspace = enumschema.Namespace;
if (string.IsNullOrEmpty(nspace))
+ {
throw new CodeGenException("Namespace required for enum schema " + enumschema.Name);
+ }
+
CodeNamespace codens = addNamespace(nspace);
codens.Types.Add(ctd);
@@ -473,7 +504,10 @@ namespace Avro
string nspace = protocol.Namespace;
if (string.IsNullOrEmpty(nspace))
+ {
throw new CodeGenException("Namespace required for enum schema " + nspace);
+ }
+
CodeNamespace codens = addNamespace(nspace);
codens.Types.Add(ctd);
@@ -537,14 +571,18 @@ namespace Avro
var response = message.Response;
if (generateCallback && message.Oneway.GetValueOrDefault())
+ {
continue;
+ }
var messageMember = new CodeMemberMethod();
messageMember.Name = CodeGenUtil.Instance.Mangle(name);
messageMember.Attributes = MemberAttributes.Public | MemberAttributes.Abstract;
if (message.Doc != null && message.Doc.Trim() != string.Empty)
+ {
messageMember.Comments.Add(new CodeCommentStatement(message.Doc));
+ }
if (message.Oneway.GetValueOrDefault() || generateCallback)
{
@@ -594,7 +632,9 @@ namespace Avro
{
var interfaceDoc = createDocComment(protocol.Doc);
if (interfaceDoc != null)
+ {
ctd.Comments.Add(interfaceDoc);
+ }
}
}
@@ -613,7 +653,10 @@ namespace Avro
protected virtual CodeTypeDeclaration processRecord(Schema schema)
{
RecordSchema recordSchema = schema as RecordSchema;
- if (null == recordSchema) throw new CodeGenException("Unable to cast schema into a record");
+ if (recordSchema == null)
+ {
+ throw new CodeGenException("Unable to cast schema into a record");
+ }
bool isError = recordSchema.Tag == Schema.Type.Error;
@@ -681,8 +724,10 @@ namespace Avro
if (!string.IsNullOrEmpty(field.Documentation))
{
propertyComment = createDocComment(field.Documentation);
- if (null != propertyComment)
+ if (propertyComment != null)
+ {
codeField.Comments.Add(propertyComment);
+ }
}
// Add field to class
@@ -699,8 +744,10 @@ namespace Avro
property.Type = ctrfield;
property.GetStatements.Add(new CodeMethodReturnStatement(fieldRef));
property.SetStatements.Add(new CodeAssignStatement(fieldRef, new CodePropertySetValueReferenceExpression()));
- if (null != propertyComment)
+ if (propertyComment != null)
+ {
property.Comments.Add(propertyComment);
+ }
// Add field property to class
ctd.Members.Add(property);
@@ -754,7 +801,10 @@ namespace Avro
string nspace = recordSchema.Namespace;
if (string.IsNullOrEmpty(nspace))
+ {
throw new CodeGenException("Namespace required for record schema " + recordSchema.Name);
+ }
+
CodeNamespace codens = addNamespace(nspace);
codens.Types.Add(ctd);
@@ -794,20 +844,54 @@ namespace Avro
case Schema.Type.Null:
return typeof(object).ToString();
case Schema.Type.Boolean:
- if (nullible) return $"System.Nullable<{typeof(bool)}>";
- else return typeof(bool).ToString();
+ if (nullible)
+ {
+ return $"System.Nullable<{typeof(bool)}>";
+ }
+ else
+ {
+ return typeof(bool).ToString();
+ }
+
case Schema.Type.Int:
- if (nullible) return $"System.Nullable<{typeof(int)}>";
- else return typeof(int).ToString();
+ if (nullible)
+ {
+ return $"System.Nullable<{typeof(int)}>";
+ }
+ else
+ {
+ return typeof(int).ToString();
+ }
+
case Schema.Type.Long:
- if (nullible) return $"System.Nullable<{typeof(long)}>";
- else return typeof(long).ToString();
+ if (nullible)
+ {
+ return $"System.Nullable<{typeof(long)}>";
+ }
+ else
+ {
+ return typeof(long).ToString();
+ }
+
case Schema.Type.Float:
- if (nullible) return $"System.Nullable<{typeof(float)}>";
- else return typeof(float).ToString();
+ if (nullible)
+ {
+ return $"System.Nullable<{typeof(float)}>";
+ }
+ else
+ {
+ return typeof(float).ToString();
+ }
+
case Schema.Type.Double:
- if (nullible) return $"System.Nullable<{typeof(double)}>";
- else return typeof(double).ToString();
+ if (nullible)
+ {
+ return $"System.Nullable<{typeof(double)}>";
+ }
+ else
+ {
+ return typeof(double).ToString();
+ }
case Schema.Type.Bytes:
return typeof(byte[]).ToString();
@@ -816,50 +900,74 @@ namespace Avro
case Schema.Type.Enumeration:
var namedSchema = schema as NamedSchema;
- if (null == namedSchema)
+ if (namedSchema == null)
+ {
throw new CodeGenException("Unable to cast schema into a named schema");
+ }
+
if (nullible)
{
nullibleEnum = true;
return "System.Nullable<" + CodeGenUtil.Instance.Mangle(namedSchema.Fullname) + ">";
}
- else return CodeGenUtil.Instance.Mangle(namedSchema.Fullname);
+ else
+ {
+ return CodeGenUtil.Instance.Mangle(namedSchema.Fullname);
+ }
case Schema.Type.Fixed:
case Schema.Type.Record:
case Schema.Type.Error:
namedSchema = schema as NamedSchema;
- if (null == namedSchema)
+ if (namedSchema == null)
+ {
throw new CodeGenException("Unable to cast schema into a named schema");
+ }
+
return CodeGenUtil.Instance.Mangle(namedSchema.Fullname);
case Schema.Type.Array:
var arraySchema = schema as ArraySchema;
- if (null == arraySchema)
+ if (arraySchema == null)
+ {
throw new CodeGenException("Unable to cast schema into an array schema");
+ }
return "IList<" + getType(arraySchema.ItemSchema, false, ref nullibleEnum) + ">";
case Schema.Type.Map:
var mapSchema = schema as MapSchema;
- if (null == mapSchema)
+ if (mapSchema == null)
+ {
throw new CodeGenException("Unable to cast schema into a map schema");
+ }
+
return "IDictionary<string," + getType(mapSchema.ValueSchema, false, ref nullibleEnum) + ">";
case Schema.Type.Union:
var unionSchema = schema as UnionSchema;
- if (null == unionSchema)
+ if (unionSchema == null)
+ {
throw new CodeGenException("Unable to cast schema into a union schema");
+ }
+
Schema nullibleType = getNullableType(unionSchema);
- if (null == nullibleType)
+ if (nullibleType == null)
+ {
return CodeGenUtil.Object;
+ }
else
+ {
return getType(nullibleType, true, ref nullibleEnum);
+ }
case Schema.Type.Logical:
var logicalSchema = schema as LogicalSchema;
- if (null == logicalSchema)
+ if (logicalSchema == null)
+ {
throw new CodeGenException("Unable to cast schema into a logical schema");
+ }
+
var csharpType = logicalSchema.LogicalType.GetCSharpType(nullible);
if (csharpType.IsGenericType && csharpType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
@@ -890,12 +998,18 @@ namespace Avro
foreach (Schema childSchema in schema.Schemas)
{
if (childSchema.Tag == Schema.Type.Null)
+ {
nullable = true;
+ }
else
+ {
ret = childSchema;
+ }
}
if (!nullable)
+ {
ret = null;
+ }
}
return ret;
}
@@ -925,7 +1039,11 @@ namespace Avro
// create property to get static schema field
var property = new CodeMemberProperty();
property.Attributes = MemberAttributes.Public;
- if (overrideFlag) property.Attributes |= MemberAttributes.Override;
+ if (overrideFlag)
+ {
+ property.Attributes |= MemberAttributes.Override;
+ }
+
property.Name = "Schema";
property.Type = ctrfield;
@@ -994,7 +1112,9 @@ namespace Avro
var new_ns = new CodeNamespace(ns.Name);
new_ns.Comments.Add(CodeGenUtil.Instance.FileComment);
foreach (CodeNamespaceImport nci in CodeGenUtil.Instance.NamespaceImports)
+ {
new_ns.Imports.Add(nci);
+ }
var types = ns.Types;
for (int j = 0; j < types.Count; j++)