Skip to content

Commit

Permalink
fix: Do not use regexp for BigQuerySchemaUtil#isProtoCompatible (#2226)
Browse files Browse the repository at this point in the history
* fix: Do not use regexp for BigQuerySchemaUtil#isProtoCompatible

Signed-off-by: Sergey Nuyanzin <snuyanzin@gmail.com>

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Signed-off-by: Sergey Nuyanzin <snuyanzin@gmail.com>
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: Yiru Tang <yiru@google.com>
  • Loading branch information
3 people committed Jan 2, 2024
1 parent 91a2bec commit 1741166
Showing 1 changed file with 19 additions and 5 deletions.
Expand Up @@ -18,14 +18,10 @@
import com.google.protobuf.Descriptors.FieldDescriptor;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.regex.Pattern;

public class BigQuerySchemaUtil {

private static final String PROTO_COMPATIBLE_NAME_REGEXP = "[A-Za-z_][A-Za-z0-9_]*";
private static final String PLACEHOLDER_FILED_NAME_PREFIX = "col_";
private static final Pattern PROTO_COMPATIBLE_NAME_PATTERN =
Pattern.compile(PROTO_COMPATIBLE_NAME_REGEXP);

/**
* * Checks if the field name is compatible with proto field naming convention.
Expand All @@ -35,7 +31,25 @@ public class BigQuerySchemaUtil {
* false.
*/
public static boolean isProtoCompatible(String fieldName) {
return PROTO_COMPATIBLE_NAME_PATTERN.matcher(fieldName).matches();
int length = fieldName.length();
if (length < 1) {
return false;
}
char ch = fieldName.charAt(0);
if (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_')) {
return false;
}
for (int i = 1; i < length; i++) {
ch = fieldName.charAt(i);
if (!((ch >= 'a' && ch <= 'z')
|| (ch >= 'A' && ch <= 'Z')
|| ch == '_'
|| (ch >= '0' && ch <= '9'))) {
return false;
}
}

return true;
}

/**
Expand Down

0 comments on commit 1741166

Please sign in to comment.