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

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

                Author: ASF GitHub Bot
            Created on: 03/Apr/22 21:20
            Start Date: 03/Apr/22 21:20
    Worklog Time Spent: 10m 
      Work Description: yanivru commented on code in PR #1597:
URL: https://github.com/apache/avro/pull/1597#discussion_r841278792


##########
lang/csharp/src/apache/main/Schema/EnumSchema.cs:
##########
@@ -103,15 +133,46 @@ internal static EnumSchema NewInstance(JToken jtok, 
PropertyMap props, SchemaNam
                             string doc, string defaultSymbol)
                             : base(Type.Enumeration, name, aliases, props, 
names, doc)
         {
-            if (null == name.Name) throw new SchemaParseException("name cannot 
be null for enum schema.");
+            if (null == name.Name) throw new AvroException("name cannot be 
null for enum schema.");
             this.Symbols = symbols;
             this.symbolMap = symbolMap;
 
             if (null != defaultSymbol && !symbolMap.ContainsKey(defaultSymbol))
-                throw new SchemaParseException($"Default symbol: 
{defaultSymbol} not found in symbols");
+                throw new AvroException($"Default symbol: {defaultSymbol} not 
found in symbols");
             Default = defaultSymbol;
         }
 
+        /// <summary>
+        /// Creates symbols map from specified list of symbols.
+        /// Symbol map contains the names of the symbols and their index.
+        /// </summary>
+        /// <param name="symbols">List of symbols</param>
+        /// <returns>Symbol map</returns>
+        /// <exception cref="AvroException">Is thrown if the symbols list 
contains invalid symbol name or duplicate symbols</exception>
+        private static IDictionary<string, int> 
CreateSymbolsMap(IEnumerable<string> symbols)
+        {
+            IDictionary<string, int> symbolMap = new Dictionary<string, int>();
+            int i = 0;
+            foreach (var symbol in symbols)
+            {
+                if (ValidateSymbolName(symbol))
+                {
+                    throw new AvroException($"Invalid symbol name: {symbol}");
+                }
+
+                if (symbolMap.ContainsKey(symbol))
+                {
+                    throw new AvroException($"Duplicate symbol: {symbol}");
+                }
+
+                symbolMap[symbol] = i++;
+            }
+
+            return symbolMap;
+        }
+
+        private static bool ValidateSymbolName(string symbol) => 
string.IsNullOrEmpty(symbol) || !Regex.IsMatch(symbol, 
"^([A-Za-z_][A-Za-z0-9_]*)$");

Review Comment:
   Validate that returns bool. Don't know what I was thinking. Fixed.
   
   Yeah, Regex is not really good at performance (I don't like the readability 
either), but since it's a new feature (the parsing code doesn't use this 
validation) so we are not degrading performance, and since my guess that 
usually schemas are not created in a tight loop (usually created once and 
reused), it should be ok. It can be fixed if it does prove to be a problem.





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

    Worklog Id:     (was: 752065)
    Time Spent: 4h 50m  (was: 4h 40m)

> SchemaBuilder equivalent or other means of schema creation
> ----------------------------------------------------------
>
>                 Key: AVRO-2211
>                 URL: https://issues.apache.org/jira/browse/AVRO-2211
>             Project: Apache Avro
>          Issue Type: Wish
>          Components: csharp
>            Reporter: Dan Stelljes
>            Priority: Major
>              Labels: pull-request-available
>          Time Spent: 4h 50m
>  Remaining Estimate: 0h
>
> Currently, the only way to create a schema is via the Schema.Parse method. 
> We'd like to be able to build a schema programmatically—would it be possible 
> to (1) introduce an analogue to the SchemaBuilder from the Java library or 
> (2) expose constructors on Schema and its descendants?
> Would be more than happy to contribute work on this given some direction.



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to