Skip to content

Commit

Permalink
fix method name capitalization after a digit, closes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Fadelis committed Oct 27, 2023
1 parent 6c76146 commit a9d3436
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
syntax = "proto3";

package test.namingcase;

option java_multiple_files = true;
option java_package = "org.test.namingcase";

message Test {
optional string id1issuertype = 1;
optional string id12issuertype = 2;
optional string id1issuer1type = 3;
optional string id3Issuertype = 4;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -43,6 +44,7 @@ public class OptionalGenerator extends Generator {
private static final String CLASS_SCOPE = "class_scope:";
private static final String DEFAULT_OPTIONAL_CLASS = Optional.class.getName();
private static final String DEFAULT_OPTIONAL_GETTER_METHOD = "get";
private static final Pattern METHOD_NAME_SPLIT_PATTERN = Pattern.compile("(?<=\\d)(?=[a-z])");
private static final Map<JavaType, String> PRIMITIVE_CLASSES = ImmutableMap.<JavaType, String>builder()
.put(JavaType.INT, Integer.class.getSimpleName())
.put(JavaType.LONG, Long.class.getSimpleName())
Expand Down Expand Up @@ -218,7 +220,9 @@ private String getFileName(FileDescriptorProto fileDescriptor, DescriptorProto m
}

private String getJavaMethodName(FieldDescriptorProto fieldDescriptor) {
return fieldDescriptor.getJsonName().substring(0, 1).toUpperCase(Locale.ROOT) + fieldDescriptor.getJsonName().substring(1);
return Stream.of(METHOD_NAME_SPLIT_PATTERN.split(fieldDescriptor.getJsonName()))
.map(OptionalGenerator::capitalize)
.collect(Collectors.joining());
}

private String getJavaTypeName(FieldDescriptorProto fieldDescriptor) {
Expand Down Expand Up @@ -260,4 +264,8 @@ private static boolean hasFieldPresence(FieldDescriptorProto fieldDescriptor) {
private static String templatePath(String path) {
return TEMPLATES_DIRECTORY + path;
}

private static String capitalize(String value) {
return value.substring(0, 1).toUpperCase(Locale.ROOT) + value.substring(1);
}
}

0 comments on commit a9d3436

Please sign in to comment.