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/01 11:31:08 UTC
[avro] branch master updated: AVRO-3334 Updated GetNullableType to simplify execution (#1484)
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 93db8e0 AVRO-3334 Updated GetNullableType to simplify execution (#1484)
93db8e0 is described below
commit 93db8e005eaaa07b5003dcbd03bee49dea3975b3
Author: Kyle Schoonover <ky...@minmaxcorp.com>
AuthorDate: Tue Feb 1 03:31:02 2022 -0800
AVRO-3334 Updated GetNullableType to simplify execution (#1484)
* AVRO-3334 Updated GetNullableType to simplify execution
* Updated documentation
* Updated to include obsolete method
Co-authored-by: Kyle T. Schoonover <Ky...@nordstrom.com>
Co-authored-by: Martin Grigorov <ma...@users.noreply.github.com>
---
lang/csharp/src/apache/main/CodeGen/CodeGen.cs | 44 ++++++++++++----------
.../apache/main/Reflect/ReflectDefaultReader.cs | 2 +-
lang/csharp/src/apache/test/CodGen/CodeGenTest.cs | 7 ++++
3 files changed, 32 insertions(+), 21 deletions(-)
diff --git a/lang/csharp/src/apache/main/CodeGen/CodeGen.cs b/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
index 88857e1..137695c 100644
--- a/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
+++ b/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
@@ -21,6 +21,7 @@ using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
+using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.CSharp;
@@ -981,7 +982,7 @@ namespace Avro
throw new CodeGenException("Unable to cast schema into a union schema");
}
- Schema nullibleType = getNullableType(unionSchema);
+ Schema nullibleType = GetNullableType(unionSchema);
if (nullibleType == null)
{
return CodeGenUtil.Object;
@@ -1019,31 +1020,34 @@ namespace Avro
/// <returns>
/// schema that is nullable.
/// </returns>
+ /// <exception cref="System.ArgumentNullException">schema - UnionSchema can not be null.</exception>
+ [Obsolete("Use GetNullableType. This method will be deprecated in a future release.")]
public static Schema getNullableType(UnionSchema schema)
{
- Schema ret = null;
- if (schema.Count == 2)
+ return GetNullableType(schema);
+ }
+
+ /// <summary>
+ /// Gets the schema of a union with null.
+ /// </summary>
+ /// <param name="schema">union schema.</param>
+ /// <returns>
+ /// schema that is nullable.
+ /// </returns>
+ /// <exception cref="System.ArgumentNullException">schema - UnionSchema can not be null.</exception>
+ public static Schema GetNullableType(UnionSchema schema)
+ {
+ if (schema == null)
{
- bool nullable = false;
- foreach (Schema childSchema in schema.Schemas)
- {
- if (childSchema.Tag == Schema.Type.Null)
- {
- nullable = true;
- }
- else
- {
- ret = childSchema;
- }
- }
+ throw new ArgumentNullException(nameof(schema), "UnionSchema can not be null");
+ }
- if (!nullable)
- {
- ret = null;
- }
+ if (schema.Count != 2 || schema.Schemas.All(x => x.Tag != Schema.Type.Null))
+ {
+ return null;
}
- return ret;
+ return schema.Schemas.FirstOrDefault(x => x.Tag != Schema.Type.Null);
}
/// <summary>
diff --git a/lang/csharp/src/apache/main/Reflect/ReflectDefaultReader.cs b/lang/csharp/src/apache/main/Reflect/ReflectDefaultReader.cs
index 676d9f3..ded0057 100644
--- a/lang/csharp/src/apache/main/Reflect/ReflectDefaultReader.cs
+++ b/lang/csharp/src/apache/main/Reflect/ReflectDefaultReader.cs
@@ -176,7 +176,7 @@ namespace Avro.Reflect
throw new Exception("Unable to cast schema into a union schema");
}
- Schema nullibleType = CodeGen.getNullableType(unionSchema);
+ Schema nullibleType = CodeGen.GetNullableType(unionSchema);
if (nullibleType == null)
{
return typeof(object);
diff --git a/lang/csharp/src/apache/test/CodGen/CodeGenTest.cs b/lang/csharp/src/apache/test/CodGen/CodeGenTest.cs
index ea46c6b..3d45485 100644
--- a/lang/csharp/src/apache/test/CodGen/CodeGenTest.cs
+++ b/lang/csharp/src/apache/test/CodGen/CodeGenTest.cs
@@ -29,6 +29,13 @@ namespace Avro.Test
class CodeGenTest
{
+
+ [Test]
+ public void TestGetNullableTypeException()
+ {
+ Assert.Throws<ArgumentNullException>(() => CodeGen.GetNullableType(null));
+ }
+
#if !NETCOREAPP // System.CodeDom compilation not supported in .NET Core: https://github.com/dotnet/corefx/issues/12180
[TestCase(@"{
""type"" : ""record"",