Skip to content

Commit

Permalink
Fix java conversions for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
thesamet committed Mar 26, 2024
1 parent 39fb4ab commit 2b5efc4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class ProtobufGenerator(
case FieldDescriptor.JavaType.MESSAGE =>
FunctionApplication(field.getMessageType.scalaType.fullName + ".fromJavaProto")
case FieldDescriptor.JavaType.ENUM =>
if (!field.getEnumType().isClosed())
if (!field.legacyEnumFieldTreatedAsClosed())
MethodApplication("intValue") andThen FunctionApplication(
field.getEnumType.scalaType.fullName + ".fromValue"
)
Expand All @@ -242,7 +242,7 @@ class ProtobufGenerator(
def javaFieldToScala(container: String, field: FieldDescriptor): String = {
val javaHazzer = container + ".has" + field.upperJavaName
val upperJavaName =
if (field.isEnum && !field.getEnumType.isClosed()) (field.upperJavaName + "Value")
if (field.isEnum && !field.legacyEnumFieldTreatedAsClosed()) (field.upperJavaName + "Value")
else field.upperJavaName
val javaGetter =
if (field.isRepeated)
Expand All @@ -258,7 +258,9 @@ class ProtobufGenerator(
def unitConversion(n: String, field: FieldDescriptor) =
javaToScalaConversion(field).apply(n, EnclosingType.None)
val upperJavaName =
if (field.mapType.valueField.isEnum && !field.mapType.valueField.getEnumType.isClosed())
if (
field.mapType.valueField.isEnum && !field.mapType.valueField.legacyEnumFieldTreatedAsClosed
)
(field.upperJavaName + "Value")
else field.upperJavaName
ExpressionBuilder.convertCollection(
Expand All @@ -284,7 +286,7 @@ class ProtobufGenerator(
case FieldDescriptor.JavaType.MESSAGE =>
FunctionApplication(field.getMessageType.scalaType.fullName + ".toJavaProto")
case FieldDescriptor.JavaType.ENUM =>
if (!field.getEnumType().isClosed())
if (!field.legacyEnumFieldTreatedAsClosed())
(MethodApplication("value") andThen maybeBox("_root_.scala.Int.box"))
else
FunctionApplication(field.getEnumType.scalaType.fullName + ".toJavaValue")
Expand All @@ -303,8 +305,7 @@ class ProtobufGenerator(
val putAll =
s"putAll${field.upperScalaName}" + (if (
field.mapType.valueField.isEnum && !field.mapType.valueField
.getEnumType()
.isClosed()
.legacyEnumFieldTreatedAsClosed()
)
"Value"
else "")
Expand All @@ -331,8 +332,10 @@ class ProtobufGenerator(
val javaSetter = javaObject +
(if (field.isRepeated) ".addAll"
else
".set") + field.upperJavaName + (if (field.isEnum && !field.getEnumType().isClosed())
"Value"
".set") + field.upperJavaName + (if (
field.isEnum && !field
.legacyEnumFieldTreatedAsClosed()
) "Value"
else "")
val scalaGetter = scalaObject + "." + fieldAccessorSymbol(field)

Expand Down
6 changes: 6 additions & 0 deletions e2e-withjava/src/main/protobuf/enum.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ syntax = "proto2";

package com.thesamet.proto.e2e;

import "enum3.proto";

enum Color {
RED = 1;
GREEN = 2;
Expand Down Expand Up @@ -29,3 +31,7 @@ message EnumTest {
message RequiredEnum {
required Color color = 1;
}

message RequiredEnum3InProto2 {
required Color3 color3 = 2;
}

0 comments on commit 2b5efc4

Please sign in to comment.