KalleOlaviNiemitalo commented on code in PR #2751:
URL: https://github.com/apache/avro/pull/2751#discussion_r2005719770


##########
lang/csharp/src/apache/main/Util/UnknownLogicalType.cs:
##########
@@ -0,0 +1,163 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Avro.Util
+{
+    /// <summary>
+    /// Class UnknownLogicalType.
+    /// Implements the <see cref="Avro.Util.LogicalType" />
+    /// </summary>
+    /// <seealso cref="Avro.Util.LogicalType" />
+    public class UnknownLogicalType : LogicalType
+    {
+        /// <summary>
+        /// Gets the schema.
+        /// </summary>
+        /// <value>The schema.</value>
+        public LogicalSchema Schema { get; }
+
+        /// <summary>
+        /// Initializes a new instance of the <see cref="UnknownLogicalType"/> 
class.
+        /// </summary>
+        /// <param name="schema">The schema.</param>
+        public UnknownLogicalType(LogicalSchema schema) : 
base(schema.LogicalTypeName)
+        {
+            this.Schema = schema;
+        }
+
+        /// <summary>
+        /// Converts a logical value to an instance of its base type.
+        /// </summary>
+        /// <param name="logicalValue">The logical value to convert.</param>
+        /// <param name="schema">The schema that represents the target of the 
conversion.</param>
+        /// <returns>An object representing the encoded value of the base 
type.</returns>
+        public override object ConvertToBaseValue(object logicalValue, 
LogicalSchema schema)
+        {
+            switch (schema.Name)
+            {
+                case @"string":
+                    return (System.String)logicalValue;
+                case @"boolean":
+                    return (System.Boolean)logicalValue;
+                case @"int":
+                    return (System.Int32)logicalValue;
+                case @"long":
+                    return (System.Int64)logicalValue;
+                case @"float":
+                    return (System.Single)logicalValue;
+                case @"double":
+                    return (System.Double)logicalValue;
+                case @"bytes":
+                    return (System.Byte[])logicalValue;
+                default:
+                    return logicalValue;
+            }
+        }
+
+        /// <summary>
+        /// Converts a base value to an instance of the logical type.
+        /// </summary>
+        /// <param name="baseValue">The base value to convert.</param>
+        /// <param name="schema">The schema that represents the target of the 
conversion.</param>
+        /// <returns>An object representing the encoded value of the logical 
type.</returns>
+        public override object ConvertToLogicalValue(object baseValue, 
LogicalSchema schema)
+        {
+            switch (schema.Name)
+            {
+                case @"string":
+                    return (System.String)baseValue;
+                case @"boolean":
+                    return (System.Boolean)baseValue;
+                case @"int":
+                    return (System.Int32)baseValue;
+                case @"long":
+                    return (System.Int64)baseValue;
+                case @"float":
+                    return (System.Single)baseValue;
+                case @"double":
+                    return (System.Double)baseValue;
+                case @"bytes":
+                    return (System.Byte[])baseValue;
+                default:
+                    return baseValue;
+            }
+        }
+
+        /// <summary>
+        /// Retrieve the .NET type that is represented by the logical type 
implementation.
+        /// </summary>
+        /// <param name="nullible">A flag indicating whether it should be 
nullible.</param>
+        /// <returns>Type.</returns>
+        public override Type GetCSharpType(bool nullible)
+        {
+            // handle all Primitive Types
+            switch (this.Schema.BaseSchema.Name)
+            {
+                case @"string":
+                    return typeof(System.String);
+                case @"boolean":
+                    return nullible ? typeof(System.Boolean?) : 
typeof(System.Boolean);
+                case @"int":
+                    return nullible ? typeof(System.Int32?) : 
typeof(System.Int32);
+                case @"long":
+                    return nullible ? typeof(System.Int64?) : 
typeof(System.Int64);
+                case @"float":
+                    return nullible ? typeof(System.Single?) : 
typeof(System.Single);
+                case @"double":
+                    return nullible ? typeof(System.Double?) : 
typeof(System.Double);
+                case @"bytes":
+                    return nullible ? typeof(System.Byte?[]) : 
typeof(System.Byte[]);

Review Comment:
   `System.Byte?[]` is an array type in which each element is a nullable byte.  
It is not the the correct type for Avro `{"type": [ "null", "bytes" ]}`, which 
allows either a null value or an array of non-null bytes. So this should return 
`typeof(System.Byte[])` regardless of `nullible`, similarly to the `case 
"string"` above.



##########
lang/csharp/src/apache/main/Util/UnknownLogicalType.cs:
##########
@@ -0,0 +1,163 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Avro.Util
+{
+    /// <summary>
+    /// Class UnknownLogicalType.
+    /// Implements the <see cref="Avro.Util.LogicalType" />
+    /// </summary>
+    /// <seealso cref="Avro.Util.LogicalType" />
+    public class UnknownLogicalType : LogicalType
+    {
+        /// <summary>
+        /// Gets the schema.
+        /// </summary>
+        /// <value>The schema.</value>
+        public LogicalSchema Schema { get; }
+
+        /// <summary>
+        /// Initializes a new instance of the <see cref="UnknownLogicalType"/> 
class.
+        /// </summary>
+        /// <param name="schema">The schema.</param>
+        public UnknownLogicalType(LogicalSchema schema) : 
base(schema.LogicalTypeName)
+        {
+            this.Schema = schema;
+        }
+
+        /// <summary>
+        /// Converts a logical value to an instance of its base type.
+        /// </summary>
+        /// <param name="logicalValue">The logical value to convert.</param>
+        /// <param name="schema">The schema that represents the target of the 
conversion.</param>
+        /// <returns>An object representing the encoded value of the base 
type.</returns>
+        public override object ConvertToBaseValue(object logicalValue, 
LogicalSchema schema)
+        {
+            switch (schema.Name)
+            {
+                case @"string":
+                    return (System.String)logicalValue;
+                case @"boolean":
+                    return (System.Boolean)logicalValue;
+                case @"int":
+                    return (System.Int32)logicalValue;
+                case @"long":
+                    return (System.Int64)logicalValue;
+                case @"float":
+                    return (System.Single)logicalValue;
+                case @"double":
+                    return (System.Double)logicalValue;
+                case @"bytes":
+                    return (System.Byte[])logicalValue;
+                default:
+                    return logicalValue;
+            }
+        }

Review Comment:
   These casts are almost no-ops, because the result of each cast is 
immediately converted back to `object`.  I think this method could just `return 
logicalValue;` always regardless of `schema.Name`.
   
   The casts have two effects, but I don't think those effects matter here, 
because the logical value should be the same as the base value, which should be 
an instance of one of the Avro standard types:
   
   * If the `logicalValue` is not compatible with the expected type, then it 
throws InvalidCastException.
   * In some cases, the .NET runtime allows unboxing from a type that does not 
match exactly.  For example, casting from Object to Int32 works even if the 
Object is actually a boxed UInt32 or a boxed enum whose underlying type is 
Int32 or UInt32.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to