From a9d3436a4c838296d9fecaea985a1804ef3d0f27 Mon Sep 17 00:00:00 2001 From: Fadelis Date: Thu, 26 Oct 2023 20:09:22 +0300 Subject: [PATCH] fix method name capitalization after a digit, closes #8 --- .../src/test/resources/irregular_naming_case.proto | 13 +++++++++++++ .../grpcmock/protoc/plugin/OptionalGenerator.java | 10 +++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 protoc-gen-java-optional-test/src/test/resources/irregular_naming_case.proto diff --git a/protoc-gen-java-optional-test/src/test/resources/irregular_naming_case.proto b/protoc-gen-java-optional-test/src/test/resources/irregular_naming_case.proto new file mode 100644 index 0000000..6ba6fad --- /dev/null +++ b/protoc-gen-java-optional-test/src/test/resources/irregular_naming_case.proto @@ -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; +} \ No newline at end of file diff --git a/protoc-gen-java-optional/src/main/java/org/grpcmock/protoc/plugin/OptionalGenerator.java b/protoc-gen-java-optional/src/main/java/org/grpcmock/protoc/plugin/OptionalGenerator.java index 99bbd30..22eaf91 100644 --- a/protoc-gen-java-optional/src/main/java/org/grpcmock/protoc/plugin/OptionalGenerator.java +++ b/protoc-gen-java-optional/src/main/java/org/grpcmock/protoc/plugin/OptionalGenerator.java @@ -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; @@ -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 PRIMITIVE_CLASSES = ImmutableMap.builder() .put(JavaType.INT, Integer.class.getSimpleName()) .put(JavaType.LONG, Long.class.getSimpleName()) @@ -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) { @@ -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); + } }