Skip to content
Open
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 @@ -22,7 +22,6 @@ import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.MemberName
import com.squareup.kotlinpoet.ParameterSpec
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.UNIT
import com.squareup.kotlinpoet.asClassName
Expand All @@ -44,13 +43,13 @@ object ImplBaseGenerator {
.addType(
TypeSpec.classBuilder("${service.name}ImplBase")
.addModifiers(KModifier.ABSTRACT)
.addSuperinterface(WireBindableService::class)
.apply { addImplBaseConstructor(options) }
.apply { if (options.suspendingCalls) superclass(AbstractCoroutineServerImpl::class) else addSuperinterface(WireBindableService::class) }
.apply { addImplBaseConstructor(this, options) }
.apply { addImplBaseBody(generator, this, service, options) }
.build(),
)

private fun TypeSpec.Builder.addImplBaseConstructor(options: KotlinGrpcGenerator.Companion.Options) {
private fun TypeSpec.Builder.addImplBaseConstructor(builder: TypeSpec.Builder, options: KotlinGrpcGenerator.Companion.Options) {
if (options.suspendingCalls) {
this.primaryConstructor(
FunSpec.constructorBuilder()
Expand All @@ -63,11 +62,8 @@ object ImplBaseGenerator {
).build(),
)
.build(),
).addProperty(
PropertySpec.builder("context", CoroutineContext::class, KModifier.PROTECTED)
.initializer("context")
.build(),
)
builder.addSuperclassConstructorParameter("context")
}
}

Expand Down
6 changes: 3 additions & 3 deletions server-generator/src/test/golden/unitService.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Code generated by Wire protocol buffer compiler, do not edit.
import com.google.protobuf.DescriptorProtos
import com.google.protobuf.Descriptors
import com.squareup.wire.kotlin.grpcserver.WireBindableService
import com.squareup.wire.kotlin.grpcserver.AbstractCoroutineServerImpl
import com.squareup.wire.kotlin.grpcserver.WireMethodMarshaller
import io.grpc.CallOptions
import io.grpc.Channel
Expand Down Expand Up @@ -110,9 +110,9 @@ public object MyServiceWireGrpc {
public fun newStub(channel: Channel): MyServiceStub = MyServiceStub(channel)

public abstract class MyServiceImplBase(
protected val context: CoroutineContext = kotlin.coroutines.EmptyCoroutineContext
context: CoroutineContext = kotlin.coroutines.EmptyCoroutineContext
,
) : WireBindableService {
) : AbstractCoroutineServerImpl(context) {
public open suspend fun doSomething(request: Unit): Unit = throw UnsupportedOperationException()

final override fun bindService(): ServerServiceDefinition =
Expand Down
7 changes: 7 additions & 0 deletions server/api/server.api
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
public abstract class com/squareup/wire/kotlin/grpcserver/AbstractCoroutineServerImpl : com/squareup/wire/kotlin/grpcserver/WireBindableService {
public fun <init> ()V
public fun <init> (Lkotlin/coroutines/CoroutineContext;)V
public synthetic fun <init> (Lkotlin/coroutines/CoroutineContext;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun getContext ()Lkotlin/coroutines/CoroutineContext;
}

public final class com/squareup/wire/kotlin/grpcserver/FlowAdapter {
public static final field INSTANCE Lcom/squareup/wire/kotlin/grpcserver/FlowAdapter;
public final fun bidiStream (Lkotlin/coroutines/CoroutineContext;Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function3;)Lkotlinx/coroutines/flow/Flow;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2025 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
*
* https://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.squareup.wire.kotlin.grpcserver

import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

abstract class AbstractCoroutineServerImpl(
/** The context in which to run server coroutines. */
open val context: CoroutineContext = EmptyCoroutineContext,
) : WireBindableService