Skip to content

Commit

Permalink
Merge pull request square#1882 from square/jwilson.0114.honor_profiles
Browse files Browse the repository at this point in the history
Honor profiles in KotlinGenerator
  • Loading branch information
swankjesse authored Jan 15, 2021
2 parents 77676a7 + 31a1444 commit 5fac94f
Show file tree
Hide file tree
Showing 21 changed files with 211 additions and 78 deletions.
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ includeBuild('wire-library') {
substitute module('com.squareup.wire:wire-kotlin-generator') with project(':wire-kotlin-generator')
substitute module('com.squareup.wire:wire-kotlin-serialization') with project(':wire-kotlin-serialization')
substitute module('com.squareup.wire:wire-moshi-adapter') with project(':wire-moshi-adapter')
substitute module('com.squareup.wire:wire-profiles') with project(':wire-profiles')
substitute module('com.squareup.wire:wire-runtime') with project(':wire-runtime')
substitute module('com.squareup.wire:wire-schema') with project(':wire-schema')
substitute module('com.squareup.wire:wire-test-utils') with project(':wire-test-utils')
Expand Down
1 change: 1 addition & 0 deletions wire-library/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ include ':wire-java-generator'
include ':wire-kotlin-generator'
include ':wire-kotlin-serialization'
include ':wire-moshi-adapter'
include ':wire-profiles'
include ':wire-runtime:japicmp'
include ':wire-runtime'
include ':wire-schema'
Expand Down
1 change: 1 addition & 0 deletions wire-library/wire-compiler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies {
api project(':wire-kotlin-generator')
implementation project(':wire-java-generator')
implementation project(':wire-swift-generator')
implementation project(':wire-profiles')
implementation deps.okio.jvm
implementation deps.guava
implementation deps.kotlin.serialization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ data class KotlinTarget(
logger: WireLogger,
profileLoader: ProfileLoader
): SchemaHandler {
val profileName = if (android) "android" else "java"
val profile = profileLoader.loadProfile(profileName, schema)

val modulePath = run {
val outPath = fs.getPath(outDirectory)
if (moduleName != null) {
Expand All @@ -305,6 +308,7 @@ data class KotlinTarget(

val kotlinGenerator = KotlinGenerator(
schema = schema,
profile = profile,
emitAndroid = android,
javaInterop = javaInterop,
emitDeclaredOptions = emitDeclaredOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import com.squareup.wire.java.AdapterConstant
import com.squareup.wire.java.Profile
import com.squareup.wire.testing.add
import com.squareup.wire.testing.addZip
import java.io.IOException
import org.assertj.core.api.Assertions.assertThat
import org.junit.Assert.fail
import org.junit.Test
import java.io.IOException

class ProfileLoaderTest {
private val fs = Jimfs.newFileSystem(Configuration.unix())
Expand Down Expand Up @@ -65,12 +65,12 @@ class ProfileLoaderTest {
val profile = loadAndLinkProfile("android")

val message1 = ProtoType.get("a.b.Message1")
assertThat(profile.getTarget(message1)).isEqualTo(ClassName.OBJECT)
assertThat(profile.javaTarget(message1)).isEqualTo(ClassName.OBJECT)
assertThat(profile.getAdapter(message1))
.isEqualTo(AdapterConstant("com.example.Message1#OBJECT_ADAPTER"))

val message2 = ProtoType.get("a.b.c.Message2")
assertThat(profile.getTarget(message2))
assertThat(profile.javaTarget(message2))
.isEqualTo(ClassName.get(String::class.java))
assertThat(profile.getAdapter(message2))
.isEqualTo(AdapterConstant("com.example.Message2#STRING_ADAPTER"))
Expand Down Expand Up @@ -99,7 +99,7 @@ class ProfileLoaderTest {
)

val message = ProtoType.get("a.b.Message")
assertThat(profile.getTarget(message)).isEqualTo(ClassName.OBJECT)
assertThat(profile.javaTarget(message)).isEqualTo(ClassName.OBJECT)
assertThat(profile.getAdapter(message))
.isEqualTo(AdapterConstant("com.example.Message#ADAPTER"))
}
Expand All @@ -121,7 +121,7 @@ class ProfileLoaderTest {
)
val profile = loadAndLinkProfile("android")
val message = ProtoType.get("a.b.Message")
assertThat(profile.getTarget(message)).isNull()
assertThat(profile.javaTarget(message)).isNull()
}

@Test
Expand Down
1 change: 1 addition & 0 deletions wire-library/wire-java-generator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jar {
dependencies {
api project(':wire-schema')
implementation project(':wire-runtime')
implementation project(':wire-profiles')
implementation deps.okio.jvm
implementation deps.guava
api deps.javapoet
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public Schema schema() {
* if that type wasn't in this generator's schema.
*/
public TypeName typeName(ProtoType protoType) {
TypeName profileJavaName = profile.getTarget(protoType);
TypeName profileJavaName = profile.javaTarget(protoType);
if (profileJavaName != null) return profileJavaName;
TypeName candidate = typeToJavaName.get(protoType);
checkArgument(candidate != null, "unexpected type %s", protoType);
Expand All @@ -356,7 +356,7 @@ public TypeName typeName(ProtoType protoType) {
* protoType}. Returns null if {@code protoType} is not using a custom proto adapter.
*/
public @Nullable ClassName abstractAdapterName(ProtoType protoType) {
TypeName profileJavaName = profile.getTarget(protoType);
TypeName profileJavaName = profile.javaTarget(protoType);
if (profileJavaName == null) return null;

TypeName typeName = typeToJavaName.get(protoType);
Expand Down Expand Up @@ -422,7 +422,7 @@ private CodeBlock singleAdapterFor(ProtoType type) {
} else {
AdapterConstant adapterConstant = profile.getAdapter(type);
if (adapterConstant != null) {
result.add("$T.$L", adapterConstant.className, adapterConstant.memberName);
result.add("$T.$L", adapterConstant.javaClassName, adapterConstant.memberName);
} else {
result.add("$T.ADAPTER", typeName(type));
}
Expand Down Expand Up @@ -1361,7 +1361,7 @@ private String adapterString(ProtoType type) {

AdapterConstant adapterConstant = profile.getAdapter(type);
if (adapterConstant != null) {
return reflectionName(adapterConstant.className) + "#" + adapterConstant.memberName;
return reflectionName(adapterConstant.javaClassName) + "#" + adapterConstant.memberName;
}

return reflectionName(typeName(type)) + "#ADAPTER";
Expand Down
1 change: 1 addition & 0 deletions wire-library/wire-kotlin-generator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ jar {

dependencies {
api project(':wire-schema')
implementation project(':wire-profiles')
implementation project(':wire-runtime')
implementation project(':wire-grpc-client')
implementation deps.okio.jvm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import com.squareup.wire.WireField
import com.squareup.wire.WireRpc
import com.squareup.wire.internal.boxedOneOfKeysFieldName
import com.squareup.wire.internal.camelCase
import com.squareup.wire.java.Profile
import com.squareup.wire.schema.EnclosingType
import com.squareup.wire.schema.EnumConstant
import com.squareup.wire.schema.EnumType
Expand Down Expand Up @@ -108,6 +109,7 @@ class KotlinGenerator private constructor(
val schema: Schema,
private val typeToKotlinName: Map<ProtoType, TypeName>,
private val memberToKotlinName: Map<ProtoMember, TypeName>,
private val profile: Profile,
private val emitAndroid: Boolean,
private val javaInterOp: Boolean,
private val emitDeclaredOptions: Boolean,
Expand All @@ -119,8 +121,12 @@ class KotlinGenerator private constructor(
) {
private val nameAllocatorStore = mutableMapOf<Type, NameAllocator>()

private val ProtoType.typeName
get() = typeToKotlinName.getValue(this)
private val ProtoType.typeName: TypeName
get() {
val profileTypeName = profile.kotlinTarget(this)
if (profileTypeName != null) return profileTypeName
return typeToKotlinName.getValue(this)
}
private val ProtoType.isEnum
get() = schema.getType(this) is EnumType
private val ProtoType.isMessage
Expand Down Expand Up @@ -276,7 +282,6 @@ class KotlinGenerator private constructor(
if (rpcRole == RpcRole.SERVER) {
val wireRpcAnnotationSpec = AnnotationSpec.builder(WireRpc::class.asClassName())
.addMember("path = %S", "/$packageName$serviceName/${rpc.name}")
// TODO(oldergod|jwilson) Lets' use Profile for this.
.addMember("requestAdapter = %S", rpc.requestType!!.adapterString())
.addMember("responseAdapter = %S", rpc.responseType!!.adapterString())
.build()
Expand Down Expand Up @@ -1525,7 +1530,12 @@ class KotlinGenerator private constructor(
}

private fun ProtoType.getAdapterName(adapterFieldDelimiterName: Char = '.'): CodeBlock {
val adapterConstant = profile.getAdapter(this)
return when {
adapterConstant != null -> {
CodeBlock.of("%T%L%L",
adapterConstant.kotlinClassName, adapterFieldDelimiterName, adapterConstant.memberName)
}
isScalar -> {
CodeBlock.of("%T$adapterFieldDelimiterName%L",
ProtoAdapter::class, simpleName.toUpperCase(Locale.US))
Expand Down Expand Up @@ -1588,6 +1598,11 @@ class KotlinGenerator private constructor(
}

private fun ProtoType.adapterString(): String {
val adapterConstant = profile.getAdapter(this)
if (adapterConstant != null) {
return "${adapterConstant.javaClassName.reflectionName()}#${adapterConstant.memberName}"
}

val builtInAdapterString = builtInAdapterString(this)
if (builtInAdapterString != null) return builtInAdapterString

Expand Down Expand Up @@ -2156,6 +2171,7 @@ class KotlinGenerator private constructor(
@JvmStatic @JvmName("get")
operator fun invoke(
schema: Schema,
profile: Profile = Profile(),
emitAndroid: Boolean = false,
javaInterop: Boolean = false,
emitDeclaredOptions: Boolean = true,
Expand Down Expand Up @@ -2202,6 +2218,7 @@ class KotlinGenerator private constructor(

return KotlinGenerator(
schema = schema,
profile = profile,
typeToKotlinName = typeToKotlinName,
memberToKotlinName = memberToKotlinName,
emitAndroid = emitAndroid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,81 @@ class KotlinGeneratorTest {
assertThat(enumCode).contains("""* g \[h.i.j\] k""")
}

@Test fun profileHonored() {
val expectedInterface = """
|package routeguide
|
|import com.squareup.wire.GrpcCall
|import com.squareup.wire.Service
|import java.lang.String
|import java.util.Properties
|
|/**
| * RouteGuide service interface.
| */
|public interface RouteGuideClient : Service {
| /**
| * Returns the \[Feature\] for a \[Point\].
| */
| public fun GetFeature(): GrpcCall<String, Properties>
|}
|""".trimMargin()
val expectedImplementation = """
|package routeguide
|
|import com.example.PropertiesFeatureAdapter
|import com.example.StringPointAdapter
|import com.squareup.wire.GrpcCall
|import com.squareup.wire.GrpcClient
|import com.squareup.wire.GrpcMethod
|import java.lang.String
|import java.util.Properties
|
|/**
| * RouteGuide service interface.
| */
|public class GrpcRouteGuideClient(
| private val client: GrpcClient
|) : RouteGuideClient {
| /**
| * Returns the \[Feature\] for a \[Point\].
| */
| public override fun GetFeature(): GrpcCall<String, Properties> = client.newCall(GrpcMethod(
| path = "/routeguide.RouteGuide/GetFeature",
| requestAdapter = StringPointAdapter.INSTANCE,
| responseAdapter = PropertiesFeatureAdapter.ADAPTER
| ))
|}
|""".trimMargin()

val repoBuilder = RepoBuilder()
.add("routeguide.proto", """
|package routeguide;
|
|// RouteGuide service interface.
|service RouteGuide {
| // Returns the [Feature] for a [Point].
| rpc GetFeature(Point) returns (Feature) {}
|}
|$pointMessage
|$featureMessage
|""".trimMargin())
.add("java.wire", """
|syntax = "wire2";
|import "routeguide.proto";
|
|type routeguide.Point {
| target java.lang.String using com.example.StringPointAdapter#INSTANCE;
|}
|
|type routeguide.Feature {
| target java.util.Properties using com.example.PropertiesFeatureAdapter#ADAPTER;
|}
|""".trimMargin())
assertEquals(listOf(expectedInterface, expectedImplementation),
repoBuilder.generateGrpcKotlin("routeguide.RouteGuide", profileName = "java"))
}

companion object {
private val pointMessage = """
|message Point {
Expand Down
23 changes: 23 additions & 0 deletions wire-library/wire-profiles/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apply plugin: 'java-library'
apply plugin: 'org.jetbrains.kotlin.jvm'

jar {
manifest {
attributes('Automatic-Module-Name': 'wire-profiles')
}
}

dependencies {
api project(':wire-schema')
implementation project(':wire-runtime')
implementation deps.okio.jvm
implementation deps.guava
api deps.javapoet
api deps.kotlinpoet
compileOnly deps.jsr305
testImplementation project(':wire-test-utils')
testImplementation deps.junit
testImplementation deps.kotlin.test.junit
testImplementation deps.assertj
testImplementation deps.jimfs
}
3 changes: 3 additions & 0 deletions wire-library/wire-profiles/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
POM_ARTIFACT_ID=wire-profiles
POM_NAME=Wire
POM_PACKAGING=jar
Loading

0 comments on commit 5fac94f

Please sign in to comment.