Skip to content

Commit

Permalink
Add a test that sets non default values for all the kotlin flags.
Browse files Browse the repository at this point in the history
  • Loading branch information
elshad-faire authored and oldergod committed Aug 15, 2023
1 parent f68b9be commit 2921d96
Show file tree
Hide file tree
Showing 7 changed files with 421 additions and 22 deletions.
32 changes: 10 additions & 22 deletions wire-compiler/src/main/java/com/squareup/wire/WireCompiler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,12 @@ class WireCompiler internal constructor(
private const val JAVA_INTEROP = "--java_interop"
private const val DRY_RUN = "--dry_run"
private const val KOTLIN_BOX_ONEOFS_MIN_SIZE = "--kotlin_box_oneofs_min_size="
private const val KOTLIN_EXCLUSIVE = "--kotlin_exclusive"
private const val KOTLIN_RPC_CALL_STYLE = "--kotlin_rpc_call_style"
private const val KOTLIN_RPC_ROLE = "--kotlin_rpc_role"
private const val NO_KOTLIN_EXCLUSIVE = "--no_kotlin_exclusive"
private const val KOTLIN_RPC_CALL_STYLE = "--kotlin_rpc_call_style="
private const val KOTLIN_RPC_ROLE = "--kotlin_rpc_role="
private const val KOTLIN_SINGLE_METHOD_SERVICES = "--kotlin_single_method_services"
private const val KOTLIN_GRPC_SERVER_COMPATIBLE = "--kotlin_grpc_server_compatible"
private const val KOTLIN_NAMESUFFIX = "--kotlin_namesuffix"
private const val KOTLIN_NAME_SUFFIX = "--kotlin_name_suffix="
private const val KOTLIN_BUILDERS_ONLY = "--kotlin_builders_only"

@Throws(IOException::class)
Expand Down Expand Up @@ -333,10 +333,6 @@ class WireCompiler internal constructor(
kotlinBoxOneOfsMinSize = arg.substring(KOTLIN_BOX_ONEOFS_MIN_SIZE.length).toInt()
}

arg.startsWith(KOTLIN_EXCLUSIVE) -> {
kotlinExclusive = arg.substring(KOTLIN_EXCLUSIVE.length).toBoolean()
}

arg.startsWith(KOTLIN_RPC_CALL_STYLE) -> {
kotlinRpcCallStyle = RpcCallStyle.valueOf(arg.substring(KOTLIN_RPC_CALL_STYLE.length).uppercase())
}
Expand All @@ -345,20 +341,8 @@ class WireCompiler internal constructor(
kotlinRpcRole = RpcRole.valueOf(arg.substring(KOTLIN_RPC_ROLE.length).uppercase())
}

arg.startsWith(KOTLIN_SINGLE_METHOD_SERVICES) -> {
kotlinSingleMethodServices = arg.substring(KOTLIN_SINGLE_METHOD_SERVICES.length).toBoolean()
}

arg.startsWith(KOTLIN_GRPC_SERVER_COMPATIBLE) -> {
kotlinGrpcServerCompatible = arg.substring(KOTLIN_GRPC_SERVER_COMPATIBLE.length).toBoolean()
}

arg.startsWith(KOTLIN_NAMESUFFIX) -> {
kotlinNameSuffix = arg.substring(KOTLIN_NAMESUFFIX.length)
}

arg.startsWith(KOTLIN_BUILDERS_ONLY) -> {
kotlinBuildersOnly = arg.substring(KOTLIN_BUILDERS_ONLY.length).toBoolean()
arg.startsWith(KOTLIN_NAME_SUFFIX) -> {
kotlinNameSuffix = arg.substring(KOTLIN_NAME_SUFFIX.length)
}

arg.startsWith(SWIFT_OUT_FLAG) -> {
Expand Down Expand Up @@ -404,6 +388,10 @@ class WireCompiler internal constructor(
modules = parseManifestModules(yaml)
}

arg == NO_KOTLIN_EXCLUSIVE -> kotlinExclusive = false
arg == KOTLIN_SINGLE_METHOD_SERVICES -> kotlinSingleMethodServices = true
arg == KOTLIN_GRPC_SERVER_COMPATIBLE -> kotlinGrpcServerCompatible = true
arg == KOTLIN_BUILDERS_ONLY -> kotlinBuildersOnly = true
arg == ANDROID -> emitAndroid = true
arg == ANDROID_ANNOTATIONS -> emitAndroidAnnotations = true
arg == COMPACT -> emitCompact = true
Expand Down
23 changes: 23 additions & 0 deletions wire-compiler/src/test/java/com/squareup/wire/WireCompilerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,29 @@ class WireCompilerTest {
assertKotlinOutputs(outputs)
}

@Test
fun testWithAllKotlinFlags() {
val sources = arrayOf("service_kotlin_with_all_flags.proto")
compileToKotlin(
sources,
"--no_kotlin_exclusive",
"--kotlin_rpc_call_style=blocking",
"--kotlin_rpc_role=server",
"--kotlin_single_method_services",
"--kotlin_grpc_server_compatible",
"--kotlin_name_suffix=SomeSuffix",
"--kotlin_builders_only",
)

val outputs = arrayOf(
"com/squareup/wire/protos/kotlin/services/all_flags_on/SomeRequest.kt",
"com/squareup/wire/protos/kotlin/services/all_flags_on/SomeResponse.kt",
"com/squareup/wire/protos/kotlin/services/all_flags_on/SomeServiceSomeMethodSomeSuffix.kt",
"com/squareup/wire/protos/kotlin/services/all_flags_on/SomeServiceWireGrpc.kt",
)
assertKotlinOutputs(outputs)
}

private fun compileToJava(sources: Array<String>, vararg extraArgs: String) =
invokeCompiler(TargetLanguage.JAVA, sources, *extraArgs)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2019 Square Inc.
*
* 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 squareup.protos.kotlin;

option java_package = "com.squareup.wire.protos.kotlin.services.all_flags_on";

message SomeRequest {}
message SomeResponse {}

service SomeService {
rpc SomeMethod (SomeRequest) returns (SomeResponse);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Code generated by Wire protocol buffer compiler, do not edit.
// Source: squareup.protos.kotlin.SomeRequest in service_kotlin_with_all_flags.proto
package com.squareup.wire.protos.kotlin.services.all_flags_on

import com.squareup.wire.FieldEncoding
import com.squareup.wire.Message
import com.squareup.wire.ProtoAdapter
import com.squareup.wire.ProtoReader
import com.squareup.wire.ProtoWriter
import com.squareup.wire.ReverseProtoWriter
import com.squareup.wire.Syntax.PROTO_2
import com.squareup.wire.`internal`.JvmField
import kotlin.Any
import kotlin.Boolean
import kotlin.Int
import kotlin.Long
import kotlin.String
import okio.ByteString

public class SomeRequest internal constructor(
unknownFields: ByteString = ByteString.EMPTY,
) : Message<SomeRequest, SomeRequest.Builder>(ADAPTER, unknownFields) {
override fun newBuilder(): Builder {
val builder = Builder()
builder.addUnknownFields(unknownFields)
return builder
}

override fun equals(other: Any?): Boolean {
if (other === this) return true
if (other !is SomeRequest) return false
if (unknownFields != other.unknownFields) return false
return true
}

override fun hashCode(): Int = unknownFields.hashCode()

override fun toString(): String = "SomeRequest{}"

public fun copy(unknownFields: ByteString = this.unknownFields): SomeRequest =
SomeRequest(unknownFields)

public class Builder : Message.Builder<SomeRequest, Builder>() {
override fun build(): SomeRequest = SomeRequest(
unknownFields = buildUnknownFields()
)
}

public companion object {
@JvmField
public val ADAPTER: ProtoAdapter<SomeRequest> = object : ProtoAdapter<SomeRequest>(
FieldEncoding.LENGTH_DELIMITED,
SomeRequest::class,
"type.googleapis.com/squareup.protos.kotlin.SomeRequest",
PROTO_2,
null,
"service_kotlin_with_all_flags.proto"
) {
override fun encodedSize(`value`: SomeRequest): Int {
var size = value.unknownFields.size
return size
}

override fun encode(writer: ProtoWriter, `value`: SomeRequest) {
writer.writeBytes(value.unknownFields)
}

override fun encode(writer: ReverseProtoWriter, `value`: SomeRequest) {
writer.writeBytes(value.unknownFields)
}

override fun decode(reader: ProtoReader): SomeRequest {
val unknownFields = reader.forEachTag(reader::readUnknownField)
return SomeRequest(
unknownFields = unknownFields
)
}

override fun redact(`value`: SomeRequest): SomeRequest = value.copy(
unknownFields = ByteString.EMPTY
)
}

private const val serialVersionUID: Long = 0L
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Code generated by Wire protocol buffer compiler, do not edit.
// Source: squareup.protos.kotlin.SomeResponse in service_kotlin_with_all_flags.proto
package com.squareup.wire.protos.kotlin.services.all_flags_on

import com.squareup.wire.FieldEncoding
import com.squareup.wire.Message
import com.squareup.wire.ProtoAdapter
import com.squareup.wire.ProtoReader
import com.squareup.wire.ProtoWriter
import com.squareup.wire.ReverseProtoWriter
import com.squareup.wire.Syntax.PROTO_2
import com.squareup.wire.`internal`.JvmField
import kotlin.Any
import kotlin.Boolean
import kotlin.Int
import kotlin.Long
import kotlin.String
import okio.ByteString

public class SomeResponse internal constructor(
unknownFields: ByteString = ByteString.EMPTY,
) : Message<SomeResponse, SomeResponse.Builder>(ADAPTER, unknownFields) {
override fun newBuilder(): Builder {
val builder = Builder()
builder.addUnknownFields(unknownFields)
return builder
}

override fun equals(other: Any?): Boolean {
if (other === this) return true
if (other !is SomeResponse) return false
if (unknownFields != other.unknownFields) return false
return true
}

override fun hashCode(): Int = unknownFields.hashCode()

override fun toString(): String = "SomeResponse{}"

public fun copy(unknownFields: ByteString = this.unknownFields): SomeResponse =
SomeResponse(unknownFields)

public class Builder : Message.Builder<SomeResponse, Builder>() {
override fun build(): SomeResponse = SomeResponse(
unknownFields = buildUnknownFields()
)
}

public companion object {
@JvmField
public val ADAPTER: ProtoAdapter<SomeResponse> = object : ProtoAdapter<SomeResponse>(
FieldEncoding.LENGTH_DELIMITED,
SomeResponse::class,
"type.googleapis.com/squareup.protos.kotlin.SomeResponse",
PROTO_2,
null,
"service_kotlin_with_all_flags.proto"
) {
override fun encodedSize(`value`: SomeResponse): Int {
var size = value.unknownFields.size
return size
}

override fun encode(writer: ProtoWriter, `value`: SomeResponse) {
writer.writeBytes(value.unknownFields)
}

override fun encode(writer: ReverseProtoWriter, `value`: SomeResponse) {
writer.writeBytes(value.unknownFields)
}

override fun decode(reader: ProtoReader): SomeResponse {
val unknownFields = reader.forEachTag(reader::readUnknownField)
return SomeResponse(
unknownFields = unknownFields
)
}

override fun redact(`value`: SomeResponse): SomeResponse = value.copy(
unknownFields = ByteString.EMPTY
)
}

private const val serialVersionUID: Long = 0L
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Code generated by Wire protocol buffer compiler, do not edit.
// Source: squareup.protos.kotlin.SomeService in service_kotlin_with_all_flags.proto
package com.squareup.wire.protos.kotlin.services.all_flags_on

import com.squareup.wire.Service
import com.squareup.wire.WireRpc

public interface SomeServiceSomeMethodSomeSuffix : Service {
@WireRpc(
path = "/squareup.protos.kotlin.SomeService/SomeMethod",
requestAdapter = "com.squareup.wire.protos.kotlin.services.all_flags_on.SomeRequest#ADAPTER",
responseAdapter = "com.squareup.wire.protos.kotlin.services.all_flags_on.SomeResponse#ADAPTER",
sourceFile = "service_kotlin_with_all_flags.proto",
)
public fun SomeMethod(request: SomeRequest): SomeResponse
}
Loading

0 comments on commit 2921d96

Please sign in to comment.