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/02/04 09:08:49 UTC

[avro] branch master updated: AVRO-3354 Simplified if statements (#1505)

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 9ca68cb  AVRO-3354 Simplified if statements (#1505)
9ca68cb is described below

commit 9ca68cb782a98badcbd24d0503777d6e45b0ab2f
Author: Kyle Schoonover <ky...@minmaxcorp.com>
AuthorDate: Fri Feb 4 01:08:37 2022 -0800

    AVRO-3354 Simplified if statements (#1505)
    
    Co-authored-by: Kyle T. Schoonover <Ky...@nordstrom.com>
---
 lang/csharp/src/apache/main/CodeGen/CodeGen.cs | 71 +++++---------------------
 1 file changed, 12 insertions(+), 59 deletions(-)

diff --git a/lang/csharp/src/apache/main/CodeGen/CodeGen.cs b/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
index 136a9a7..922bfe0 100644
--- a/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
+++ b/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
@@ -907,54 +907,19 @@ 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();
-                    }
+                    return nullible ? $"System.Nullable<{typeof(bool)}>" : typeof(bool).ToString();
 
                 case Schema.Type.Int:
-                    if (nullible)
-                    {
-                        return $"System.Nullable<{typeof(int)}>";
-                    }
-                    else
-                    {
-                        return typeof(int).ToString();
-                    }
+                    return nullible ? $"System.Nullable<{typeof(int)}>" : typeof(int).ToString();
 
                 case Schema.Type.Long:
-                    if (nullible)
-                    {
-                        return $"System.Nullable<{typeof(long)}>";
-                    }
-                    else
-                    {
-                        return typeof(long).ToString();
-                    }
+                    return nullible ? $"System.Nullable<{typeof(long)}>" : typeof(long).ToString();
 
                 case Schema.Type.Float:
-                    if (nullible)
-                    {
-                        return $"System.Nullable<{typeof(float)}>";
-                    }
-                    else
-                    {
-                        return typeof(float).ToString();
-                    }
+                    return nullible ? $"System.Nullable<{typeof(float)}>" : typeof(float).ToString();
 
                 case Schema.Type.Double:
-                    if (nullible)
-                    {
-                        return $"System.Nullable<{typeof(double)}>";
-                    }
-                    else
-                    {
-                        return typeof(double).ToString();
-                    }
+                    return nullible ? $"System.Nullable<{typeof(double)}>" : typeof(double).ToString();
 
                 case Schema.Type.Bytes:
                     return typeof(byte[]).ToString();
@@ -1015,14 +980,8 @@ namespace Avro
                     }
 
                     Schema nullibleType = GetNullableType(unionSchema);
-                    if (nullibleType == null)
-                    {
-                        return CodeGenUtil.Object;
-                    }
-                    else
-                    {
-                        return getType(nullibleType, true, ref nullibleEnum);
-                    }
+
+                    return nullibleType == null ? CodeGenUtil.Object : getType(nullibleType, true, ref nullibleEnum);
 
                 case Schema.Type.Logical:
                     var logicalSchema = schema as LogicalSchema;
@@ -1032,14 +991,8 @@ namespace Avro
                     }
 
                     var csharpType = logicalSchema.LogicalType.GetCSharpType(nullible);
-                    if (csharpType.IsGenericType && csharpType.GetGenericTypeDefinition() == typeof(Nullable<>))
-                    {
-                        return $"System.Nullable<{csharpType.GetGenericArguments()[0]}>";
-                    }
-                    else
-                    {
-                        return csharpType.ToString();
-                    }
+                    return csharpType.IsGenericType && csharpType.GetGenericTypeDefinition() == typeof(Nullable<>)
+                        ? $"System.Nullable<{csharpType.GetGenericArguments()[0]}>" : csharpType.ToString();
             }
 
             throw new CodeGenException("Unable to generate CodeTypeReference for " + schema.Name + " type " + schema.Tag);
@@ -1074,12 +1027,12 @@ namespace Avro
                 throw new ArgumentNullException(nameof(schema), "UnionSchema can not be null");
             }
 
-            if (schema.Count != 2 || schema.Schemas.All(x => x.Tag != Schema.Type.Null))
+            if (schema.Count == 2 && !schema.Schemas.All(x => x.Tag != Schema.Type.Null))
             {
-                return null;
+                return schema.Schemas.FirstOrDefault(x => x.Tag != Schema.Type.Null);
             }
 
-            return schema.Schemas.FirstOrDefault(x => x.Tag != Schema.Type.Null);
+            return null;
         }
 
         /// <summary>