Skip to content

Commit

Permalink
Merge pull request #3147 from square/bquenaudon.2024-10-15.opaqueType…
Browse files Browse the repository at this point in the history
…sWireCompiler

Introduce opaque_types for the WireCompiler
  • Loading branch information
oldergod authored Oct 16, 2024
2 parents 5caf911 + 2fa5b69 commit 16b7962
Show file tree
Hide file tree
Showing 8 changed files with 1,087 additions and 5 deletions.
26 changes: 21 additions & 5 deletions gen-tests.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,26 @@ val generateAndroidCompactTests by tasks.creating(JavaExec::class) {
)
}

// OPAQUE TYPES

val generateOpaqueTypeTests by tasks.creating(JavaExec::class) {
group = "Generate Tests"
description = "Generates Java, Kotlin, and Swift code to demonstrate opaque types"
classpath = wire
mainClass.set("com.squareup.wire.WireCompiler")
args = listOf(
"--proto_path=wire-tests/src/commonTest/proto/kotlin",
"--java_out=wire-tests/src/jvmJavaTest/proto-java",
"--no_java_exclusive",
"--kotlin_out=wire-tests/src/commonTest/proto-kotlin",
"--no_kotlin_exclusive",
"--swift_out=wire-tests-swift/opaques",
"--no_swift_exclusive",
"--opaque_types=squareup.protos.opaque_types.OuterOpaqueType.InnerOpaqueType1",
"opaque_types.proto"
)
}

// KOTLIN

val generateKotlinTests by tasks.creating(JavaExec::class) {
Expand Down Expand Up @@ -493,11 +513,9 @@ val generateSwiftProto2Tests by tasks.creating(JavaExec::class) {
"custom_options.proto",
"deprecated.proto",
"deprecated_enum.proto",
// 'edge_cases.proto',
"external_message.proto",
"foreign.proto",
"form.proto",
// 'keyword.proto',
"map.proto",
"negative_value_enum.proto",
"no_fields.proto",
Expand All @@ -508,14 +526,11 @@ val generateSwiftProto2Tests by tasks.creating(JavaExec::class) {
"percents_in_kdoc.proto",
"person.proto",
"redacted_one_of.proto",
// 'redacted_test.proto',
"recursive_map.proto",
"same_name_enum.proto",
"swift_edge_cases.proto",
// 'simple_message.proto',
"to_string.proto",
"unknown_fields.proto"
// 'uses_any.proto',
)
}

Expand Down Expand Up @@ -714,6 +729,7 @@ val generateTests by tasks.creating {
generateProto3JavaTests,
generateProtoReader32KotlinTests,
generateSharedJson,
generateOpaqueTypeTests,
)
}

Expand Down
2 changes: 2 additions & 0 deletions wire-compiler/api/wire-compiler.api
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public final class com/squareup/wire/DryRunFileSystem : okio/ForwardingFileSyste
public final class com/squareup/wire/WireCompiler {
public static final field CODE_GENERATED_BY_WIRE Ljava/lang/String;
public static final field Companion Lcom/squareup/wire/WireCompiler$Companion;
public synthetic fun <init> (Lokio/FileSystem;Lcom/squareup/wire/WireLogger;Ljava/util/List;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/Map;ZZZZZZZIZZZLcom/squareup/wire/kotlin/RpcCallStyle;Lcom/squareup/wire/kotlin/RpcRole;ZLjava/lang/String;ZZZLjava/util/List;Ljava/util/Map;Ljava/util/List;IILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun compile ()V
public static final fun forArgs (Ljava/nio/file/FileSystem;Lcom/squareup/wire/WireLogger;[Ljava/lang/String;)Lcom/squareup/wire/WireCompiler;
public static final fun forArgs (Lokio/FileSystem;Lcom/squareup/wire/WireLogger;[Ljava/lang/String;)Lcom/squareup/wire/WireCompiler;
Expand Down Expand Up @@ -37,6 +38,7 @@ public final class com/squareup/wire/WireCompiler {
public final fun getKotlinSingleMethodServices ()Z
public final fun getLog ()Lcom/squareup/wire/WireLogger;
public final fun getModules ()Ljava/util/Map;
public final fun getOpaqueTypes ()Ljava/util/List;
public final fun getPermitPackageCycles ()Z
public final fun getProtoPaths ()Ljava/util/List;
public final fun getSchemaHandlerFactoryClass ()Ljava/lang/String;
Expand Down
18 changes: 18 additions & 0 deletions wire-compiler/src/main/java/com/squareup/wire/WireCompiler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ import okio.openZip
*
* The `--android-annotations` flag will add the `Nullable` annotation to optional fields.
*
* The `--opaque_types` takes a list of qualified named Protobuf types separated by a ','. All types
* in the list will be evaluated as being of type `bytes`. On code generation, the fields of such
* types will be using the platform equivalent of `bytes`, like [okio.ByteString] for the JVM. Note
* that scalar types cannot be opaqued. The opaque step will happen before the tree shaking one.
*
* The `--compact` flag will emit code that uses reflection for reading, writing, and
* toString methods which are normally implemented with code generation.
*
Expand Down Expand Up @@ -138,6 +143,7 @@ class WireCompiler internal constructor(
val emitProtoReader32: Boolean,
val eventListenerFactoryClasses: List<String>,
val customOptions: Map<String, String>,
val opaqueTypes: List<String> = listOf(),
) {

@Throws(IOException::class)
Expand Down Expand Up @@ -212,6 +218,7 @@ class WireCompiler internal constructor(
modules = modules,
permitPackageCycles = permitPackageCycles,
eventListeners = eventListenerFactoryClasses.map { newEventListenerFactory(it).create() },
opaqueTypes = opaqueTypes,
)

wireRun.execute(fs, log)
Expand Down Expand Up @@ -279,6 +286,7 @@ class WireCompiler internal constructor(
private const val KOTLIN_ESCAPE_KEYWORDS = "--kotlin_escape_keywords"
private const val EMIT_PROTO_READER_32 = "--emit_proto_reader_32"
private const val CUSTOM_OPTION_FLAG = "--custom_option="
private const val OPAQUE_TYPES_FLAG = "--opaque_types="

@Throws(IOException::class)
@JvmStatic
Expand Down Expand Up @@ -343,6 +351,7 @@ class WireCompiler internal constructor(
var emitProtoReader32 = false
var dryRun = false
val customOptions = mutableMapOf<String, String>()
val opaqueTypes = mutableListOf<String>()

for (arg in args) {
when {
Expand Down Expand Up @@ -403,6 +412,14 @@ class WireCompiler internal constructor(
customOptions[key] = value
}

arg.startsWith(OPAQUE_TYPES_FLAG) -> {
opaqueTypes.addAll(
arg
.substring(OPAQUE_TYPES_FLAG.length)
.split(','),
)
}

arg.startsWith(FILES_FLAG) -> {
val files = arg.substring(FILES_FLAG.length).toPath()
try {
Expand Down Expand Up @@ -503,6 +520,7 @@ class WireCompiler internal constructor(
emitProtoReader32 = emitProtoReader32,
eventListenerFactoryClasses = eventListenerFactoryClasses,
customOptions = customOptions,
opaqueTypes = opaqueTypes,
)
}
}
Expand Down
11 changes: 11 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 @@ -345,6 +345,17 @@ class WireCompilerTest {
assertJavaOutputs(outputs)
}

@Test
fun testOpaqueTypes() {
val sources = arrayOf("opaque_types.proto")
compileToKotlin(sources, "--opaque_types=squareup.protos.opaque_types.OuterOpaqueType.InnerOpaqueType1")

val outputs = arrayOf(
"squareup/protos/opaque_types/OuterOpaqueType.kt",
)
assertKotlinOutputs(outputs)
}

@Test
fun testCustomOptionsNoOptions() {
val sources = arrayOf("custom_options.proto", "option_redacted.proto")
Expand Down
Loading

0 comments on commit 16b7962

Please sign in to comment.