Skip to content

Commit

Permalink
Use builderClassName, add test and test protos
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan O'Neill committed Oct 11, 2023
1 parent b4ab452 commit ae795fc
Show file tree
Hide file tree
Showing 13 changed files with 836 additions and 6 deletions.
1 change: 1 addition & 0 deletions gen-tests.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ val generateKotlinBuildersOnlyTests by tasks.creating(JavaExec::class) {
"--kotlin_out=wire-tests/src/commonTest/proto-kotlin",
"--kotlin_builders_only",
"redacted_test_builders_only.proto",
"simple_message_builders_only.proto",
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ class KotlinGenerator private constructor(

addDefaultFields(type, companionBuilder, nameAllocator)
addAdapter(type, companionBuilder)
if (buildersOnly || javaInterOp) addBuildClosure(type, companionBuilder)
if (buildersOnly || javaInterOp) addBuildClosure(type, companionBuilder, builderClassName)

val classBuilder = TypeSpec.classBuilder(className)
.apply {
Expand Down Expand Up @@ -1970,16 +1970,16 @@ class KotlinGenerator private constructor(
}
}

private fun addBuildClosure(type: MessageType, companionBuilder: TypeSpec.Builder) {
private fun addBuildClosure(type: MessageType, companionBuilder: TypeSpec.Builder, builderClassName: ClassName) {
val body = buildCodeBlock {
addStatement("val builder = Builder()")
addStatement("buildFn(builder)")
addStatement("val builder = %T()", builderClassName)
addStatement("body(builder)")
addStatement("return builder.build()")
}
val buildFn = FunSpec.builder("build")
.addParameter("buildFn",
.addParameter("body",
LambdaTypeName.get(
parameters = arrayOf(TypeVariableName("Builder")),
parameters = arrayOf(builderClassName),
returnType = Unit::class.asClassName()
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.squareup.wire

import com.squareup.wire.protos.kotlin.simple.buildersonly.SimpleMessage
import kotlin.test.Test
import kotlin.test.assertEquals

class KotlinBuildTest {
@Test fun kotlinBuildEquivalentToBuilderResult() {
val kotlinBuildResult = SimpleMessage.build {
it.required_int32 = 4
it.optional_int32 = 5
it.optional_nested_msg = SimpleMessage.NestedMessage.build {
it.bb = 100
}
}
val builderResult = SimpleMessage.Builder()
.required_int32(4)
.optional_int32(5)
.optional_nested_msg(SimpleMessage.NestedMessage.Builder()
.bb(100)
.build()
).build()

assertEquals(kotlinBuildResult, builderResult)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import kotlin.Boolean
import kotlin.Int
import kotlin.Long
import kotlin.String
import kotlin.Unit
import okio.ByteString

public class NotRedacted private constructor(
Expand Down Expand Up @@ -148,5 +149,11 @@ public class NotRedacted private constructor(
}

private const val serialVersionUID: Long = 0L

public fun build(body: (Builder) -> Unit): NotRedacted {
val builder = Builder()
body(builder)
return builder.build()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import kotlin.Boolean
import kotlin.Int
import kotlin.Long
import kotlin.String
import kotlin.Unit
import okio.ByteString

public class RedactedChild private constructor(
Expand Down Expand Up @@ -174,5 +175,11 @@ public class RedactedChild private constructor(
}

private const val serialVersionUID: Long = 0L

public fun build(body: (Builder) -> Unit): RedactedChild {
val builder = Builder()
body(builder)
return builder.build()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import kotlin.Boolean
import kotlin.Int
import kotlin.Long
import kotlin.String
import kotlin.Unit
import okio.ByteString

public class RedactedCycleA private constructor(
Expand Down Expand Up @@ -124,5 +125,11 @@ public class RedactedCycleA private constructor(
}

private const val serialVersionUID: Long = 0L

public fun build(body: (Builder) -> Unit): RedactedCycleA {
val builder = Builder()
body(builder)
return builder.build()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import kotlin.Boolean
import kotlin.Int
import kotlin.Long
import kotlin.String
import kotlin.Unit
import okio.ByteString

public class RedactedCycleB private constructor(
Expand Down Expand Up @@ -124,5 +125,11 @@ public class RedactedCycleB private constructor(
}

private const val serialVersionUID: Long = 0L

public fun build(body: (Builder) -> Unit): RedactedCycleB {
val builder = Builder()
body(builder)
return builder.build()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import kotlin.Boolean
import kotlin.Int
import kotlin.Long
import kotlin.String
import kotlin.Unit
import okio.ByteString

public class RedactedExtension private constructor(
Expand Down Expand Up @@ -150,5 +151,11 @@ public class RedactedExtension private constructor(
}

private const val serialVersionUID: Long = 0L

public fun build(body: (Builder) -> Unit): RedactedExtension {
val builder = Builder()
body(builder)
return builder.build()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import kotlin.Boolean
import kotlin.Int
import kotlin.Long
import kotlin.String
import kotlin.Unit
import okio.ByteString

public class RedactedFields private constructor(
Expand Down Expand Up @@ -202,5 +203,11 @@ public class RedactedFields private constructor(
}

private const val serialVersionUID: Long = 0L

public fun build(body: (Builder) -> Unit): RedactedFields {
val builder = Builder()
body(builder)
return builder.build()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import kotlin.Boolean
import kotlin.Int
import kotlin.Long
import kotlin.String
import kotlin.Unit
import kotlin.collections.List
import okio.ByteString

Expand Down Expand Up @@ -168,5 +169,11 @@ public class RedactedRepeated private constructor(
}

private const val serialVersionUID: Long = 0L

public fun build(body: (Builder) -> Unit): RedactedRepeated {
val builder = Builder()
body(builder)
return builder.build()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import kotlin.Boolean
import kotlin.Int
import kotlin.Long
import kotlin.String
import kotlin.Unit
import kotlin.UnsupportedOperationException
import okio.ByteString

Expand Down Expand Up @@ -124,5 +125,11 @@ public class RedactedRequired private constructor(
}

private const val serialVersionUID: Long = 0L

public fun build(body: (Builder) -> Unit): RedactedRequired {
val builder = Builder()
body(builder)
return builder.build()
}
}
}
Loading

0 comments on commit ae795fc

Please sign in to comment.