Skip to content

Commit

Permalink
Merge pull request #255 from TheMerski/patch-enum-import
Browse files Browse the repository at this point in the history
fix(enum): Use full import path in enum generation
  • Loading branch information
garyp authored Apr 13, 2024
2 parents 0d98b75 + 4ac1d76 commit c275f23
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package pbandk.conformance.pb

@pbandk.Export
public sealed class WireFormat(override val value: Int, override val name: String? = null) : pbandk.Message.Enum {
override fun equals(other: kotlin.Any?): Boolean = other is WireFormat && other.value == value
override fun equals(other: kotlin.Any?): Boolean = other is pbandk.conformance.pb.WireFormat && other.value == value
override fun hashCode(): Int = value.hashCode()
override fun toString(): String = "WireFormat.${name ?: "UNRECOGNIZED"}(value=$value)"

Expand All @@ -15,16 +15,16 @@ public sealed class WireFormat(override val value: Int, override val name: Strin
public object TEXT_FORMAT : WireFormat(4, "TEXT_FORMAT")
public class UNRECOGNIZED(value: Int) : WireFormat(value)

public companion object : pbandk.Message.Enum.Companion<WireFormat> {
public val values: List<WireFormat> by lazy { listOf(UNSPECIFIED, PROTOBUF, JSON, JSPB, TEXT_FORMAT) }
override fun fromValue(value: Int): WireFormat = values.firstOrNull { it.value == value } ?: UNRECOGNIZED(value)
override fun fromName(name: String): WireFormat = values.firstOrNull { it.name == name } ?: throw IllegalArgumentException("No WireFormat with name: $name")
public companion object : pbandk.Message.Enum.Companion<pbandk.conformance.pb.WireFormat> {
public val values: List<pbandk.conformance.pb.WireFormat> by lazy { listOf(UNSPECIFIED, PROTOBUF, JSON, JSPB, TEXT_FORMAT) }
override fun fromValue(value: Int): pbandk.conformance.pb.WireFormat = values.firstOrNull { it.value == value } ?: UNRECOGNIZED(value)
override fun fromName(name: String): pbandk.conformance.pb.WireFormat = values.firstOrNull { it.name == name } ?: throw IllegalArgumentException("No WireFormat with name: $name")
}
}

@pbandk.Export
public sealed class TestCategory(override val value: Int, override val name: String? = null) : pbandk.Message.Enum {
override fun equals(other: kotlin.Any?): Boolean = other is TestCategory && other.value == value
override fun equals(other: kotlin.Any?): Boolean = other is pbandk.conformance.pb.TestCategory && other.value == value
override fun hashCode(): Int = value.hashCode()
override fun toString(): String = "TestCategory.${name ?: "UNRECOGNIZED"}(value=$value)"

Expand All @@ -36,10 +36,10 @@ public sealed class TestCategory(override val value: Int, override val name: Str
public object TEXT_FORMAT_TEST : TestCategory(5, "TEXT_FORMAT_TEST")
public class UNRECOGNIZED(value: Int) : TestCategory(value)

public companion object : pbandk.Message.Enum.Companion<TestCategory> {
public val values: List<TestCategory> by lazy { listOf(UNSPECIFIED_TEST, BINARY_TEST, JSON_TEST, JSON_IGNORE_UNKNOWN_PARSING_TEST, JSPB_TEST, TEXT_FORMAT_TEST) }
override fun fromValue(value: Int): TestCategory = values.firstOrNull { it.value == value } ?: UNRECOGNIZED(value)
override fun fromName(name: String): TestCategory = values.firstOrNull { it.name == name } ?: throw IllegalArgumentException("No TestCategory with name: $name")
public companion object : pbandk.Message.Enum.Companion<pbandk.conformance.pb.TestCategory> {
public val values: List<pbandk.conformance.pb.TestCategory> by lazy { listOf(UNSPECIFIED_TEST, BINARY_TEST, JSON_TEST, JSON_IGNORE_UNKNOWN_PARSING_TEST, JSPB_TEST, TEXT_FORMAT_TEST) }
override fun fromValue(value: Int): pbandk.conformance.pb.TestCategory = values.firstOrNull { it.value == value } ?: UNRECOGNIZED(value)
override fun fromName(name: String): pbandk.conformance.pb.TestCategory = values.firstOrNull { it.name == name } ?: throw IllegalArgumentException("No TestCategory with name: $name")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package pbandk.conformance.pb

@pbandk.Export
public sealed class ForeignEnumProto2(override val value: Int, override val name: String? = null) : pbandk.Message.Enum {
override fun equals(other: kotlin.Any?): Boolean = other is ForeignEnumProto2 && other.value == value
override fun equals(other: kotlin.Any?): Boolean = other is pbandk.conformance.pb.ForeignEnumProto2 && other.value == value
override fun hashCode(): Int = value.hashCode()
override fun toString(): String = "ForeignEnumProto2.${name ?: "UNRECOGNIZED"}(value=$value)"

Expand All @@ -13,10 +13,10 @@ public sealed class ForeignEnumProto2(override val value: Int, override val name
public object FOREIGN_BAZ : ForeignEnumProto2(2, "FOREIGN_BAZ")
public class UNRECOGNIZED(value: Int) : ForeignEnumProto2(value)

public companion object : pbandk.Message.Enum.Companion<ForeignEnumProto2> {
public val values: List<ForeignEnumProto2> by lazy { listOf(FOREIGN_FOO, FOREIGN_BAR, FOREIGN_BAZ) }
override fun fromValue(value: Int): ForeignEnumProto2 = values.firstOrNull { it.value == value } ?: UNRECOGNIZED(value)
override fun fromName(name: String): ForeignEnumProto2 = values.firstOrNull { it.name == name } ?: throw IllegalArgumentException("No ForeignEnumProto2 with name: $name")
public companion object : pbandk.Message.Enum.Companion<pbandk.conformance.pb.ForeignEnumProto2> {
public val values: List<pbandk.conformance.pb.ForeignEnumProto2> by lazy { listOf(FOREIGN_FOO, FOREIGN_BAR, FOREIGN_BAZ) }
override fun fromValue(value: Int): pbandk.conformance.pb.ForeignEnumProto2 = values.firstOrNull { it.value == value } ?: UNRECOGNIZED(value)
override fun fromName(name: String): pbandk.conformance.pb.ForeignEnumProto2 = values.firstOrNull { it.name == name } ?: throw IllegalArgumentException("No ForeignEnumProto2 with name: $name")
}
}

Expand Down Expand Up @@ -1366,7 +1366,7 @@ public data class TestAllTypesProto2(
}

public sealed class NestedEnum(override val value: Int, override val name: String? = null) : pbandk.Message.Enum {
override fun equals(other: kotlin.Any?): Boolean = other is TestAllTypesProto2.NestedEnum && other.value == value
override fun equals(other: kotlin.Any?): Boolean = other is pbandk.conformance.pb.TestAllTypesProto2.NestedEnum && other.value == value
override fun hashCode(): Int = value.hashCode()
override fun toString(): String = "TestAllTypesProto2.NestedEnum.${name ?: "UNRECOGNIZED"}(value=$value)"

Expand All @@ -1376,10 +1376,10 @@ public data class TestAllTypesProto2(
public object NEG : NestedEnum(-1, "NEG")
public class UNRECOGNIZED(value: Int) : NestedEnum(value)

public companion object : pbandk.Message.Enum.Companion<TestAllTypesProto2.NestedEnum> {
public val values: List<TestAllTypesProto2.NestedEnum> by lazy { listOf(FOO, BAR, BAZ, NEG) }
override fun fromValue(value: Int): TestAllTypesProto2.NestedEnum = values.firstOrNull { it.value == value } ?: UNRECOGNIZED(value)
override fun fromName(name: String): TestAllTypesProto2.NestedEnum = values.firstOrNull { it.name == name } ?: throw IllegalArgumentException("No NestedEnum with name: $name")
public companion object : pbandk.Message.Enum.Companion<pbandk.conformance.pb.TestAllTypesProto2.NestedEnum> {
public val values: List<pbandk.conformance.pb.TestAllTypesProto2.NestedEnum> by lazy { listOf(FOO, BAR, BAZ, NEG) }
override fun fromValue(value: Int): pbandk.conformance.pb.TestAllTypesProto2.NestedEnum = values.firstOrNull { it.value == value } ?: UNRECOGNIZED(value)
override fun fromName(name: String): pbandk.conformance.pb.TestAllTypesProto2.NestedEnum = values.firstOrNull { it.name == name } ?: throw IllegalArgumentException("No NestedEnum with name: $name")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package pbandk.conformance.pb

@pbandk.Export
public sealed class ForeignEnum(override val value: Int, override val name: String? = null) : pbandk.Message.Enum {
override fun equals(other: kotlin.Any?): Boolean = other is ForeignEnum && other.value == value
override fun equals(other: kotlin.Any?): Boolean = other is pbandk.conformance.pb.ForeignEnum && other.value == value
override fun hashCode(): Int = value.hashCode()
override fun toString(): String = "ForeignEnum.${name ?: "UNRECOGNIZED"}(value=$value)"

Expand All @@ -13,10 +13,10 @@ public sealed class ForeignEnum(override val value: Int, override val name: Stri
public object FOREIGN_BAZ : ForeignEnum(2, "FOREIGN_BAZ")
public class UNRECOGNIZED(value: Int) : ForeignEnum(value)

public companion object : pbandk.Message.Enum.Companion<ForeignEnum> {
public val values: List<ForeignEnum> by lazy { listOf(FOREIGN_FOO, FOREIGN_BAR, FOREIGN_BAZ) }
override fun fromValue(value: Int): ForeignEnum = values.firstOrNull { it.value == value } ?: UNRECOGNIZED(value)
override fun fromName(name: String): ForeignEnum = values.firstOrNull { it.name == name } ?: throw IllegalArgumentException("No ForeignEnum with name: $name")
public companion object : pbandk.Message.Enum.Companion<pbandk.conformance.pb.ForeignEnum> {
public val values: List<pbandk.conformance.pb.ForeignEnum> by lazy { listOf(FOREIGN_FOO, FOREIGN_BAR, FOREIGN_BAZ) }
override fun fromValue(value: Int): pbandk.conformance.pb.ForeignEnum = values.firstOrNull { it.value == value } ?: UNRECOGNIZED(value)
override fun fromName(name: String): pbandk.conformance.pb.ForeignEnum = values.firstOrNull { it.name == name } ?: throw IllegalArgumentException("No ForeignEnum with name: $name")
}
}

Expand Down Expand Up @@ -1716,7 +1716,7 @@ public data class TestAllTypesProto3(
}

public sealed class NestedEnum(override val value: Int, override val name: String? = null) : pbandk.Message.Enum {
override fun equals(other: kotlin.Any?): Boolean = other is TestAllTypesProto3.NestedEnum && other.value == value
override fun equals(other: kotlin.Any?): Boolean = other is pbandk.conformance.pb.TestAllTypesProto3.NestedEnum && other.value == value
override fun hashCode(): Int = value.hashCode()
override fun toString(): String = "TestAllTypesProto3.NestedEnum.${name ?: "UNRECOGNIZED"}(value=$value)"

Expand All @@ -1726,15 +1726,15 @@ public data class TestAllTypesProto3(
public object NEG : NestedEnum(-1, "NEG")
public class UNRECOGNIZED(value: Int) : NestedEnum(value)

public companion object : pbandk.Message.Enum.Companion<TestAllTypesProto3.NestedEnum> {
public val values: List<TestAllTypesProto3.NestedEnum> by lazy { listOf(FOO, BAR, BAZ, NEG) }
override fun fromValue(value: Int): TestAllTypesProto3.NestedEnum = values.firstOrNull { it.value == value } ?: UNRECOGNIZED(value)
override fun fromName(name: String): TestAllTypesProto3.NestedEnum = values.firstOrNull { it.name == name } ?: throw IllegalArgumentException("No NestedEnum with name: $name")
public companion object : pbandk.Message.Enum.Companion<pbandk.conformance.pb.TestAllTypesProto3.NestedEnum> {
public val values: List<pbandk.conformance.pb.TestAllTypesProto3.NestedEnum> by lazy { listOf(FOO, BAR, BAZ, NEG) }
override fun fromValue(value: Int): pbandk.conformance.pb.TestAllTypesProto3.NestedEnum = values.firstOrNull { it.value == value } ?: UNRECOGNIZED(value)
override fun fromName(name: String): pbandk.conformance.pb.TestAllTypesProto3.NestedEnum = values.firstOrNull { it.name == name } ?: throw IllegalArgumentException("No NestedEnum with name: $name")
}
}

public sealed class AliasedEnum(override val value: Int, override val name: String? = null) : pbandk.Message.Enum {
override fun equals(other: kotlin.Any?): Boolean = other is TestAllTypesProto3.AliasedEnum && other.value == value
override fun equals(other: kotlin.Any?): Boolean = other is pbandk.conformance.pb.TestAllTypesProto3.AliasedEnum && other.value == value
override fun hashCode(): Int = value.hashCode()
override fun toString(): String = "TestAllTypesProto3.AliasedEnum.${name ?: "UNRECOGNIZED"}(value=$value)"

Expand All @@ -1746,10 +1746,10 @@ public data class TestAllTypesProto3(
public object B_AZ : AliasedEnum(2, "bAz")
public class UNRECOGNIZED(value: Int) : AliasedEnum(value)

public companion object : pbandk.Message.Enum.Companion<TestAllTypesProto3.AliasedEnum> {
public val values: List<TestAllTypesProto3.AliasedEnum> by lazy { listOf(ALIAS_FOO, ALIAS_BAR, ALIAS_BAZ, QUX, QUX_, B_AZ) }
override fun fromValue(value: Int): TestAllTypesProto3.AliasedEnum = values.firstOrNull { it.value == value } ?: UNRECOGNIZED(value)
override fun fromName(name: String): TestAllTypesProto3.AliasedEnum = values.firstOrNull { it.name == name } ?: throw IllegalArgumentException("No AliasedEnum with name: $name")
public companion object : pbandk.Message.Enum.Companion<pbandk.conformance.pb.TestAllTypesProto3.AliasedEnum> {
public val values: List<pbandk.conformance.pb.TestAllTypesProto3.AliasedEnum> by lazy { listOf(ALIAS_FOO, ALIAS_BAR, ALIAS_BAZ, QUX, QUX_, B_AZ) }
override fun fromValue(value: Int): pbandk.conformance.pb.TestAllTypesProto3.AliasedEnum = values.firstOrNull { it.value == value } ?: UNRECOGNIZED(value)
override fun fromName(name: String): pbandk.conformance.pb.TestAllTypesProto3.AliasedEnum = values.firstOrNull { it.name == name } ?: throw IllegalArgumentException("No AliasedEnum with name: $name")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ public open class CodeGenerator(
// Enums are sealed classes w/ a value and a name, and a companion object with all values
line("$visibility sealed class ${type.kotlinTypeName}(override val value: Int, override val name: String? = null) : pbandk.Message.Enum {")
.indented {
line("override fun equals(other: kotlin.Any?): Boolean = other is ${type.kotlinFullTypeName} && other.value == value")
line("override fun equals(other: kotlin.Any?): Boolean = other is ${file.kotlinPackageName}.${type.kotlinFullTypeName} && other.value == value")
line("override fun hashCode(): Int = value.hashCode()")
line("override fun toString(): String = \"${type.kotlinFullTypeName}.\${name ?: \"UNRECOGNIZED\"}(value=\$value)\"")
line()
type.values.forEach { line("$visibility object ${it.kotlinValueTypeName} : ${type.kotlinTypeName}(${it.number}, \"${it.name}\")") }
line("$visibility class UNRECOGNIZED(value: Int) : ${type.kotlinTypeName}(value)")
line()
line("$visibility companion object : pbandk.Message.Enum.Companion<${type.kotlinFullTypeName}> {").indented {
line("$visibility val values: List<${type.kotlinFullTypeName}> by lazy { listOf(${type.values.joinToString(", ") { it.kotlinValueTypeName }}) }")
line("override fun fromValue(value: Int): ${type.kotlinFullTypeName} = values.firstOrNull { it.value == value } ?: UNRECOGNIZED(value)")
line("override fun fromName(name: String): ${type.kotlinFullTypeName} = values.firstOrNull { it.name == name } ?: throw IllegalArgumentException(\"No ${type.kotlinTypeName} with name: \$name\")")
line("$visibility companion object : pbandk.Message.Enum.Companion<${file.kotlinPackageName}.${type.kotlinFullTypeName}> {").indented {
line("$visibility val values: List<${file.kotlinPackageName}.${type.kotlinFullTypeName}> by lazy { listOf(${type.values.joinToString(", ") { it.kotlinValueTypeName }}) }")
line("override fun fromValue(value: Int): ${file.kotlinPackageName}.${type.kotlinFullTypeName} = values.firstOrNull { it.value == value } ?: UNRECOGNIZED(value)")
line("override fun fromName(name: String): ${file.kotlinPackageName}.${type.kotlinFullTypeName} = values.firstOrNull { it.name == name } ?: throw IllegalArgumentException(\"No ${type.kotlinTypeName} with name: \$name\")")
}.line("}")
}.line("}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,18 @@ public data class CodeGeneratorResponse(
}

public sealed class Feature(override val value: Int, override val name: String? = null) : pbandk.Message.Enum {
override fun equals(other: kotlin.Any?): Boolean = other is CodeGeneratorResponse.Feature && other.value == value
override fun equals(other: kotlin.Any?): Boolean = other is pbandk.gen.pb.CodeGeneratorResponse.Feature && other.value == value
override fun hashCode(): Int = value.hashCode()
override fun toString(): String = "CodeGeneratorResponse.Feature.${name ?: "UNRECOGNIZED"}(value=$value)"

public object NONE : Feature(0, "FEATURE_NONE")
public object PROTO3_OPTIONAL : Feature(1, "FEATURE_PROTO3_OPTIONAL")
public class UNRECOGNIZED(value: Int) : Feature(value)

public companion object : pbandk.Message.Enum.Companion<CodeGeneratorResponse.Feature> {
public val values: List<CodeGeneratorResponse.Feature> by lazy { listOf(NONE, PROTO3_OPTIONAL) }
override fun fromValue(value: Int): CodeGeneratorResponse.Feature = values.firstOrNull { it.value == value } ?: UNRECOGNIZED(value)
override fun fromName(name: String): CodeGeneratorResponse.Feature = values.firstOrNull { it.name == name } ?: throw IllegalArgumentException("No Feature with name: $name")
public companion object : pbandk.Message.Enum.Companion<pbandk.gen.pb.CodeGeneratorResponse.Feature> {
public val values: List<pbandk.gen.pb.CodeGeneratorResponse.Feature> by lazy { listOf(NONE, PROTO3_OPTIONAL) }
override fun fromValue(value: Int): pbandk.gen.pb.CodeGeneratorResponse.Feature = values.firstOrNull { it.value == value } ?: UNRECOGNIZED(value)
override fun fromName(name: String): pbandk.gen.pb.CodeGeneratorResponse.Feature = values.firstOrNull { it.name == name } ?: throw IllegalArgumentException("No Feature with name: $name")
}
}

Expand Down
14 changes: 14 additions & 0 deletions protoc-gen-pbandk/lib/src/jvmTest/kotlin/CodeGeneratorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ class CodeGeneratorTest {
valueClazz.classLoader.loadClass("foobar.Value\$Value")
}

@Test
fun testOneOf_Enum_SameNameField() {
val result = compileProto("oneof_enum_same_name.proto")

assertEquals(ExitCode.OK, result.exitCode, result.messages)

// Ensure the oneof with the same name as its containing message was generated correctly
val actionClazz = result.classLoader.loadClass("foobar.Action")

// Ensure the message with a same named enum in the message was generated correctly
val enumClazz = actionClazz.classLoader.loadClass("foobar.Action\$ActionV1").kotlin
enumClazz.declaredMemberProperties.single { it.name == "type" }
}

@Test
fun testKotlinBuiltinNames() {
val result = compileProto("kotlin_builtin_names.proto")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";
package foobar;


message Action {
oneof action {
ActionV1 action_v1 = 1;
}
message ActionV1 {
Type type = 1;
enum Type {
TYPE_UNSPECIFIED = 0;
}
}
}
Loading

0 comments on commit c275f23

Please sign in to comment.