You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@avro.apache.org by GitBox <gi...@apache.org> on 2020/11/13 02:18:43 UTC

[GitHub] [avro] blachniet commented on a change in pull request #820: AVRO-2750: Add support for enum defaults in c#

blachniet commented on a change in pull request #820:
URL: https://github.com/apache/avro/pull/820#discussion_r383003202



##########
File path: lang/csharp/src/apache/main/Generic/GenericEnum.cs
##########
@@ -37,8 +37,15 @@ public class GenericEnum
             get { return value; }
             set
             {
-                if (! Schema.Contains(value)) throw new AvroException("Unknown value for enum: " + value + "(" + Schema + ")");
-                this.value = value;
+                if (!string.IsNullOrEmpty(Schema.Default))
+                {
+                    this.value = Schema.Contains(value) ? value : Schema.Default;
+                }
+                else
+                {
+                    if (! Schema.Contains(value)) throw new AvroException("Unknown value for enum: " + value + "(" + Schema + ")");
+                    this.value = value;
+                }

Review comment:
       We could change up the order of these checks to simplify the code a little. We would only have to check whether or not `Schema.Default` is null/empty when the schema doesn't contain the given value, which is something we always have to check.
   
   ```c#
   if (!Schema.Contains(value))
   {
       if (!string.IsNullOrEmpty(Schema.Default))
       {
           this.value = Schema.Default;
       }
       else
       {
           throw new AvroException("Unknown value for enum: " + value + "(" + Schema + ")");
       }
   }
   else
   {
       this.value = value;
   }
   ```




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org