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/19 18:19:21 UTC
[avro] branch branch-1.11 updated: AVRO-3424: Added support to parse string into Schema.Type (#1571)
This is an automated email from the ASF dual-hosted git repository.
mgrigorov pushed a commit to branch branch-1.11
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/branch-1.11 by this push:
new 51906dcbb AVRO-3424: Added support to parse string into Schema.Type (#1571)
51906dcbb is described below
commit 51906dcbb62baf486210a20529ce7258e2b31f8d
Author: Kyle Schoonover <ky...@minmaxcorp.com>
AuthorDate: Tue Apr 19 11:19:01 2022 -0700
AVRO-3424: Added support to parse string into Schema.Type (#1571)
* AVRO-3360 Updated XML documentation
* Revert "AVRO-3360 Updated XML documentation"
This reverts commit b8601c072a5083380d30b580804dd0908b8cf4cc.
* AVRO-3424 Created extension method for converting string into a Schema.Type enumeration
* Updated functionality
* Removed breaking code
* Updated remove quotes
* Removed if from tests
Co-authored-by: Kyle T. Schoonover <Ky...@nordstrom.com>
(cherry picked from commit 3079cb8e88d185a3d3713163bed59d6b1c56ba5d)
---
lang/csharp/src/apache/main/Schema/Schema.cs | 89 ++++++++++++++++++++++-
lang/csharp/src/apache/test/Schema/SchemaTests.cs | 43 ++++++++++-
2 files changed, 127 insertions(+), 5 deletions(-)
diff --git a/lang/csharp/src/apache/main/Schema/Schema.cs b/lang/csharp/src/apache/main/Schema/Schema.cs
index 69de388c3..3e54653f0 100644
--- a/lang/csharp/src/apache/main/Schema/Schema.cs
+++ b/lang/csharp/src/apache/main/Schema/Schema.cs
@@ -196,7 +196,8 @@ namespace Avro
return LogicalSchema.NewInstance(jtok, props, names, encspace);
Schema schema = PrimitiveSchema.NewInstance((string)type, props);
- if (null != schema) return schema;
+ if (null != schema)
+ return schema;
return NamedSchema.NewInstance(jo, props, names, encspace);
}
@@ -380,5 +381,91 @@ namespace Avro
{
return obj == null ? 0 : obj.GetHashCode();
}
+
+ /// <summary>
+ /// Parses the Schema.Type from a string.
+ /// </summary>
+ /// <param name="type">The type to convert.</param>
+ /// <param name="removeQuotes">if set to <c>true</c> [remove quotes].</param>
+ /// <returns>A Schema.Type unless it could not parse then null</returns>
+ /// <remarks>
+ /// usage ParseType("string") returns Schema.Type.String
+ /// </remarks>
+ public static Schema.Type? ParseType(string type, bool removeQuotes = false)
+ {
+ string newValue = removeQuotes ? RemoveQuotes(type) : type;
+
+ switch (newValue)
+ {
+ case "null":
+ return Schema.Type.Null;
+
+ case "boolean":
+ return Schema.Type.Boolean;
+
+ case "int":
+ return Schema.Type.Int;
+
+ case "long":
+ return Schema.Type.Long;
+
+ case "float":
+ return Schema.Type.Float;
+
+ case "double":
+ return Schema.Type.Double;
+
+ case "bytes":
+ return Schema.Type.Bytes;
+
+ case "string":
+ return Schema.Type.String;
+
+ case "record":
+ return Schema.Type.Record;
+
+ case "enumeration":
+ return Schema.Type.Enumeration;
+
+ case "array":
+ return Schema.Type.Array;
+
+ case "map":
+ return Schema.Type.Map;
+
+ case "union":
+ return Schema.Type.Union;
+
+ case "fixed":
+ return Schema.Type.Fixed;
+
+ case "error":
+ return Schema.Type.Error;
+
+ case "logical":
+ return Schema.Type.Logical;
+
+ default:
+ return null;
+ }
+ }
+
+ /// <summary>
+ /// Removes the quotes from the first position and last position of the string.
+ /// </summary>
+ /// <param name="value">The value.</param>
+ /// <returns>
+ /// If string has a quote at the beginning and the end it removes them,
+ /// otherwise it returns the original string
+ /// </returns>
+ private static string RemoveQuotes(string value)
+ {
+ if(value.StartsWith("\"") && value.EndsWith("\""))
+ {
+ return value.Substring(1, value.Length - 2);
+ }
+
+ return value;
+ }
}
}
diff --git a/lang/csharp/src/apache/test/Schema/SchemaTests.cs b/lang/csharp/src/apache/test/Schema/SchemaTests.cs
index 59b4a90f3..a061d5373 100644
--- a/lang/csharp/src/apache/test/Schema/SchemaTests.cs
+++ b/lang/csharp/src/apache/test/Schema/SchemaTests.cs
@@ -16,10 +16,7 @@
* limitations under the License.
*/
using System;
-using System.Collections.Generic;
-using System.Text;
using NUnit.Framework;
-using Avro;
namespace Avro.Test
{
@@ -226,7 +223,7 @@ namespace Avro.Test
Assert.True(recordSchema["f1"].Schema.ToString().Contains("metafield"));
Assert.True(definedSchema.Equals(recordSchema["f1"].Schema));
- Assert.AreEqual(definedSchema.GetHashCode(),recordSchema["f1"].Schema.GetHashCode());
+ Assert.AreEqual(definedSchema.GetHashCode(), recordSchema["f1"].Schema.GetHashCode());
}
[TestCase("{\"type\":\"record\",\"name\":\"LongList\"," +
@@ -440,5 +437,43 @@ namespace Avro.Test
var schema = Schema.Parse(schemaJson);
Assert.AreEqual(schema.ToString(), expectedSchemaJson);
}
+
+ [TestFixture]
+ public class SchemaTypeExtensionsTests
+ {
+ [TestCase("null", Schema.Type.Null)]
+ [TestCase("boolean", Schema.Type.Boolean)]
+ [TestCase("int", Schema.Type.Int)]
+ [TestCase("long", Schema.Type.Long)]
+ [TestCase("float", Schema.Type.Float)]
+ [TestCase("double", Schema.Type.Double)]
+ [TestCase("bytes", Schema.Type.Bytes)]
+ [TestCase("string", Schema.Type.String)]
+ [TestCase("record", Schema.Type.Record)]
+ [TestCase("enumeration", Schema.Type.Enumeration)]
+ [TestCase("array", Schema.Type.Array)]
+ [TestCase("map", Schema.Type.Map)]
+ [TestCase("union", Schema.Type.Union)]
+ [TestCase("fixed", Schema.Type.Fixed)]
+ [TestCase("error", Schema.Type.Error)]
+ [TestCase("logical", Schema.Type.Logical)]
+ [TestCase("Logical", null)]
+ [TestCase("InvalidValue", null)]
+ [TestCase("\"null\"", null)]
+ [TestCase("", null)]
+ [TestCase(null, null)]
+ public void ParseTypeTest(string value, object expectedResult)
+ {
+ Assert.AreEqual(Schema.ParseType(value), expectedResult);
+ }
+
+ [TestCase("\"null\"", Schema.Type.Null)]
+ [TestCase("\"nu\"ll\"", null)]
+ [TestCase("\"\"", null)]
+ public void ParseTypeRemoveQuotesTest(string value, object expectedResult)
+ {
+ Assert.AreEqual(Schema.ParseType(value, true), expectedResult);
+ }
+ }
}
}