[ 
https://issues.apache.org/jira/browse/AVRO-3540?focusedWorklogId=783690&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-783690
 ]

ASF GitHub Bot logged work on AVRO-3540:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 22/Jun/22 04:14
            Start Date: 22/Jun/22 04:14
    Worklog Time Spent: 10m 
      Work Description: czf commented on code in PR #1728:
URL: https://github.com/apache/avro/pull/1728#discussion_r903264664


##########
lang/csharp/src/apache/main/Reflect/DotnetProperty.cs:
##########
@@ -19,6 +19,7 @@
 using System;
 using System.Reflection;
 using System.Collections;
+using System.Collections.Generic;

Review Comment:
   removed



##########
lang/csharp/src/apache/test/Reflect/TestReflect.cs:
##########
@@ -21,12 +21,75 @@
 using NUnit.Framework;
 using Avro.IO;
 using Avro.Reflect;
+using System.Collections.Generic;
+using System;
+using Avro.Specific;
+using System.Linq;
+
+
 
 namespace Avro.Test
 {
     [TestFixture]
     class TestReflect
     {
+        public class DictionaryTestClass
+        {
+            public Dictionary<int, int> p { get; set; }
+            public NestedTestClass ntc { get; set; }
+
+
+            public class NestedTestClass
+            {
+                public int NestedTestClassInt { get; set; }
+            }
+        }
+
+        public class DictionaryTestClass2
+        {
+            public Dictionary<int, string> p { get; set; }
+
+        }
+        class TestMapConverter : IAvroFieldConverter
+        {
+            public object FromAvroType(object o, Schema s) =>
+                ((Dictionary<string, int>)o).ToDictionary(x => 
int.Parse(x.Key), y=>y.Value);
+                
+            
+            public Type GetAvroType() => typeof(IDictionary<string, int>);
+            public Type GetPropertyType() => typeof(Dictionary<int, int>);
+            public object ToAvroType(object o, Schema s) =>
+                ((Dictionary<int, int>)o).ToDictionary(x => x.Key.ToString(), 
y => y.Value);
+        }
+
+        [TestCase]
+        public void TestMapWithConverterSucceeds()
+        {
+            ClassCache.AddDefaultConverter(new TestMapConverter());
+            
+            var schemaJson = 
"{\"fields\":[{\"name\":\"ntc\",\"type\":{\"type\":\"record\",\"name\":\"NestedTestClass\",\"fields\":[{\"name\":\"NestedTestClassInt\",\"type\":\"int\"}]}},{\"type\":{\"values\":\"int\",\"type\":\"map\"},\"name\":\"p\"}],\"type\":\"record\",\"name\":\"DictionaryTestClass\",\"namespace\":\"Avro.Test.TestReflect\\u002B\"}";
+            var schema = Schema.Parse(schemaJson);
+            DictionaryTestClass expected = new DictionaryTestClass() { p = new 
Dictionary<int, int>() { { 1, 1 }, { 2, 4 }, { 3, 5 } } , ntc = new 
DictionaryTestClass.NestedTestClass() { NestedTestClassInt = 1 } };
+
+            using Stream stream =  serialize(schema, expected);
+            var avroReader = new ReflectReader<DictionaryTestClass>(schema, 
schema);    
+            stream.Position = 0;
+            var actual = avroReader.Read(null, new BinaryDecoder(stream));
+           
+            CollectionAssert.AreEquivalent(expected.p, actual.p);
+        }
+
+        [TestCase]
+        public void TestMapWithoutConverterFails()
+        {
+            var schemaJson = 
"{\"fields\":[{\"type\":{\"values\":\"string\",\"type\":\"map\"},\"name\":\"p\"}],\"type\":\"record\",\"name\":\"DictionaryTestClass\",\"namespace\":\"Avro.Test.TestReflect\\u002B\"}";
+            var schema = Schema.Parse(schemaJson);
+            DictionaryTestClass2 expected = new DictionaryTestClass2() { p = 
new Dictionary<int, string>() { { 1, "1" }, { 2, "4" }, { 3, "5" } } };

Review Comment:
   removed





Issue Time Tracking
-------------------

    Worklog Id:     (was: 783690)
    Time Spent: 50m  (was: 40m)

> .NET/C# Allow Reflect reader/writer to support Dictionaries keyed by 
> something other than string
> ------------------------------------------------------------------------------------------------
>
>                 Key: AVRO-3540
>                 URL: https://issues.apache.org/jira/browse/AVRO-3540
>             Project: Apache Avro
>          Issue Type: Improvement
>          Components: csharp
>            Reporter: Christopher Fingar
>            Priority: Major
>              Labels: pull-request-available
>          Time Spent: 50m
>  Remaining Estimate: 0h
>
> Currently the existing reflect code does not support converting other key 
> types to string.
> My change provides an opt-in functionality using the default converters. 
> This allows users to keep there existing code instead of converting types to 
> use string keys.
>  
>  



--
This message was sent by Atlassian Jira
(v8.20.7#820007)

Reply via email to