Skip to content

Commit a9d3436

Browse files
committed
fix method name capitalization after a digit, closes #8
1 parent 6c76146 commit a9d3436

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
syntax = "proto3";
2+
3+
package test.namingcase;
4+
5+
option java_multiple_files = true;
6+
option java_package = "org.test.namingcase";
7+
8+
message Test {
9+
optional string id1issuertype = 1;
10+
optional string id12issuertype = 2;
11+
optional string id1issuer1type = 3;
12+
optional string id3Issuertype = 4;
13+
}

protoc-gen-java-optional/src/main/java/org/grpcmock/protoc/plugin/OptionalGenerator.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.OptionalInt;
2626
import java.util.OptionalLong;
2727
import java.util.function.Function;
28+
import java.util.regex.Pattern;
2829
import java.util.stream.Collectors;
2930
import java.util.stream.Stream;
3031

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

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

224228
private String getJavaTypeName(FieldDescriptorProto fieldDescriptor) {
@@ -260,4 +264,8 @@ private static boolean hasFieldPresence(FieldDescriptorProto fieldDescriptor) {
260264
private static String templatePath(String path) {
261265
return TEMPLATES_DIRECTORY + path;
262266
}
267+
268+
private static String capitalize(String value) {
269+
return value.substring(0, 1).toUpperCase(Locale.ROOT) + value.substring(1);
270+
}
263271
}

0 commit comments

Comments
 (0)