Skip to content

Commit 7ee72c4

Browse files
committed
Use enum value type for Rust struct enum field type
1 parent 9b2e1ee commit 7ee72c4

File tree

6 files changed

+121
-6
lines changed

6 files changed

+121
-6
lines changed

internal/src/main/java/online/sharedtype/processor/domain/def/EnumDef.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import lombok.experimental.SuperBuilder;
66
import online.sharedtype.processor.domain.component.EnumValueInfo;
77
import online.sharedtype.processor.domain.type.ConcreteTypeInfo;
8+
import online.sharedtype.processor.domain.type.TypeInfo;
89

910
import java.util.ArrayList;
1011
import java.util.HashSet;
@@ -46,6 +47,14 @@ public List<EnumValueInfo> components() {
4647
return enumValueInfos;
4748
}
4849

50+
/** If the enum is empty, return this enum type. */
51+
public TypeInfo getComponentValueType() {
52+
if (enumValueInfos.isEmpty()) {
53+
return typeInfo;
54+
}
55+
return enumValueInfos.get(0).value().getValueType();
56+
}
57+
4958
@Override
5059
public Set<ConcreteTypeInfo> typeInfoSet() {
5160
Set<ConcreteTypeInfo> typeInfoSet = new HashSet<>(1);

processor/src/main/java/online/sharedtype/processor/writer/converter/RustEnumConverter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ public Tuple<Template, Object> convert(TypeDef typeDef) {
4040

4141
@Nullable
4242
private String getValueTypeExpr(EnumDef enumDef) {
43-
EnumValueInfo component = enumDef.components().get(0);
43+
TypeInfo componentValueType = enumDef.getComponentValueType();
4444
TypeInfo enumTypeInfo = enumDef.typeInfoSet().iterator().next();
45-
if (enumTypeInfo.equals(component.value().getValueType())) {
45+
if (enumTypeInfo.equals(componentValueType)) {
4646
return null;
4747
}
48-
return typeExpressionConverter.toTypeExpr(component.value().getValueType(), enumDef);
48+
return typeExpressionConverter.toTypeExpr(componentValueType, enumDef);
4949
}
5050

5151
private List<EnumerationExpr> extractEnumValues(List<EnumValueInfo> components) {

processor/src/main/java/online/sharedtype/processor/writer/converter/RustStructConverter.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import online.sharedtype.processor.context.Context;
77
import online.sharedtype.processor.domain.component.FieldComponentInfo;
88
import online.sharedtype.processor.domain.def.ClassDef;
9+
import online.sharedtype.processor.domain.def.EnumDef;
910
import online.sharedtype.processor.domain.def.TypeDef;
1011
import online.sharedtype.processor.domain.type.ConcreteTypeInfo;
1112
import online.sharedtype.processor.domain.type.TypeInfo;
@@ -83,11 +84,21 @@ private List<PropertyExpr> gatherProperties(ClassDef classDef) {
8384
private PropertyExpr toPropertyExpr(FieldComponentInfo field, TypeDef contextTypeDef) {
8485
return new PropertyExpr(
8586
ctx.getProps().getRust().isConvertToSnakeCase() ? ConversionUtils.toSnakeCase(field.name()) : field.name(),
86-
typeExpressionConverter.toTypeExpr(field.type(), contextTypeDef),
87+
typeExpressionConverter.toTypeExpr(getFieldValueType(field), contextTypeDef),
8788
ConversionUtils.isOptionalField(field)
8889
);
8990
}
9091

92+
private TypeInfo getFieldValueType(FieldComponentInfo field) {
93+
if (field.type() instanceof ConcreteTypeInfo) {
94+
ConcreteTypeInfo type = (ConcreteTypeInfo) field.type();
95+
if (type.getKind() == ConcreteTypeInfo.Kind.ENUM && type.typeDef() instanceof EnumDef) {
96+
return ((EnumDef) type.typeDef()).getComponentValueType();
97+
}
98+
}
99+
return field.type();
100+
}
101+
91102
@SuppressWarnings("unused")
92103
@RequiredArgsConstructor
93104
static final class StructExpr {

processor/src/main/resources/templates/rust/enum.mustache

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ pub enum {{name}} {
44
{{name}},
55
{{/enumerations}}
66
}
7-
87
{{#hasValue}}
98
impl {{name}} {
109
pub const fn value(self) -> {{{valueType}}} {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package online.sharedtype.processor.domain.def;
2+
3+
import online.sharedtype.processor.domain.Constants;
4+
import online.sharedtype.processor.domain.component.EnumValueInfo;
5+
import online.sharedtype.processor.domain.type.ConcreteTypeInfo;
6+
import online.sharedtype.processor.domain.value.ValueHolder;
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.api.TestInstance;
9+
10+
import java.util.Collections;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
15+
final class EnumDefTest {
16+
private final ConcreteTypeInfo enumTypeInfo = ConcreteTypeInfo.builder().simpleName("EnumA").kind(ConcreteTypeInfo.Kind.ENUM).build();
17+
18+
@Test
19+
void componentValueTypeIsNullForEmptyEnum() {
20+
EnumDef enumDef = EnumDef.builder()
21+
.qualifiedName("com.github.cuzfrog.EnumA").simpleName("EnumA")
22+
.enumValueInfos(Collections.emptyList())
23+
.typeInfo(enumTypeInfo)
24+
.build();
25+
assertThat(enumDef.getComponentValueType()).isEqualTo(enumTypeInfo);
26+
}
27+
28+
@Test
29+
void componentValueType() {
30+
EnumDef enumDef = EnumDef.builder()
31+
.qualifiedName("com.github.cuzfrog.EnumA").simpleName("EnumA")
32+
.enumValueInfos(Collections.singletonList(
33+
new EnumValueInfo("Value1", ValueHolder.ofEnum("Value1", Constants.BOOLEAN_TYPE_INFO, true))
34+
))
35+
.typeInfo(enumTypeInfo)
36+
.build();
37+
assertThat(enumDef.getComponentValueType()).isEqualTo(Constants.BOOLEAN_TYPE_INFO);
38+
}
39+
}

processor/src/test/java/online/sharedtype/processor/writer/converter/RustStructConverterTest.java

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@
33
import online.sharedtype.processor.context.Config;
44
import online.sharedtype.processor.context.ContextMocks;
55
import online.sharedtype.processor.context.TestUtils;
6+
import online.sharedtype.processor.domain.component.EnumValueInfo;
67
import online.sharedtype.processor.domain.def.ClassDef;
78
import online.sharedtype.processor.domain.type.ConcreteTypeInfo;
89
import online.sharedtype.processor.domain.Constants;
910
import online.sharedtype.processor.domain.def.EnumDef;
1011
import online.sharedtype.processor.domain.component.FieldComponentInfo;
1112
import online.sharedtype.processor.domain.type.TypeVariableInfo;
13+
import online.sharedtype.processor.domain.value.ValueHolder;
1214
import online.sharedtype.processor.writer.converter.type.TypeExpressionConverter;
1315
import org.junit.jupiter.api.Test;
1416
import org.junit.jupiter.api.TestInstance;
1517

1618
import java.util.List;
1719
import java.util.Set;
1820

21+
import static online.sharedtype.processor.domain.type.ConcreteTypeInfo.Kind.ENUM;
1922
import static org.assertj.core.api.Assertions.assertThat;
2023
import static org.mockito.Mockito.mock;
2124
import static org.mockito.Mockito.when;
@@ -58,7 +61,61 @@ void shouldAcceptClassDefAnnotated() {
5861
}
5962

6063
@Test
61-
void convert() {
64+
void convertTypeWithEnumField() {
65+
var enumATypeInfo = ConcreteTypeInfo.builder().qualifiedName("com.github.cuzfrog.EnumA").simpleName("EnumA").kind(ENUM).build();
66+
EnumDef enumADef = EnumDef.builder()
67+
.simpleName("EnumA")
68+
.qualifiedName("com.github.cuzfrog.EnumA")
69+
.enumValueInfos(List.of(
70+
new EnumValueInfo("Value1", ValueHolder.ofEnum("Value1", Constants.BOOLEAN_TYPE_INFO, true)),
71+
new EnumValueInfo("Value2", ValueHolder.ofEnum("Value2", Constants.BOOLEAN_TYPE_INFO, false))
72+
))
73+
.build();
74+
enumATypeInfo.markShallowResolved(enumADef);
75+
76+
var enumBTypeInfo = ConcreteTypeInfo.builder().qualifiedName("com.github.cuzfrog.EnumB").simpleName("EnumB").kind(ENUM).build();
77+
EnumDef enumBDef = EnumDef.builder()
78+
.simpleName("EnumB")
79+
.qualifiedName("com.github.cuzfrog.EnumB")
80+
.enumValueInfos(List.of(
81+
new EnumValueInfo("ValueB1", ValueHolder.ofEnum("ValueB1", enumBTypeInfo, "ValueB1"))
82+
))
83+
.build();
84+
enumBTypeInfo.markShallowResolved(enumBDef);
85+
86+
ClassDef classDef = ClassDef.builder()
87+
.simpleName("ClassA")
88+
.qualifiedName("com.github.cuzfrog.ClassA")
89+
.components(List.of(
90+
FieldComponentInfo.builder()
91+
.name("field1")
92+
.type(enumATypeInfo)
93+
.build(),
94+
FieldComponentInfo.builder()
95+
.name("field2")
96+
.type(enumBTypeInfo)
97+
.build()
98+
))
99+
.build();
100+
101+
var data = converter.convert(classDef);
102+
var model = (RustStructConverter.StructExpr)data.b();
103+
104+
assertThat(model.name).isEqualTo("ClassA");
105+
assertThat(model.properties).satisfiesExactly(
106+
v1 -> {
107+
assertThat(v1.name).isEqualTo("field1");
108+
assertThat(v1.type).isEqualTo("bool");
109+
},
110+
v2 -> {
111+
assertThat(v2.name).isEqualTo("field2");
112+
assertThat(v2.type).isEqualTo("EnumB");
113+
}
114+
);
115+
}
116+
117+
@Test
118+
void convertComplexType() {
62119
ConcreteTypeInfo recursiveTypeInfo = ConcreteTypeInfo.builder()
63120
.qualifiedName("com.github.cuzfrog.RecursiveClass")
64121
.simpleName("RecursiveClass")

0 commit comments

Comments
 (0)