Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update codegen to support malformed rpc method names #38

Merged
merged 1 commit into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import com.google.protobuf.DescriptorProtos
import com.google.protobuf.DescriptorProtos.FieldDescriptorProto.Label.LABEL_REPEATED
import com.google.protobuf.compiler.PluginProtos
import com.squareup.kotlinpoet.*
import org.jetbrains.kotlin.utils.sure

object ProtoBuildersGenerator : Generator {

Expand Down Expand Up @@ -178,7 +177,7 @@ object ProtoBuildersGenerator : Generator {
.filterNot { it.second.isMapEntry }
.map { (fieldDescriptorProto, protoMessageForField) ->

val fieldNameCamelCase = camelCaseFieldName(fieldDescriptorProto.name)
val fieldNameCamelCase = fieldDescriptorProto.name.toUpperCamelCase()
val statementTemplate = "return this.%N(%T.newBuilder().apply(block).build())"

val funSpecBuilder = if (fieldDescriptorProto.label == LABEL_REPEATED)
Expand Down Expand Up @@ -241,13 +240,4 @@ object ProtoBuildersGenerator : Generator {

private val ProtoFile.dslBuilderClassName: ClassName
get() = ClassName(javaPackage, "${javaOuterClassname}DslBuilder")
}

private val camelCaseFieldName = { it: String ->
// We cant use CaseFormat.UPPER_CAMEL since
// protoc is lenient with malformed field names
if (it.contains("_"))
it.split("_").joinToString(separator = "") { it.capitalize() } else
it.capitalize()

}.memoize()
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@

package com.github.marcoferrer.krotoplus.proto

import com.github.marcoferrer.krotoplus.utils.toUpperCamelCase
import com.google.protobuf.DescriptorProtos
import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.asClassName
import io.grpc.MethodDescriptor

class ProtoMethod(
override val descriptorProto: DescriptorProtos.MethodDescriptorProto,
val protoService: ProtoService
) : Schema.DescriptorWrapper {

val functionName = descriptorProto.name.decapitalize()
val functionName = descriptorProto.name.toUpperCamelCase().let{
if(descriptorProto.name.startsWith("_"))
"_$it" else it.decapitalize()
}

val methodDefinitionGetterName = "get${descriptorProto.name}Method"
val methodDefinitionGetterName = "get${descriptorProto.name.toUpperCamelCase()}Method"

val requestType = protoService.protoFile.schema.protoTypes[descriptorProto.inputType]
?: throw IllegalStateException("${descriptorProto.inputType} was not found in schema type map.")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2019 Kroto+ Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.marcoferrer.krotoplus.utils

val upperCamelCase = { it: String ->
// We cant use CaseFormat.UPPER_CAMEL since
// protoc is lenient with malformed field names
if (it.contains("_"))
it.split("_").joinToString(separator = "") { it.capitalize() } else
it.capitalize()

}.memoize()

fun String.toUpperCamelCase(): String = upperCamelCase(this)
17 changes: 17 additions & 0 deletions test-api/src/main/proto/message/test_messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,21 @@ message TestRepeated {
repeated string string_field = 15;
repeated bytes bytes_field = 16;
repeated L1Message1 message_field = 17;
}


service __MalformedService__ {

rpc say_hello (L1Message1) returns (L1Message2);

rpc sayHelloStreaming (stream L1Message1) returns (stream L1Message2);
}

service malformed_SERvice_2 {

rpc say_hello (L1Message1) returns (L1Message2);

rpc __sayHEloStr_eaming (stream L1Message1) returns (stream L1Message2);

rpc _____s_a_YH_EloStr_eaming____ (stream L1Message1) returns (stream L1Message2);
}