Skip to content

Commit 2fa5b69

Browse files
committed
Introduce opaque_types for the WireCompiler
1 parent 96bf704 commit 2fa5b69

8 files changed

Lines changed: 1087 additions & 5 deletions

File tree

gen-tests.gradle.kts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,26 @@ val generateAndroidCompactTests by tasks.creating(JavaExec::class) {
213213
)
214214
}
215215

216+
// OPAQUE TYPES
217+
218+
val generateOpaqueTypeTests by tasks.creating(JavaExec::class) {
219+
group = "Generate Tests"
220+
description = "Generates Java, Kotlin, and Swift code to demonstrate opaque types"
221+
classpath = wire
222+
mainClass.set("com.squareup.wire.WireCompiler")
223+
args = listOf(
224+
"--proto_path=wire-tests/src/commonTest/proto/kotlin",
225+
"--java_out=wire-tests/src/jvmJavaTest/proto-java",
226+
"--no_java_exclusive",
227+
"--kotlin_out=wire-tests/src/commonTest/proto-kotlin",
228+
"--no_kotlin_exclusive",
229+
"--swift_out=wire-tests-swift/opaques",
230+
"--no_swift_exclusive",
231+
"--opaque_types=squareup.protos.opaque_types.OuterOpaqueType.InnerOpaqueType1",
232+
"opaque_types.proto"
233+
)
234+
}
235+
216236
// KOTLIN
217237

218238
val generateKotlinTests by tasks.creating(JavaExec::class) {
@@ -493,11 +513,9 @@ val generateSwiftProto2Tests by tasks.creating(JavaExec::class) {
493513
"custom_options.proto",
494514
"deprecated.proto",
495515
"deprecated_enum.proto",
496-
// 'edge_cases.proto',
497516
"external_message.proto",
498517
"foreign.proto",
499518
"form.proto",
500-
// 'keyword.proto',
501519
"map.proto",
502520
"negative_value_enum.proto",
503521
"no_fields.proto",
@@ -508,14 +526,11 @@ val generateSwiftProto2Tests by tasks.creating(JavaExec::class) {
508526
"percents_in_kdoc.proto",
509527
"person.proto",
510528
"redacted_one_of.proto",
511-
// 'redacted_test.proto',
512529
"recursive_map.proto",
513530
"same_name_enum.proto",
514531
"swift_edge_cases.proto",
515-
// 'simple_message.proto',
516532
"to_string.proto",
517533
"unknown_fields.proto"
518-
// 'uses_any.proto',
519534
)
520535
}
521536

@@ -714,6 +729,7 @@ val generateTests by tasks.creating {
714729
generateProto3JavaTests,
715730
generateProtoReader32KotlinTests,
716731
generateSharedJson,
732+
generateOpaqueTypeTests,
717733
)
718734
}
719735

wire-compiler/api/wire-compiler.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public final class com/squareup/wire/DryRunFileSystem : okio/ForwardingFileSyste
88
public final class com/squareup/wire/WireCompiler {
99
public static final field CODE_GENERATED_BY_WIRE Ljava/lang/String;
1010
public static final field Companion Lcom/squareup/wire/WireCompiler$Companion;
11+
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
1112
public final fun compile ()V
1213
public static final fun forArgs (Ljava/nio/file/FileSystem;Lcom/squareup/wire/WireLogger;[Ljava/lang/String;)Lcom/squareup/wire/WireCompiler;
1314
public static final fun forArgs (Lokio/FileSystem;Lcom/squareup/wire/WireLogger;[Ljava/lang/String;)Lcom/squareup/wire/WireCompiler;
@@ -37,6 +38,7 @@ public final class com/squareup/wire/WireCompiler {
3738
public final fun getKotlinSingleMethodServices ()Z
3839
public final fun getLog ()Lcom/squareup/wire/WireLogger;
3940
public final fun getModules ()Ljava/util/Map;
41+
public final fun getOpaqueTypes ()Ljava/util/List;
4042
public final fun getPermitPackageCycles ()Z
4143
public final fun getProtoPaths ()Ljava/util/List;
4244
public final fun getSchemaHandlerFactoryClass ()Ljava/lang/String;

wire-compiler/src/main/java/com/squareup/wire/WireCompiler.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ import okio.openZip
9898
*
9999
* The `--android-annotations` flag will add the `Nullable` annotation to optional fields.
100100
*
101+
* The `--opaque_types` takes a list of qualified named Protobuf types separated by a ','. All types
102+
* in the list will be evaluated as being of type `bytes`. On code generation, the fields of such
103+
* types will be using the platform equivalent of `bytes`, like [okio.ByteString] for the JVM. Note
104+
* that scalar types cannot be opaqued. The opaque step will happen before the tree shaking one.
105+
*
101106
* The `--compact` flag will emit code that uses reflection for reading, writing, and
102107
* toString methods which are normally implemented with code generation.
103108
*
@@ -138,6 +143,7 @@ class WireCompiler internal constructor(
138143
val emitProtoReader32: Boolean,
139144
val eventListenerFactoryClasses: List<String>,
140145
val customOptions: Map<String, String>,
146+
val opaqueTypes: List<String> = listOf(),
141147
) {
142148

143149
@Throws(IOException::class)
@@ -212,6 +218,7 @@ class WireCompiler internal constructor(
212218
modules = modules,
213219
permitPackageCycles = permitPackageCycles,
214220
eventListeners = eventListenerFactoryClasses.map { newEventListenerFactory(it).create() },
221+
opaqueTypes = opaqueTypes,
215222
)
216223

217224
wireRun.execute(fs, log)
@@ -279,6 +286,7 @@ class WireCompiler internal constructor(
279286
private const val KOTLIN_ESCAPE_KEYWORDS = "--kotlin_escape_keywords"
280287
private const val EMIT_PROTO_READER_32 = "--emit_proto_reader_32"
281288
private const val CUSTOM_OPTION_FLAG = "--custom_option="
289+
private const val OPAQUE_TYPES_FLAG = "--opaque_types="
282290

283291
@Throws(IOException::class)
284292
@JvmStatic
@@ -343,6 +351,7 @@ class WireCompiler internal constructor(
343351
var emitProtoReader32 = false
344352
var dryRun = false
345353
val customOptions = mutableMapOf<String, String>()
354+
val opaqueTypes = mutableListOf<String>()
346355

347356
for (arg in args) {
348357
when {
@@ -403,6 +412,14 @@ class WireCompiler internal constructor(
403412
customOptions[key] = value
404413
}
405414

415+
arg.startsWith(OPAQUE_TYPES_FLAG) -> {
416+
opaqueTypes.addAll(
417+
arg
418+
.substring(OPAQUE_TYPES_FLAG.length)
419+
.split(','),
420+
)
421+
}
422+
406423
arg.startsWith(FILES_FLAG) -> {
407424
val files = arg.substring(FILES_FLAG.length).toPath()
408425
try {
@@ -503,6 +520,7 @@ class WireCompiler internal constructor(
503520
emitProtoReader32 = emitProtoReader32,
504521
eventListenerFactoryClasses = eventListenerFactoryClasses,
505522
customOptions = customOptions,
523+
opaqueTypes = opaqueTypes,
506524
)
507525
}
508526
}

wire-compiler/src/test/java/com/squareup/wire/WireCompilerTest.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,17 @@ class WireCompilerTest {
345345
assertJavaOutputs(outputs)
346346
}
347347

348+
@Test
349+
fun testOpaqueTypes() {
350+
val sources = arrayOf("opaque_types.proto")
351+
compileToKotlin(sources, "--opaque_types=squareup.protos.opaque_types.OuterOpaqueType.InnerOpaqueType1")
352+
353+
val outputs = arrayOf(
354+
"squareup/protos/opaque_types/OuterOpaqueType.kt",
355+
)
356+
assertKotlinOutputs(outputs)
357+
}
358+
348359
@Test
349360
fun testCustomOptionsNoOptions() {
350361
val sources = arrayOf("custom_options.proto", "option_redacted.proto")

0 commit comments

Comments
 (0)