Skip to content

Commit

Permalink
generate unrecognized option to plugin + tests with protoc
Browse files Browse the repository at this point in the history
  • Loading branch information
oldergod committed Apr 4, 2024
1 parent feb5bed commit 9c3c0da
Show file tree
Hide file tree
Showing 16 changed files with 529 additions and 9 deletions.
10 changes: 6 additions & 4 deletions wire-compiler/api/wire-compiler.api
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,16 @@ 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;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;ZZILkotlin/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;ZZZ)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;ZZZILkotlin/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
public final fun component12 ()I
public final fun component13 ()Ljava/lang/String;
public final fun component14 ()Z
public final fun component15 ()Z
public final fun component16 ()Z
public final fun component2 ()Ljava/util/List;
public final fun component3 ()Z
public final fun component4 ()Ljava/lang/String;
Expand All @@ -127,8 +128,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;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;ZZILjava/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;ZZZ)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;ZZZILjava/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 @@ -139,6 +140,7 @@ public final class com/squareup/wire/schema/KotlinTarget : com/squareup/wire/sch
public final fun getEscapeKotlinKeywords ()Z
public fun getExcludes ()Ljava/util/List;
public fun getExclusive ()Z
public final fun getGenerateUnrecognizedEnumConstant ()Z
public fun getIncludes ()Ljava/util/List;
public final fun getJavaInterop ()Z
public final fun getNameSuffix ()Ljava/lang/String;
Expand Down
12 changes: 11 additions & 1 deletion wire-compiler/src/main/java/com/squareup/wire/schema/Target.kt
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,17 @@ data class KotlinTarget(
*/
val buildersOnly: Boolean = false,

/** If true, Kotlin keywords are escaped with backticks. If false, an underscore is added as a suffix. */
/**
* If true, Kotlin keywords are escaped with backticks. If false, an underscore is added as a
* suffix.
*/
val escapeKotlinKeywords: Boolean = false,

/**
* If true, generated enums will have an extra `UNRECOGNIZED` constant with a value of `-1`. This
* only applies to enum which syntax is proto3.
*/
val generateUnrecognizedEnumConstant: Boolean = false,
) : Target() {
override fun newHandler(): SchemaHandler {
return KotlinSchemaHandler(
Expand All @@ -146,6 +155,7 @@ data class KotlinTarget(
nameSuffix = nameSuffix,
buildersOnly = buildersOnly,
escapeKotlinKeywords = escapeKotlinKeywords,
generateUnrecognizedEnumConstant = generateUnrecognizedEnumConstant,
)
}

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 @@ -5,6 +5,13 @@ plugins {
}

wire {
kotlin {
includes = listOf("squareup.wire.unrecognized_constant.*")
out = "src/main/kotlin"
buildersOnly = true
generateUnrecognizedEnumConstant = true
}

kotlin {
includes = listOf(
"squareup.wire.buildersonly.*",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
// Code generated by Wire protocol buffer compiler, do not edit.
// Source: squareup.wire.unrecognized_constant.Easter in squareup/wire/easter_unrecognized_enum.proto
@file:Suppress("DEPRECATION")

package squareup.wire.unrecognized_constant

import com.squareup.wire.FieldEncoding
import com.squareup.wire.Message
import com.squareup.wire.ProtoAdapter
import com.squareup.wire.ProtoReader
import com.squareup.wire.ProtoWriter
import com.squareup.wire.ReverseProtoWriter
import com.squareup.wire.Syntax.PROTO_3
import com.squareup.wire.WireField
import com.squareup.wire.`internal`.JvmField
import com.squareup.wire.`internal`.JvmSynthetic
import com.squareup.wire.`internal`.checkElementsNotNull
import com.squareup.wire.`internal`.immutableCopyOf
import kotlin.Any
import kotlin.Boolean
import kotlin.Int
import kotlin.Long
import kotlin.String
import kotlin.Suppress
import kotlin.Unit
import kotlin.collections.List
import okio.ByteString

/**
* Proto3 message with a proto3 enum.
*/
public class Easter private constructor(
builder: Builder,
unknownFields: ByteString = ByteString.EMPTY,
) : Message<Easter, Easter.Builder>(ADAPTER, unknownFields) {
@field:WireField(
tag = 2,
adapter = "squareup.wire.unrecognized_constant.EasterAnimal#ADAPTER",
jsonName = "optionalEasterAnimal",
schemaIndex = 0,
)
@JvmField
public val optional_easter_animal: EasterAnimal? = builder.optional_easter_animal

@field:WireField(
tag = 3,
adapter = "squareup.wire.unrecognized_constant.EasterAnimal#ADAPTER",
label = WireField.Label.OMIT_IDENTITY,
jsonName = "identityEasterAnimal",
schemaIndex = 1,
)
@JvmField
public val identity_easter_animal: EasterAnimal = builder.identity_easter_animal

@field:WireField(
tag = 4,
adapter = "squareup.wire.unrecognized_constant.EasterAnimal#ADAPTER",
label = WireField.Label.REPEATED,
jsonName = "easterAnimals",
schemaIndex = 2,
)
@JvmField
public val easter_animals: List<EasterAnimal> = immutableCopyOf("easter_animals",
builder.easter_animals)

override fun newBuilder(): Builder {
val builder = Builder()
builder.optional_easter_animal = optional_easter_animal
builder.identity_easter_animal = identity_easter_animal
builder.easter_animals = easter_animals
builder.addUnknownFields(unknownFields)
return builder
}

override fun equals(other: Any?): Boolean {
if (other === this) return true
if (other !is Easter) return false
if (unknownFields != other.unknownFields) return false
if (optional_easter_animal != other.optional_easter_animal) return false
if (identity_easter_animal != other.identity_easter_animal) return false
if (easter_animals != other.easter_animals) return false
return true
}

override fun hashCode(): Int {
var result = super.hashCode
if (result == 0) {
result = unknownFields.hashCode()
result = result * 37 + (optional_easter_animal?.hashCode() ?: 0)
result = result * 37 + identity_easter_animal.hashCode()
result = result * 37 + easter_animals.hashCode()
super.hashCode = result
}
return result
}

override fun toString(): String {
val result = mutableListOf<String>()
if (optional_easter_animal != null) result +=
"""optional_easter_animal=$optional_easter_animal"""
result += """identity_easter_animal=$identity_easter_animal"""
if (easter_animals.isNotEmpty()) result += """easter_animals=$easter_animals"""
return result.joinToString(prefix = "Easter{", separator = ", ", postfix = "}")
}

public class Builder : Message.Builder<Easter, Builder>() {
@JvmField
public var optional_easter_animal: EasterAnimal? = null

@JvmField
public var identity_easter_animal: EasterAnimal = EasterAnimal.EASTER_ANIMAL_DEFAULT

@JvmField
public var easter_animals: List<EasterAnimal> = emptyList()

public fun optional_easter_animal(optional_easter_animal: EasterAnimal?): Builder {
this.optional_easter_animal = optional_easter_animal
return this
}

public fun identity_easter_animal(identity_easter_animal: EasterAnimal): Builder {
this.identity_easter_animal = identity_easter_animal
return this
}

public fun easter_animals(easter_animals: List<EasterAnimal>): Builder {
checkElementsNotNull(easter_animals)
this.easter_animals = easter_animals
return this
}

override fun build(): Easter = Easter(
builder = this,
unknownFields = buildUnknownFields()
)
}

public companion object {
@JvmField
public val ADAPTER: ProtoAdapter<Easter> = object : ProtoAdapter<Easter>(
FieldEncoding.LENGTH_DELIMITED,
Easter::class,
"type.googleapis.com/squareup.wire.unrecognized_constant.Easter",
PROTO_3,
null,
"squareup/wire/easter_unrecognized_enum.proto"
) {
override fun encodedSize(`value`: Easter): Int {
var size = value.unknownFields.size
size += EasterAnimal.ADAPTER.encodedSizeWithTag(2, value.optional_easter_animal)
if (value.identity_easter_animal != EasterAnimal.EASTER_ANIMAL_DEFAULT) size +=
EasterAnimal.ADAPTER.encodedSizeWithTag(3, value.identity_easter_animal)
size += EasterAnimal.ADAPTER.asRepeated().encodedSizeWithTag(4, value.easter_animals)
return size
}

override fun encode(writer: ProtoWriter, `value`: Easter) {
EasterAnimal.ADAPTER.encodeWithTag(writer, 2, value.optional_easter_animal)
if (value.identity_easter_animal != EasterAnimal.EASTER_ANIMAL_DEFAULT)
EasterAnimal.ADAPTER.encodeWithTag(writer, 3, value.identity_easter_animal)
EasterAnimal.ADAPTER.asRepeated().encodeWithTag(writer, 4, value.easter_animals)
writer.writeBytes(value.unknownFields)
}

override fun encode(writer: ReverseProtoWriter, `value`: Easter) {
writer.writeBytes(value.unknownFields)
EasterAnimal.ADAPTER.asRepeated().encodeWithTag(writer, 4, value.easter_animals)
if (value.identity_easter_animal != EasterAnimal.EASTER_ANIMAL_DEFAULT)
EasterAnimal.ADAPTER.encodeWithTag(writer, 3, value.identity_easter_animal)
EasterAnimal.ADAPTER.encodeWithTag(writer, 2, value.optional_easter_animal)
}

override fun decode(reader: ProtoReader): Easter {
val builder = Builder()
val easter_animals = mutableListOf<EasterAnimal>()
val unknownFields = reader.forEachTag { tag ->
when (tag) {
2 -> try {
builder.optional_easter_animal(EasterAnimal.ADAPTER.decode(reader))
} catch (e: ProtoAdapter.EnumConstantNotFoundException) {
builder.optional_easter_animal(EasterAnimal.UNRECOGNIZED)
reader.addUnknownField(tag, FieldEncoding.VARINT, e.value.toLong())
}
3 -> try {
builder.identity_easter_animal(EasterAnimal.ADAPTER.decode(reader))
} catch (e: ProtoAdapter.EnumConstantNotFoundException) {
builder.identity_easter_animal(EasterAnimal.UNRECOGNIZED)
reader.addUnknownField(tag, FieldEncoding.VARINT, e.value.toLong())
}
4 -> try {
EasterAnimal.ADAPTER.tryDecode(reader, easter_animals)
} catch (e: ProtoAdapter.EnumConstantNotFoundException) {
easter_animals.add(EasterAnimal.UNRECOGNIZED)
reader.addUnknownField(tag, FieldEncoding.VARINT, e.value.toLong())
}
else -> reader.readUnknownField(tag)
}
}
return Easter(
builder = builder
.easter_animals(easter_animals),
unknownFields = unknownFields
)
}

override fun redact(`value`: Easter): Easter = Easter(
builder = value.newBuilder(),
unknownFields = ByteString.EMPTY,
)
}

private const val serialVersionUID: Long = 0L

@JvmSynthetic
public inline fun build(body: Builder.() -> Unit): Easter = Builder().apply(body).build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Code generated by Wire protocol buffer compiler, do not edit.
// Source: squareup.wire.unrecognized_constant.EasterAnimal in squareup/wire/easter_unrecognized_enum.proto
@file:Suppress("DEPRECATION")

package squareup.wire.unrecognized_constant

import com.squareup.wire.EnumAdapter
import com.squareup.wire.ProtoAdapter
import com.squareup.wire.Syntax.PROTO_3
import com.squareup.wire.WireEnum
import com.squareup.wire.`internal`.JvmField
import com.squareup.wire.`internal`.JvmStatic
import kotlin.Int
import kotlin.Suppress

public enum class EasterAnimal(
override val `value`: Int,
) : WireEnum {
EASTER_ANIMAL_DEFAULT(0),
BUNNY(1),
HEN(2),
UNRECOGNIZED(-1),
;

public companion object {
@JvmField
public val ADAPTER: ProtoAdapter<EasterAnimal> = object : EnumAdapter<EasterAnimal>(
EasterAnimal::class,
PROTO_3,
EasterAnimal.EASTER_ANIMAL_DEFAULT
) {
override fun fromValue(`value`: Int): EasterAnimal? = EasterAnimal.fromValue(`value`)
}

@JvmStatic
public fun fromValue(`value`: Int): EasterAnimal? = when (`value`) {
0 -> EASTER_ANIMAL_DEFAULT
1 -> BUNNY
2 -> HEN
-1 -> UNRECOGNIZED
else -> null
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2023 Block Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
syntax = "proto3";
package squareup.wire.unrecognized_constant;

// Proto3 message with a proto3 enum.
message Easter {
optional EasterAnimal optional_easter_animal = 2;
EasterAnimal identity_easter_animal = 3;
repeated EasterAnimal easter_animals = 4;
}

enum EasterAnimal {
EASTER_ANIMAL_DEFAULT = 0;
BUNNY = 1;
HEN = 2;
}
2 changes: 2 additions & 0 deletions wire-gradle-plugin/api/wire-gradle-plugin.api
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class com/squareup/wire/gradle/KotlinOutput : com/squareup/wire/gradle/Wi
public final fun getEscapeKotlinKeywords ()Z
public final fun getExcludes ()Ljava/util/List;
public final fun getExclusive ()Z
public final fun getGenerateUnrecognizedEnumConstant ()Z
public final fun getGrpcServerCompatible ()Z
public final fun getIncludes ()Ljava/util/List;
public final fun getJavaInterop ()Z
Expand All @@ -65,6 +66,7 @@ public class com/squareup/wire/gradle/KotlinOutput : com/squareup/wire/gradle/Wi
public final fun setEscapeKotlinKeywords (Z)V
public final fun setExcludes (Ljava/util/List;)V
public final fun setExclusive (Z)V
public final fun setGenerateUnrecognizedEnumConstant (Z)V
public final fun setGrpcServerCompatible (Z)V
public final fun setIncludes (Ljava/util/List;)V
public final fun setJavaInterop (Z)V
Expand Down
Loading

0 comments on commit 9c3c0da

Please sign in to comment.