Skip to content

Commit

Permalink
Address comments in the PR.
Browse files Browse the repository at this point in the history
* `newBuilder()` throws an `AssertionError` instead. Thanks @oldergod.
* Updated the `KotlinGeneratorTest` to use the renamed option in the `KotlinGenerator`.
* Update goldens to include the new error message.
* Update API files for `wire-compiler`.
  • Loading branch information
tikurahul committed Dec 18, 2024
1 parent 83c75b8 commit 5f09272
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 12 deletions.
8 changes: 4 additions & 4 deletions wire-compiler/api/wire-compiler.api
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ public final class com/squareup/wire/schema/JavaTarget : com/squareup/wire/schem
}

public final class com/squareup/wire/schema/KotlinTarget : com/squareup/wire/schema/Target {
public fun <init> (Ljava/util/List;Ljava/util/List;ZLjava/lang/String;ZZZZLcom/squareup/wire/kotlin/RpcCallStyle;Lcom/squareup/wire/kotlin/RpcRole;ZILjava/lang/String;ZZLcom/squareup/wire/kotlin/EnumMode;Z)V
public synthetic fun <init> (Ljava/util/List;Ljava/util/List;ZLjava/lang/String;ZZZZLcom/squareup/wire/kotlin/RpcCallStyle;Lcom/squareup/wire/kotlin/RpcRole;ZILjava/lang/String;ZZLcom/squareup/wire/kotlin/EnumMode;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Ljava/util/List;Ljava/util/List;ZLjava/lang/String;ZZZZLcom/squareup/wire/kotlin/RpcCallStyle;Lcom/squareup/wire/kotlin/RpcRole;ZILjava/lang/String;ZZLcom/squareup/wire/kotlin/EnumMode;ZZ)V
public synthetic fun <init> (Ljava/util/List;Ljava/util/List;ZLjava/lang/String;ZZZZLcom/squareup/wire/kotlin/RpcCallStyle;Lcom/squareup/wire/kotlin/RpcRole;ZILjava/lang/String;ZZLcom/squareup/wire/kotlin/EnumMode;ZZILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Ljava/util/List;
public final fun component10 ()Lcom/squareup/wire/kotlin/RpcRole;
public final fun component11 ()Z
Expand All @@ -134,8 +134,8 @@ public final class com/squareup/wire/schema/KotlinTarget : com/squareup/wire/sch
public final fun component7 ()Z
public final fun component8 ()Z
public final fun component9 ()Lcom/squareup/wire/kotlin/RpcCallStyle;
public final fun copy (Ljava/util/List;Ljava/util/List;ZLjava/lang/String;ZZZZLcom/squareup/wire/kotlin/RpcCallStyle;Lcom/squareup/wire/kotlin/RpcRole;ZILjava/lang/String;ZZLcom/squareup/wire/kotlin/EnumMode;Z)Lcom/squareup/wire/schema/KotlinTarget;
public static synthetic fun copy$default (Lcom/squareup/wire/schema/KotlinTarget;Ljava/util/List;Ljava/util/List;ZLjava/lang/String;ZZZZLcom/squareup/wire/kotlin/RpcCallStyle;Lcom/squareup/wire/kotlin/RpcRole;ZILjava/lang/String;ZZLcom/squareup/wire/kotlin/EnumMode;ZILjava/lang/Object;)Lcom/squareup/wire/schema/KotlinTarget;
public final fun copy (Ljava/util/List;Ljava/util/List;ZLjava/lang/String;ZZZZLcom/squareup/wire/kotlin/RpcCallStyle;Lcom/squareup/wire/kotlin/RpcRole;ZILjava/lang/String;ZZLcom/squareup/wire/kotlin/EnumMode;ZZ)Lcom/squareup/wire/schema/KotlinTarget;
public static synthetic fun copy$default (Lcom/squareup/wire/schema/KotlinTarget;Ljava/util/List;Ljava/util/List;ZLjava/lang/String;ZZZZLcom/squareup/wire/kotlin/RpcCallStyle;Lcom/squareup/wire/kotlin/RpcRole;ZILjava/lang/String;ZZLcom/squareup/wire/kotlin/EnumMode;ZZILjava/lang/Object;)Lcom/squareup/wire/schema/KotlinTarget;
public fun copyTarget (Ljava/util/List;Ljava/util/List;ZLjava/lang/String;)Lcom/squareup/wire/schema/Target;
public fun equals (Ljava/lang/Object;)Z
public final fun getAndroid ()Z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class MutableHeader(
message = "Shouldn't be used in Kotlin",
level = DeprecationLevel.HIDDEN,
)
override fun newBuilder(): Nothing = throw AssertionError("Builders are deprecated and only available in a javaInterop build; see https://square.github.io/wire/wire_compiler/#kotlin")
override fun newBuilder(): Nothing = throw AssertionError("newBuilder() is unsupported for Mutable message types")

override fun equals(other: Any?): Boolean {
if (other !is MutableHeader) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class MutablePacket(
message = "Shouldn't be used in Kotlin",
level = DeprecationLevel.HIDDEN,
)
override fun newBuilder(): Nothing = throw AssertionError("Builders are deprecated and only available in a javaInterop build; see https://square.github.io/wire/wire_compiler/#kotlin")
override fun newBuilder(): Nothing = throw AssertionError("newBuilder() is unsupported for Mutable message types")

override fun equals(other: Any?): Boolean {
if (other !is MutablePacket) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class MutablePayload(
message = "Shouldn't be used in Kotlin",
level = DeprecationLevel.HIDDEN,
)
override fun newBuilder(): Nothing = throw AssertionError("Builders are deprecated and only available in a javaInterop build; see https://square.github.io/wire/wire_compiler/#kotlin")
override fun newBuilder(): Nothing = throw AssertionError("newBuilder() is unsupported for Mutable message types")

override fun equals(other: Any?): Boolean {
if (other !is MutablePayload) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,12 @@ class KotlinGenerator private constructor(
.addModifiers(OVERRIDE)

if (mutableTypes || !javaInterOp) {
val errorMessage = if (mutableTypes) {
"newBuilder() is unsupported for Mutable message types"
} else {
"Builders are deprecated and only available in a javaInterop build; see https://square.github.io/wire/wire_compiler/#kotlin"
}

return funBuilder
.addAnnotation(
AnnotationSpec.builder(Deprecated::class)
Expand All @@ -652,7 +658,7 @@ class KotlinGenerator private constructor(
.build(),
)
.returns(NOTHING)
.addStatement("throw %T(%S)", ClassName("kotlin", "AssertionError"), "Builders are deprecated and only available in a javaInterop build; see https://square.github.io/wire/wire_compiler/#kotlin")
.addStatement("throw %T(%S)", ClassName("kotlin", "AssertionError"), errorMessage)
.build()
}

Expand Down Expand Up @@ -702,7 +708,7 @@ class KotlinGenerator private constructor(

val body = buildCodeBlock {
if (!mutableTypes) {
// This is true iff the message is not mutable
// We cannot rely on referential equality if the message is mutable.
addStatement("if (%N === this) return·true", otherName)
}
addStatement("if (%N !is %T) return·false", otherName, kotlinType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2327,14 +2327,17 @@ class KotlinGeneratorTest {
}
val code = KotlinWithProfilesGenerator(schema).generateKotlin(
typeName = "Packet",
generateMutableMessages = true,
mutableTypes = true,
)
assertThat(code).contains("class MutablePacket")
assertThat(code).contains("public var header_: MutableHeader? = null")
assertThat(code).contains("public var payload: MutablePayload? = null")
assertThat(code).contains("MutableHeader#ADAPTER") // should refer to adapters of Mutable message types.
assertThat(code).contains("MutablePayload#ADAPTER")
assertThat(code).contains("var result = 0") // hashCode() is no longer calling super.hashCode().
assertThat(code).contains(
"throw AssertionError(\"newBuilder() is unsupported for Mutable message types\")",
)
assertThat(code).contains(
"throw UnsupportedOperationException(\"redact() is unsupported for Mutable message types\")",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal class KotlinWithProfilesGenerator(private val schema: Schema) {
buildersOnly: Boolean = false,
javaInterop: Boolean = false,
enumMode: EnumMode = EnumMode.ENUM_CLASS,
generateMutableMessages: Boolean = false,
mutableTypes: Boolean = false,
): String {
val kotlinGenerator = KotlinGenerator(
schema,
Expand All @@ -63,7 +63,7 @@ internal class KotlinWithProfilesGenerator(private val schema: Schema) {
buildersOnly = buildersOnly,
javaInterop = javaInterop,
enumMode = enumMode,
generateMutableMessages = generateMutableMessages,
mutableTypes = mutableTypes,
)
val type = schema.getType(typeName)!!
val typeSpec = kotlinGenerator.generateType(type)
Expand Down

0 comments on commit 5f09272

Please sign in to comment.