Skip to content

Commit

Permalink
fix: correct capitalization of proto files which don't follow the sty…
Browse files Browse the repository at this point in the history
…le guide. (#189)
  • Loading branch information
lowasser authored Oct 12, 2020
1 parent 0cbd1ef commit bc0882f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ val FileDescriptorProto.outerClassSimpleName: ClassSimpleName
explicitOuterClassSimpleName?.let { return it }

val defaultOuterClassName = ClassSimpleName(
LOWER_UNDERSCORE.to(UPPER_CAMEL, fileName.name.replace("-", "_"))
fileName.name.replace("-", "_").underscoresToCamel()
)

val foundDuplicate =
Expand All @@ -136,6 +136,30 @@ val FileDescriptorProto.outerClassSimpleName: ClassSimpleName
}
}

private fun String.underscoresToCamel(): String {
val builder = StringBuilder()
var capNextLetter = true
for ((i, ch) in this.withIndex()) {
if (ch in 'a'..'z') {
builder.append(
if (capNextLetter) Ascii.toUpperCase(ch) else ch
)
capNextLetter = false
} else if (ch in 'A'..'Z') {
builder.append(
if (i == 0 && !capNextLetter) Ascii.toLowerCase(ch) else ch
)
capNextLetter = false
} else if (ch in '0'..'9') {
builder.append(ch)
capNextLetter = true
} else {
capNextLetter = true
}
}
return builder.toString()
}

private fun DescriptorProto.anyHaveNameRecursive(name: ClassSimpleName): Boolean =
messageClassSimpleName == name ||
enumTypeList.any { it.enumClassSimpleName == name } ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ kt_jvm_test(
"//compiler/src/test/proto/testing:has_outer_class_name_conflict_java_proto",
"//compiler/src/test/proto/testing:implicit_java_package_java_proto",
"//compiler/src/test/proto/testing:service_name_conflicts_with_file_java_proto",
"//compiler/src/test/proto/testing:service_t_java_proto",
"@maven//:com_google_truth_truth",
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import io.grpc.kotlin.generator.protoc.testproto.Example3
import io.grpc.kotlin.generator.protoc.testproto.HasOuterClassNameConflictOuterClass
import io.grpc.kotlin.generator.protoc.testproto.MyExplicitOuterClassName
import io.grpc.testing.ServiceNameConflictsWithFileOuterClass
import io.grpc.testing.ServiceTOuterClass
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
Expand Down Expand Up @@ -184,7 +185,8 @@ class GeneratorConfigTest {
HasOuterClassNameConflictOuterClass::class,
ImplicitJavaPackage.getDescriptor() to ImplicitJavaPackage::class,
ServiceNameConflictsWithFileOuterClass.getDescriptor() to
ServiceNameConflictsWithFileOuterClass::class
ServiceNameConflictsWithFileOuterClass::class,
ServiceTOuterClass.getDescriptor() to ServiceTOuterClass::class
)

@Test
Expand Down
11 changes: 11 additions & 0 deletions compiler/src/test/proto/testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,14 @@ java_proto_library(
name = "service_name_conflicts_with_file_java_proto",
deps = ["service_name_conflicts_with_file_proto"],
)

proto_library(
name = "service_t_proto",
srcs = ["serviceT.proto"],
strip_import_prefix = "//compiler/src/test/proto",
)

java_proto_library(
name = "service_t_java_proto",
deps = ["service_t_proto"],
)
10 changes: 10 additions & 0 deletions compiler/src/test/proto/testing/serviceT.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";
package io.grpc.testing;

service ServiceT {
rpc serviceT(stream reqT) returns (stream resT) {}
}

message reqT {}

message resT {}

0 comments on commit bc0882f

Please sign in to comment.