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
22 changes: 20 additions & 2 deletions docs/wire_compiler.md
Original file line number Diff line number Diff line change
Expand Up @@ -489,12 +489,12 @@ wire {
// `suspending` to generate coroutines APIs that require a Kotlin
// coroutines context.
// `blocking` to generate blocking APIs callable by Java and Kotlin.
rpcCallStyle = 'blocking'
rpcCallStyle = 'suspending'

// `client` to generate interfaces best suited to sending outbound calls.
// `server` to generate interfaces best suited to receiving inbound calls.
// `none` to not generate services.
rpcRole = 'server'
rpcRole = 'client'

// If set, the value will be appended to generated service type names. If
// null, their rpcRole will be used as a suffix instead.
Expand All @@ -515,9 +515,27 @@ wire {
// Defines how an protobuf enum type is to be generated. See `com.squareup.wire.kotlin.EnumMode`
enumMode = "enum_class"

// `flat` (default) to generate each oneof field as a separate nullable property on the
// message class. `boxed` to generate all oneof fields as boxed types. `sealed_class` to
// generate a nested sealed class with a data class subtype per field.
// See `com.squareup.wire.kotlin.OneofMode`.
oneofMode = "flat"

// True to emit a adapters that include a decode() function that accepts a `ProtoReader32`.
// Use this optimization when targeting Kotlin/JS, where `Long` cursors are inefficient.
emitProtoReader32 = false

// True for the generated classes to be mutable.
mutableTypes = false

// True for the generated gRPC client to use explicit classes for client, server,
// and bidirectional streaming calls.
explicitStreamingCalls = false

// False to skip making immutable copies of repeated and map fields when constructing a
// Message. Only set to false when you can guarantee these fields won't be mutated after
// construction, to avoid the copy overhead.
makeImmutableCopies = true
}
}
```
Expand Down
13 changes: 8 additions & 5 deletions wire-compiler/api/wire-compiler.api
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +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;ZLjava/util/Map;ZZZZZZZZIZZZLcom/squareup/wire/kotlin/RpcCallStyle;Lcom/squareup/wire/kotlin/RpcRole;ZLjava/lang/String;ZZZZLcom/squareup/wire/kotlin/EnumMode;Ljava/util/List;Ljava/util/Map;Ljava/util/List;IILkotlin/jvm/internal/DefaultConstructorMarker;)V
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;ZLjava/util/Map;ZZZZZZZZIZZZLcom/squareup/wire/kotlin/RpcCallStyle;Lcom/squareup/wire/kotlin/RpcRole;ZLjava/lang/String;ZZZZLcom/squareup/wire/kotlin/EnumMode;Lcom/squareup/wire/kotlin/OneofMode;Ljava/util/List;Ljava/util/Map;Ljava/util/List;IILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun compile ()V
public final fun createRun ()Lcom/squareup/wire/schema/WireRun;
public static final fun forArgs (Ljava/nio/file/FileSystem;Lcom/squareup/wire/WireLogger;[Ljava/lang/String;)Lcom/squareup/wire/WireCompiler;
Expand All @@ -35,6 +35,7 @@ public final class com/squareup/wire/WireCompiler {
public final fun getKotlinExclusive ()Z
public final fun getKotlinExplicitStreamingCalls ()Z
public final fun getKotlinNameSuffix ()Ljava/lang/String;
public final fun getKotlinOneofMode ()Lcom/squareup/wire/kotlin/OneofMode;
public final fun getKotlinOut ()Ljava/lang/String;
public final fun getKotlinRpcCallStyle ()Lcom/squareup/wire/kotlin/RpcCallStyle;
public final fun getKotlinRpcRole ()Lcom/squareup/wire/kotlin/RpcRole;
Expand Down Expand Up @@ -121,8 +122,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;ZZZZ)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;ZZZZILkotlin/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;Lcom/squareup/wire/kotlin/OneofMode;ZZZZ)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;Lcom/squareup/wire/kotlin/OneofMode;ZZZZILkotlin/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 @@ -131,6 +132,7 @@ public final class com/squareup/wire/schema/KotlinTarget : com/squareup/wire/sch
public final fun component14 ()Z
public final fun component15 ()Z
public final fun component16 ()Lcom/squareup/wire/kotlin/EnumMode;
public final fun component17 ()Lcom/squareup/wire/kotlin/OneofMode;
public final fun component2 ()Ljava/util/List;
public final fun component3 ()Z
public final fun component4 ()Ljava/lang/String;
Expand All @@ -139,8 +141,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;ZZZZ)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;ZZZZILjava/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;Lcom/squareup/wire/kotlin/OneofMode;ZZZZ)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;Lcom/squareup/wire/kotlin/OneofMode;ZZZZILjava/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 All @@ -155,6 +157,7 @@ public final class com/squareup/wire/schema/KotlinTarget : com/squareup/wire/sch
public fun getIncludes ()Ljava/util/List;
public final fun getJavaInterop ()Z
public final fun getNameSuffix ()Ljava/lang/String;
public final fun getOneofMode ()Lcom/squareup/wire/kotlin/OneofMode;
public fun getOutDirectory ()Ljava/lang/String;
public final fun getRpcCallStyle ()Lcom/squareup/wire/kotlin/RpcCallStyle;
public final fun getRpcRole ()Lcom/squareup/wire/kotlin/RpcRole;
Expand Down
15 changes: 15 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 @@ -16,6 +16,7 @@
package com.squareup.wire

import com.squareup.wire.kotlin.EnumMode
import com.squareup.wire.kotlin.OneofMode
import com.squareup.wire.kotlin.RpcCallStyle
import com.squareup.wire.kotlin.RpcRole
import com.squareup.wire.schema.CustomTarget
Expand Down Expand Up @@ -50,6 +51,7 @@ import okio.openZip
* [--java_out=<path>]
* [--kotlin_out=<path>]
* [--kotlin_enum_mode=<enum_mode>]
* [--kotlin_oneof_mode=<oneof_mode>]
* [--swift_out=<path>]
* [--custom_out=<path>]
* [--schema_handler_factory_class=<class_name>]
Expand All @@ -71,6 +73,10 @@ import okio.openZip
* (default) to generate Kotlin enum classes, or `sealed_class` to generate sealed classes with
* data objects for each value and an Unrecognized case.
*
* `--kotlin_oneof_mode` controls how oneofs are generated in Kotlin. Valid values are `flat`
* (default) to generate each oneof field as a separate nullable property, `boxed` to generate
* boxed oneofs, or `sealed_class` to generate a nested sealed class with a data class per field.
*
* `--swift_out` should provide the folder where the files generated by the Swift code generator
* should be placed.
*
Expand Down Expand Up @@ -151,6 +157,7 @@ class WireCompiler internal constructor(
val emitProtoReader32: Boolean,
val kotlinExplicitStreamingCalls: Boolean,
val kotlinEnumMode: EnumMode,
val kotlinOneofMode: OneofMode,
val eventListenerFactoryClasses: List<String>,
val customOptions: Map<String, String>,
val opaqueTypes: List<String> = listOf(),
Expand Down Expand Up @@ -194,6 +201,7 @@ class WireCompiler internal constructor(
emitProtoReader32 = emitProtoReader32,
explicitStreamingCalls = kotlinExplicitStreamingCalls,
enumMode = kotlinEnumMode,
oneofMode = kotlinOneofMode,
)
}
if (swiftOut != null) {
Expand Down Expand Up @@ -305,6 +313,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 KOTLIN_ENUM_MODE = "--kotlin_enum_mode="
private const val KOTLIN_ONEOF_MODE = "--kotlin_oneof_mode="
private const val CUSTOM_OPTION_FLAG = "--custom_option="
private const val OPAQUE_TYPES_FLAG = "--opaque_types="
private const val IGNORE_UNUSED_ROOTS_AND_PRUNES = "--ignore_unused_roots_and_prunes"
Expand Down Expand Up @@ -371,6 +380,7 @@ class WireCompiler internal constructor(
var kotlinEscapeKeywords = false
var emitProtoReader32 = false
var kotlinEnumMode = EnumMode.ENUM_CLASS
var kotlinOneofMode = OneofMode.FLAT
var kotlinExplicitStreamingCalls = false
var dryRun = false
val customOptions = mutableMapOf<String, String>()
Expand Down Expand Up @@ -413,6 +423,10 @@ class WireCompiler internal constructor(
kotlinEnumMode = EnumMode.valueOf(arg.substring(KOTLIN_ENUM_MODE.length).uppercase())
}

arg.startsWith(KOTLIN_ONEOF_MODE) -> {
kotlinOneofMode = OneofMode.valueOf(arg.substring(KOTLIN_ONEOF_MODE.length).uppercase())
}

arg.startsWith(SWIFT_OUT_FLAG) -> {
swiftOut = arg.substring(SWIFT_OUT_FLAG.length)
}
Expand Down Expand Up @@ -551,6 +565,7 @@ class WireCompiler internal constructor(
kotlinEscapeKeywords = kotlinEscapeKeywords,
emitProtoReader32 = emitProtoReader32,
kotlinEnumMode = kotlinEnumMode,
kotlinOneofMode = kotlinOneofMode,
eventListenerFactoryClasses = eventListenerFactoryClasses,
customOptions = customOptions,
opaqueTypes = opaqueTypes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.squareup.wire.schema
import com.squareup.wire.java.JavaSchemaHandler
import com.squareup.wire.kotlin.EnumMode
import com.squareup.wire.kotlin.KotlinSchemaHandler
import com.squareup.wire.kotlin.OneofMode
import com.squareup.wire.kotlin.RpcCallStyle
import com.squareup.wire.kotlin.RpcRole
import com.squareup.wire.swift.SwiftSchemaHandler
Expand Down Expand Up @@ -133,6 +134,9 @@ data class KotlinTarget(
/** enum_class or sealed_class. See [EnumMode][com.squareup.wire.kotlin.EnumMode]. */
val enumMode: EnumMode = EnumMode.ENUM_CLASS,

/** flat, boxed, or sealed_class. See [OneofMode][com.squareup.wire.kotlin.OneofMode]. */
val oneofMode: OneofMode = OneofMode.FLAT,

/**
* If true, adapters will generate decode functions for `ProtoReader32`. Use this optimization
* when targeting Kotlin/JS, where `Long` cursors are inefficient.
Expand Down Expand Up @@ -174,6 +178,7 @@ data class KotlinTarget(
buildersOnly = buildersOnly,
escapeKotlinKeywords = escapeKotlinKeywords,
enumMode = enumMode,
oneofMode = oneofMode,
emitProtoReader32 = emitProtoReader32,
mutableTypes = mutableTypes,
explicitStreamingCalls = explicitStreamingCalls,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import assertk.assertions.isNull
import assertk.assertions.isTrue
import assertk.assertions.prop
import com.squareup.wire.kotlin.EnumMode
import com.squareup.wire.kotlin.OneofMode
import com.squareup.wire.kotlin.RpcCallStyle
import com.squareup.wire.kotlin.RpcRole
import com.squareup.wire.schema.CustomTarget
Expand Down Expand Up @@ -158,6 +159,7 @@ class WireCompilerTest {
"--kotlin_escape_keywords",
"--emit_proto_reader_32",
"--kotlin_enum_mode=sealed_class",
"--kotlin_oneof_mode=sealed_class",
// "--custom_option=a,1",
// "--custom_option=b,2",
"--opaque_types=opaque_types",
Expand Down Expand Up @@ -205,6 +207,7 @@ class WireCompilerTest {
buildersOnly = true,
escapeKotlinKeywords = true,
enumMode = EnumMode.SEALED_CLASS,
oneofMode = OneofMode.SEALED_CLASS,
emitProtoReader32 = true,
mutableTypes = false,
explicitStreamingCalls = true,
Expand Down Expand Up @@ -450,6 +453,7 @@ class WireCompilerTest {
"--kotlin_escape_keywords",
"--emit_proto_reader_32",
"--kotlin_enum_mode=sealed_class",
"--kotlin_oneof_mode=sealed_class",
"--custom_option=a,1",
"--custom_option=b,2",
"--kotlin_explicit_streaming_calls",
Expand Down Expand Up @@ -488,6 +492,7 @@ class WireCompilerTest {
buildersOnly = true,
escapeKotlinKeywords = true,
enumMode = EnumMode.SEALED_CLASS,
oneofMode = OneofMode.SEALED_CLASS,
emitProtoReader32 = true,
mutableTypes = false,
explicitStreamingCalls = true,
Expand Down
7 changes: 7 additions & 0 deletions wire-golden-files/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ wire {
boxOneOfsMinSize = 1
}

kotlin {
includes = listOf("squareup.wire.sealedoneof.*")
out = "src/main/kotlin"
oneofMode = "sealed_class"
buildersOnly = true
}

opaque("squareup.protos.opaque_types.OuterOpaqueType.InnerOpaqueType1")
kotlin {
includes = listOf("squareup.protos.opaque_types.*")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Code generated by Wire protocol buffer compiler, do not edit.
// Source: squareup.proto3.options.field_option in squareup/wire/options.proto
@file:Suppress(
"DEPRECATION",
"RUNTIME_ANNOTATION_NOT_SUPPORTED",
)

package squareup.proto3.options

import kotlin.Int
import kotlin.Suppress
import kotlin.`annotation`.AnnotationRetention
import kotlin.`annotation`.AnnotationTarget
import kotlin.`annotation`.Retention
import kotlin.`annotation`.Target

@Retention(AnnotationRetention.RUNTIME)
@Target(
AnnotationTarget.CLASS,
AnnotationTarget.PROPERTY,
AnnotationTarget.FIELD,
)
public annotation class FieldOptionOption(
public val `value`: Int,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Code generated by Wire protocol buffer compiler, do not edit.
// Source: squareup.proto3.options.oneof_option in squareup/wire/options.proto
@file:Suppress(
"DEPRECATION",
"RUNTIME_ANNOTATION_NOT_SUPPORTED",
)

package squareup.proto3.options

import kotlin.Int
import kotlin.Suppress
import kotlin.`annotation`.AnnotationRetention
import kotlin.`annotation`.AnnotationTarget
import kotlin.`annotation`.Retention
import kotlin.`annotation`.Target

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.CLASS)
public annotation class OneofOptionOption(
public val `value`: Int,
)
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import kotlin.jvm.JvmField
import kotlin.jvm.JvmStatic
import kotlin.jvm.JvmSynthetic
import okio.ByteString
import squareup.proto3.options.FieldOptionOption

public class BoxedOneOfs(
@JvmField
Expand Down Expand Up @@ -149,6 +150,7 @@ public class BoxedOneOfs(
public val VALUE_SECOND_VALUE: Value<String> =
Value<String>(tag = 2, adapter = ProtoAdapter.STRING, declaredName = "second_value")

@FieldOptionOption(806)
public val VALUE_VALUE: Value<String> =
Value<String>(tag = 3, adapter = ProtoAdapter.STRING, declaredName = "value")

Expand Down
Loading
Loading