rdblue commented on a change in pull request #220: Support parsing of special
characters in TypeToSchema visitor
URL: https://github.com/apache/incubator-iceberg/pull/220#discussion_r296340114
##########
File path: core/src/main/java/org/apache/iceberg/avro/TypeToSchema.java
##########
@@ -214,4 +216,58 @@ public Schema primitive(Type.PrimitiveType primitive) {
return primitiveSchema;
}
+
+ private static String sanitize(String name) {
+ if (validAvroName(name)) {
+ return name;
+ }
+ int length = name.length();
+ StringBuilder sb = new StringBuilder(name.length());
+ char first = name.charAt(0);
+ if (!(Character.isLetter(first) || first == '_')) {
+ sb.append(sanitizedValue(first));
+ } else {
+ sb.append(first);
+ }
+ for (int i = 1; i < length; i++) {
+ char character = name.charAt(i);
+ if (!(Character.isLetterOrDigit(character) || character == '_')) {
+ sb.append(sanitizedValue(character));
+ } else {
+ sb.append(character);
+ }
+ }
+ return sb.toString();
+ }
+
+ private static final Map<Character, String> SANITIZE_MAP = new ImmutableMap
+ .Builder<Character, String>()
+ .put('.', "__DOT__").put('#', "__HASH__")
+ .put('0', "__ZERO__").put('1', "__ONE__").put('2', "__TWO__").put('3',
"__THREE__").put('4', "__FOUR__")
+ .put('5', "__FIVE__").put('6', "__SIX__").put('7', "__SEVEN__").put('8',
"__EIGHT__").put('9', "__NINE__")
+ .build();
+
+ private static String sanitizedValue(Character character) {
+ String value = SANITIZE_MAP.get(character);
+ Preconditions.checkState(value != null, "Special character %s not yet
supported", character);
+ return value;
+ }
+
+ private static boolean validAvroName(String name) {
+ int length = name.length();
+ if (length == 0) {
+ throw new IllegalArgumentException("Empty name " + name);
Review comment:
The check looks fine, but could you use `Preconditions` instead of an if?
----------------------------------------------------------------
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:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]