Skip to content

Commit bcbc06e

Browse files
authored
62 rust enum values (#125)
* Impl Rust enum values * Referenced enum value now have actual end valueType
1 parent b6106fe commit bcbc06e

42 files changed

Lines changed: 466 additions & 209 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

annotation/src/main/java/online/sharedtype/SharedType.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@
6565
*
6666
* <p>
6767
* <b>Enums:</b><br>
68-
* Enums are emitted as according to target schema:
68+
* Enums are emitted as below:
6969
* <ul>
70-
* <li>Typescript: type union or enum. By default enum values are strings.</li>
71-
* <li>Rust: plain enum (Enum values of different types are not supported yet.)</li>
70+
* <li>Typescript: type union or enum. For simple enums, values are enum constants' names.</li>
71+
* <li>Rust: plain enum for simple enums; impl a const fun {@code value()} for custom enum values.</li>
7272
* </ul>
7373
* See {@link EnumValue} for how to mark an enum value.
7474
*

client-test/rust/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,15 @@ mod tests {
128128
r#"{"valueOptional":"foo","nestedValueOptional":null,"setNestedValueOptional":null,"mapNestedValueOptional":{"1":"foo"}}"#
129129
);
130130
}
131+
132+
#[test]
133+
fn enum_values() {
134+
assert_eq!(EnumTShirt::S.value(), "S");
135+
assert_eq!(EnumSize::LARGE.value(), 3);
136+
assert_eq!(EnumConstReference::ReferenceConstantInOther.value(), 999);
137+
assert_eq!(EnumConstReference::ReferenceConstantLocally.value(), 156);
138+
assert_eq!(EnumEnumReference::ReferenceAnother.value(), 3);
139+
assert_eq!(EnumSimpleEnumReference::ReferenceAnother.value(), EnumGalaxy::Andromeda);
140+
assert_eq!(EnumEnumEnumReference::ReferenceAnother2.value(), EnumGalaxy::Andromeda);
141+
}
131142
}

internal/src/main/java/online/sharedtype/processor/domain/component/ConstantField.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,19 @@
1616
public final class ConstantField implements ComponentInfo {
1717
private static final long serialVersionUID = -155863067131290289L;
1818
private final String name;
19-
private final TypeInfo type;
2019
private final ValueHolder value;
2120

2221
public String name() {
2322
return name;
2423
}
2524

26-
public TypeInfo type() {
27-
return type;
28-
}
29-
3025
public ValueHolder value() {
3126
return value;
3227
}
3328

3429
@Override
3530
public boolean resolved() {
36-
return type.resolved();
31+
return value.getValueType().resolved();
3732
}
3833

3934
@Override

internal/src/main/java/online/sharedtype/processor/domain/component/EnumValueInfo.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,19 @@
2121
public final class EnumValueInfo implements ComponentInfo {
2222
private static final long serialVersionUID = 1117324458104635595L;
2323
private final String name;
24-
private final TypeInfo type;
2524
private final EnumConstantValue value;
2625

2726
public String name() {
2827
return name;
2928
}
3029

31-
public TypeInfo type() {
32-
return type;
33-
}
34-
3530
public EnumConstantValue value() {
3631
return value;
3732
}
3833

3934
@Override
4035
public boolean resolved() {
41-
return type.resolved();
36+
return value.getValueType().resolved();
4237
}
4338

4439
@Override
Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
11
package online.sharedtype.processor.domain.value;
22

3-
import lombok.AccessLevel;
43
import lombok.EqualsAndHashCode;
54
import lombok.Getter;
6-
import lombok.RequiredArgsConstructor;
5+
import online.sharedtype.processor.domain.type.ConcreteTypeInfo;
76
import online.sharedtype.processor.domain.type.TypeInfo;
87

9-
@EqualsAndHashCode
10-
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
11-
public final class EnumConstantValue implements ValueHolder {
8+
@Getter
9+
@EqualsAndHashCode(callSuper = true)
10+
public final class EnumConstantValue extends LiteralValue {
1211
private static final long serialVersionUID = -6711930218877737970L;
13-
@Getter
1412
private final String enumConstantName;
15-
@Getter
16-
private final TypeInfo valueType;
17-
private final Object value;
18-
19-
@Override
20-
public Object getValue() {
21-
if (value instanceof ValueHolder) {
22-
return ((ValueHolder) value).getValue();
23-
}
24-
return value;
13+
EnumConstantValue(String enumConstantName, ConcreteTypeInfo valueType, Object value) {
14+
super(valueType, value);
15+
this.enumConstantName = enumConstantName;
2516
}
2617

2718
@Override
2819
public String toString() {
29-
return String.format("%s(%s)", enumConstantName, value);
20+
return String.format("%s(%s)", enumConstantName, getValue());
3021
}
3122
}

internal/src/main/java/online/sharedtype/processor/domain/value/LiteralValue.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,21 @@
22

33
import lombok.AccessLevel;
44
import lombok.EqualsAndHashCode;
5+
import lombok.Getter;
56
import lombok.RequiredArgsConstructor;
7+
import online.sharedtype.processor.domain.type.ConcreteTypeInfo;
8+
import online.sharedtype.processor.domain.type.TypeInfo;
69

710
import java.util.Objects;
811

12+
@Getter
913
@EqualsAndHashCode
1014
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
11-
public final class LiteralValue implements ValueHolder {
15+
public class LiteralValue implements ValueHolder {
1216
private static final long serialVersionUID = -7324230239169028973L;
17+
private final ConcreteTypeInfo valueType;
1318
private final Object value;
1419

15-
@Override
16-
public Object getValue() {
17-
Object v = value;
18-
while (v instanceof ValueHolder) {
19-
v = ((ValueHolder) v).getValue();
20-
}
21-
return v;
22-
}
23-
2420
@Override
2521
public String toString() {
2622
return Objects.toString(value);
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package online.sharedtype.processor.domain.value;
22

3-
import online.sharedtype.processor.domain.Constants;
3+
import online.sharedtype.processor.domain.type.ConcreteTypeInfo;
44
import online.sharedtype.processor.domain.type.TypeInfo;
55

66
import java.io.Serializable;
77

88
public interface ValueHolder extends Serializable {
9+
ConcreteTypeInfo getValueType();
910
Object getValue();
11+
1012
default String literalValue() {
1113
Object value = getValue();
1214
if (value instanceof CharSequence || value instanceof Character) {
@@ -16,21 +18,24 @@ default String literalValue() {
1618
}
1719
}
1820

19-
static ValueHolder of(Object value) {
21+
static ValueHolder of(ConcreteTypeInfo valueType, Object value) {
2022
if (value instanceof ValueHolder) {
2123
return (ValueHolder) value;
2224
} else {
23-
return new LiteralValue(value);
25+
return new LiteralValue(valueType, value);
2426
}
2527
}
2628

27-
static EnumConstantValue ofEnum(String enumConstantName, TypeInfo valueType, Object value) {
28-
return new EnumConstantValue(enumConstantName, valueType, of(value));
29-
}
30-
31-
static EnumConstantValue ofEnum(String enumConstantName) {
32-
return new EnumConstantValue(enumConstantName, Constants.STRING_TYPE_INFO, enumConstantName);
29+
static EnumConstantValue ofEnum(String enumConstantName, ConcreteTypeInfo valueType, Object value) {
30+
ConcreteTypeInfo actualValueType = valueType;
31+
Object actualValue = value;
32+
while (actualValue instanceof ValueHolder) {
33+
ValueHolder valueHolder = (ValueHolder) actualValue;
34+
actualValueType = valueHolder.getValueType();
35+
actualValue = valueHolder.getValue();
36+
}
37+
return new EnumConstantValue(enumConstantName, actualValueType, actualValue);
3338
}
3439

35-
LiteralValue NULL = new LiteralValue(null);
40+
LiteralValue NULL = new LiteralValue(null,null);
3641
}

it/java17/src/test/java/online/sharedtype/it/ConstantsIntegrationTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package online.sharedtype.it;
22

33
import online.sharedtype.processor.domain.def.ConstantNamespaceDef;
4+
import online.sharedtype.processor.domain.value.EnumConstantValue;
45
import org.junit.jupiter.api.Test;
56

67
import static online.sharedtype.it.support.TypeDefDeserializer.deserializeTypeDef;
@@ -60,7 +61,8 @@ void parseMyConstants() {
6061
},
6162
component -> {
6263
assertThat(component.name()).isEqualTo("REFERENCED_ENUM_VALUE");
63-
assertThat(component.value().getValue()).isEqualTo("MilkyWay");
64+
EnumConstantValue value = (EnumConstantValue) component.value();
65+
assertThat(value.getEnumConstantName()).isEqualTo("MilkyWay");
6466
},
6567
component -> {
6668
assertThat(component.name()).isEqualTo("REFERENCED_ENUM_VALUE2");
@@ -116,7 +118,8 @@ void parseMyConstants() {
116118
},
117119
component -> {
118120
assertThat(component.name()).isEqualTo("REFERENCED_ENUM_VALUE_IN_STATIC_BLOCK");
119-
assertThat(component.value().getValue()).isEqualTo("MilkyWay");
121+
EnumConstantValue value = (EnumConstantValue) component.value();
122+
assertThat(value.getEnumConstantName()).isEqualTo("MilkyWay");
120123
},
121124
component -> {
122125
assertThat(component.name()).isEqualTo("REFERENCED_ENUM_VALUE2_IN_STATIC_BLOCK");

it/java17/src/test/java/online/sharedtype/it/EnumIntegrationTest.java

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void parseEnumSize() {
3030
assertThat(enumSize.simpleName()).isEqualTo("EnumSize");
3131
assertThat(enumSize.qualifiedName()).isEqualTo("online.sharedtype.it.java8.EnumSize");
3232
assertThat(enumSize.components()).hasSize(3).allMatch(constant -> {
33-
ConcreteTypeInfo typeInfo = (ConcreteTypeInfo)constant.type();
33+
ConcreteTypeInfo typeInfo = (ConcreteTypeInfo)constant.value().getValueType();
3434
return typeInfo.qualifiedName().equals("int");
3535
});
3636

@@ -51,20 +51,20 @@ void enumGalaxy() {
5151
assertThat(enumGalaxy.simpleName()).isEqualTo("EnumGalaxy");
5252
assertThat(enumGalaxy.qualifiedName()).isEqualTo("online.sharedtype.it.java8.EnumGalaxy");
5353
assertThat(enumGalaxy.components()).hasSize(3).allMatch(constant -> {
54-
ConcreteTypeInfo typeInfo = (ConcreteTypeInfo)constant.type();
54+
ConcreteTypeInfo typeInfo = (ConcreteTypeInfo)constant.value().getValueType();
5555
return typeInfo.qualifiedName().equals("online.sharedtype.it.java8.EnumGalaxy");
5656
});
5757
EnumValueInfo constant1 = enumGalaxy.components().get(0);
58-
assertThat(constant1.value().getValue()).isEqualTo("MilkyWay");
5958
assertThat(constant1.value().getEnumConstantName()).isEqualTo("MilkyWay");
59+
assertThat(constant1.value().getValue()).isEqualTo("MilkyWay");
6060
var constant1EnumType = (ConcreteTypeInfo)constant1.value().getValueType();
6161
assertThat(constant1EnumType.qualifiedName()).isEqualTo("online.sharedtype.it.java8.EnumGalaxy");
6262

6363
EnumValueInfo constant2 = enumGalaxy.components().get(1);
64-
assertThat(constant2.value().getValue()).isEqualTo("Andromeda");
64+
assertThat(constant2.value().getEnumConstantName()).isEqualTo("Andromeda");
6565

6666
EnumValueInfo constant3 = enumGalaxy.components().get(2);
67-
assertThat(constant3.value().getValue()).isEqualTo("Triangulum");
67+
assertThat(constant3.value().getEnumConstantName()).isEqualTo("Triangulum");
6868
}
6969

7070
@Test
@@ -78,14 +78,14 @@ void parseEnumConstReference() {
7878
assertThat(constant1.value().getValue()).isEqualTo(999L);
7979
assertThat(constant1.value().getEnumConstantName()).isEqualTo("ReferenceConstantInOther");
8080
var constant1TypeInfo = (ConcreteTypeInfo)constant1.value().getValueType();
81-
assertThat(constant1.type()).isEqualTo(constant1TypeInfo);
81+
assertThat(constant1.value().getValueType()).isEqualTo(constant1TypeInfo);
8282
assertThat(constant1TypeInfo.qualifiedName()).isEqualTo("long");
8383

8484
EnumValueInfo constant2 = enumDef.components().get(1);
8585
assertThat(constant2.value().getValue()).isEqualTo(156L);
8686
assertThat(constant2.value().getEnumConstantName()).isEqualTo("ReferenceConstantLocally");
8787
var constant2TypeInfo = (ConcreteTypeInfo)constant2.value().getValueType();
88-
assertThat(constant2.type()).isEqualTo(constant2TypeInfo);
88+
assertThat(constant2.value().getValueType()).isEqualTo(constant2TypeInfo);
8989
assertThat(constant2TypeInfo.qualifiedName()).isEqualTo("long");
9090
}
9191

@@ -100,7 +100,34 @@ void parseEnumEnumReference() {
100100
assertThat(constant1.value().getValue()).isEqualTo(3);
101101
assertThat(constant1.value().getEnumConstantName()).isEqualTo("ReferenceAnother");
102102
var constant1TypeInfo = (ConcreteTypeInfo)constant1.value().getValueType();
103-
assertThat(constant1.type()).isEqualTo(constant1TypeInfo);
104-
assertThat(constant1TypeInfo.qualifiedName()).isEqualTo("online.sharedtype.it.java8.EnumSize"); // TODO: should be int? EnumSize's value type is effectively int
103+
assertThat(constant1TypeInfo.qualifiedName()).isEqualTo("int");
104+
}
105+
106+
@Test
107+
void parseEnumSimpleEnumReference() {
108+
EnumDef enumDef = (EnumDef) deserializeTypeDef("online.sharedtype.it.java8.EnumSimpleEnumReference.ser");
109+
assertThat(enumDef.simpleName()).isEqualTo("EnumSimpleEnumReference");
110+
assertThat(enumDef.qualifiedName()).isEqualTo("online.sharedtype.it.java8.EnumSimpleEnumReference");
111+
assertThat(enumDef.components()).hasSize(1);
112+
113+
EnumValueInfo constant1 = enumDef.components().get(0);
114+
assertThat(constant1.value().getValue()).isEqualTo("Andromeda");
115+
assertThat(constant1.value().getEnumConstantName()).isEqualTo("ReferenceAnother");
116+
var constant1TypeInfo = (ConcreteTypeInfo)constant1.value().getValueType();
117+
assertThat(constant1TypeInfo.qualifiedName()).isEqualTo("online.sharedtype.it.java8.EnumGalaxy");
118+
}
119+
120+
@Test
121+
void parseEnumEnumEnumReference() {
122+
EnumDef enumDef = (EnumDef) deserializeTypeDef("online.sharedtype.it.java8.EnumEnumEnumReference.ser");
123+
assertThat(enumDef.simpleName()).isEqualTo("EnumEnumEnumReference");
124+
assertThat(enumDef.qualifiedName()).isEqualTo("online.sharedtype.it.java8.EnumEnumEnumReference");
125+
assertThat(enumDef.components()).hasSize(1);
126+
127+
EnumValueInfo constant1 = enumDef.components().get(0);
128+
assertThat(constant1.value().getValue()).isEqualTo("Andromeda");
129+
assertThat(constant1.value().getEnumConstantName()).isEqualTo("ReferenceAnother2");
130+
var constant1TypeInfo = (ConcreteTypeInfo)constant1.value().getValueType();
131+
assertThat(constant1TypeInfo.qualifiedName()).isEqualTo("online.sharedtype.it.java8.EnumGalaxy");
105132
}
106133
}

it/java8/src/main/java/online/sharedtype/it/java8/EnumGalaxy.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import lombok.RequiredArgsConstructor;
44
import online.sharedtype.SharedType;
55

6+
@SharedType(
7+
rustMacroTraits = {"PartialEq", "Eq", "Hash", "serde::Serialize", "serde::Deserialize"}
8+
)
69
public enum EnumGalaxy {
710
MilkyWay, Andromeda, Triangulum;
811
}
@@ -26,3 +29,21 @@ enum EnumEnumReference {
2629
@SharedType.EnumValue
2730
private final EnumSize enumValue;
2831
}
32+
33+
@RequiredArgsConstructor
34+
@SharedType
35+
enum EnumSimpleEnumReference {
36+
ReferenceAnother(EnumGalaxy.Andromeda),
37+
;
38+
@SharedType.EnumValue
39+
private final EnumGalaxy enumValue;
40+
}
41+
42+
@RequiredArgsConstructor
43+
@SharedType
44+
enum EnumEnumEnumReference {
45+
ReferenceAnother2(EnumSimpleEnumReference.ReferenceAnother),
46+
;
47+
@SharedType.EnumValue
48+
private final EnumSimpleEnumReference enumValue;
49+
}

0 commit comments

Comments
 (0)