Skip to content

Commit ce6f3c8

Browse files
authored
75 Add option to use static as rust const keyword (#146)
1 parent b52dcce commit ce6f3c8

19 files changed

Lines changed: 168 additions & 81 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@
225225
*/
226226
String rustTargetDatetimeTypeLiteral() default "";
227227

228+
/**
229+
* @return either "const" or "static".
230+
*/
231+
String rustConstKeyword() default "const";
232+
228233
/**
229234
* How to render optional fields in Typescript.
230235
*

client-test/rust/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ mod tests {
124124
assert_eq!(LONG_VALUE, 999);
125125
assert_eq!(MATH_VALUE, "1.1");
126126
assert_eq!(MATH_VALUE_QUALIFIED_NAME, "88885555");
127+
assert_eq!(ANOTHER_INLINE_LONG_VALUE, 112);
127128

128129
assert_eq!(MyEnumConstants::INT_VALUE, 1);
129130
assert_eq!(MyEnumConstants::STR_VALUE, "abc");

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,16 @@ static final class InnerConstantClass {
7575
}
7676
}
7777

78-
@SharedType(includes = SharedType.ComponentType.CONSTANTS)
78+
@SharedType(includes = SharedType.ComponentType.CONSTANTS, rustConstKeyword = "static")
7979
enum MyEnumConstants {
8080
;
8181
@SharedType.TagLiteral(tags = "// test comments for constant in enum type")
8282
@SharedType.TagLiteral(tags = "// test inline comments", position = SharedType.TagPosition.INLINE_AFTER)
8383
private static final int INT_VALUE = 1;
8484
private static final String STR_VALUE = "abc";
8585
}
86+
87+
@SharedType(constantNamespaced = SharedType.OptionalBool.FALSE, includes = SharedType.ComponentType.CONSTANTS, rustConstKeyword = "static")
88+
class AnotherInlineConstants {
89+
static final long ANOTHER_INLINE_LONG_VALUE = 112L;
90+
}

processor/src/main/java/online/sharedtype/processor/context/Config.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public final class Config {
3535
private final String typescriptTargetDatetimeTypeLiteral;
3636
private final String goTargetDatetimeTypeLiteral;
3737
private final String rustTargetDatetimeTypeLiteral;
38+
private final String rustConstKeyword;
3839

3940
@Retention(RetentionPolicy.RUNTIME)
4041
@interface AnnoContainer {
@@ -74,6 +75,7 @@ public Config(TypeElement typeElement, Context ctx) {
7475
ctx.getProps().getRust().getTargetDatetimeTypeLiteral(),
7576
() -> String.format("Loading rustTargetDatetimeTypeLiteral failed. Please check your configuration for '%s'", qualifiedName)
7677
);
78+
rustConstKeyword = validateRustConstKeyword(anno.rustConstKeyword());
7779
}
7880

7981
public boolean includes(SharedType.ComponentType componentType) {
@@ -143,4 +145,12 @@ private Props.Go.EnumFormat parseGoEnumFormat(SharedType anno, Context ctx) {
143145
}
144146
return ctx.getProps().getGo().getEnumFormat();
145147
}
148+
149+
private static String validateRustConstKeyword(String rustConstKeyword) {
150+
if (!rustConstKeyword.equals("const") && !rustConstKeyword.equals("static")) {
151+
throw new SharedTypeException(String.format(
152+
"Invalid value for SharedType.rustConstKeyword: '%s', only 'const' or 'static' is allowed.", rustConstKeyword));
153+
}
154+
return rustConstKeyword;
155+
}
146156
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package online.sharedtype.processor.writer.converter;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import online.sharedtype.processor.domain.def.ConstantNamespaceDef;
5+
import online.sharedtype.processor.domain.def.TypeDef;
6+
7+
import java.util.List;
8+
9+
@RequiredArgsConstructor
10+
abstract class AbstractConstantConverter implements TemplateDataConverter {
11+
@Override
12+
public final boolean shouldAccept(TypeDef typeDef) {
13+
return typeDef instanceof ConstantNamespaceDef;
14+
}
15+
16+
@RequiredArgsConstructor
17+
static final class ConstantNamespaceExpr<A extends AbstractFieldExpr> extends AbstractTypeExpr {
18+
final String name;
19+
final List<A> constants;
20+
}
21+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package online.sharedtype.processor.writer.converter;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import online.sharedtype.SharedType;
5+
import online.sharedtype.processor.context.Config;
6+
import online.sharedtype.processor.context.Context;
7+
import online.sharedtype.processor.domain.component.ComponentInfo;
8+
import online.sharedtype.processor.domain.component.ConstantField;
9+
import online.sharedtype.processor.domain.def.ConstantNamespaceDef;
10+
import online.sharedtype.processor.domain.def.TypeDef;
11+
import online.sharedtype.processor.support.utils.Tuple;
12+
import online.sharedtype.processor.writer.converter.type.TypeExpressionConverter;
13+
import online.sharedtype.processor.writer.render.Template;
14+
15+
import java.util.List;
16+
import java.util.stream.Collectors;
17+
18+
@RequiredArgsConstructor
19+
final class DefaultConstantConverter extends AbstractConstantConverter {
20+
private final Context ctx;
21+
private final TypeExpressionConverter typeExpressionConverter;
22+
private final SharedType.TargetType targetType;
23+
24+
@Override
25+
public Tuple<Template, AbstractTypeExpr> convert(TypeDef typeDef) {
26+
ConstantNamespaceDef constantNamespaceDef = (ConstantNamespaceDef) typeDef;
27+
ConstantNamespaceExpr<ConstantExpr> value = new ConstantNamespaceExpr<>(
28+
constantNamespaceDef.simpleName(),
29+
constantNamespaceDef.components().stream().map(field -> toConstantExpr(field, typeDef)).collect(Collectors.toList())
30+
);
31+
32+
Config config = ctx.getTypeStore().getConfig(typeDef);
33+
return Tuple.of(Template.forConstant(targetType, config.isConstantNamespaced()), value);
34+
}
35+
36+
private ConstantExpr toConstantExpr(ConstantField constantField, TypeDef contextTypeDef) {
37+
return new ConstantExpr(
38+
constantField,
39+
typeExpressionConverter.toTypeExpr(constantField.value().getValueType(), contextTypeDef),
40+
constantField.value().literalValue()
41+
);
42+
}
43+
44+
final class ConstantExpr extends AbstractFieldExpr {
45+
final String type;
46+
final String value;
47+
ConstantExpr(ComponentInfo componentInfo, String type, String value) {
48+
super(componentInfo, targetType);
49+
this.type = type;
50+
this.value = value;
51+
}
52+
}
53+
}

processor/src/main/java/online/sharedtype/processor/writer/converter/ConstantConverter.java renamed to processor/src/main/java/online/sharedtype/processor/writer/converter/RustConstantConverter.java

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,38 @@
1616
import online.sharedtype.processor.writer.converter.type.TypeExpressionConverter;
1717
import online.sharedtype.processor.writer.render.Template;
1818

19-
import online.sharedtype.processor.support.annotation.Nullable;
2019
import java.util.List;
2120
import java.util.stream.Collectors;
2221

2322
@RequiredArgsConstructor
24-
final class ConstantConverter implements TemplateDataConverter {
23+
final class RustConstantConverter extends AbstractConstantConverter {
2524
private final Context ctx;
2625
private final TypeExpressionConverter typeExpressionConverter;
27-
private final SharedType.TargetType targetType;
28-
29-
@Override
30-
public boolean shouldAccept(TypeDef typeDef) {
31-
return typeDef instanceof ConstantNamespaceDef;
32-
}
3326

3427
@Override
3528
public Tuple<Template, AbstractTypeExpr> convert(TypeDef typeDef) {
29+
Config config = ctx.getTypeStore().getConfig(typeDef);
30+
3631
ConstantNamespaceDef constantNamespaceDef = (ConstantNamespaceDef) typeDef;
37-
ConstantNamespaceExpr value = new ConstantNamespaceExpr(
32+
ConstantNamespaceExpr<ConstantExpr> value = new ConstantNamespaceExpr<>(
3833
constantNamespaceDef.simpleName(),
39-
constantNamespaceDef.components().stream().map(field -> toConstantExpr(field, typeDef)).collect(Collectors.toList())
34+
constantNamespaceDef.components().stream().map(field -> toConstantExpr(field, typeDef, config.getRustConstKeyword())).collect(Collectors.toList())
4035
);
4136

42-
Config config = ctx.getTypeStore().getConfig(typeDef);
43-
return Tuple.of(Template.forConstant(targetType, config.isConstantNamespaced()), value);
37+
return Tuple.of(Template.forConstant(SharedType.TargetType.RUST, config.isConstantNamespaced()), value);
4438
}
4539

46-
private ConstantExpr toConstantExpr(ConstantField constantField, TypeDef contextTypeDef) {
40+
private ConstantExpr toConstantExpr(ConstantField constantField, TypeDef contextTypeDef, String keyword) {
4741
return new ConstantExpr(
4842
constantField,
43+
keyword,
4944
toConstantTypeExpr(constantField, contextTypeDef),
5045
toConstantValue(constantField)
5146
);
5247
}
5348

5449
private String toConstantTypeExpr(ConstantField constantField, TypeDef contextTypeDef) {
55-
if (targetType == SharedType.TargetType.RUST && constantField.value() instanceof EnumConstantValue) {
50+
if (constantField.value() instanceof EnumConstantValue) {
5651
EnumConstantValue enumConstantValue = (EnumConstantValue) constantField.value();
5752
ConcreteTypeInfo enumTypeInfo = enumConstantValue.getEnumType();
5853
EnumDef enumTypeDef = (EnumDef) enumTypeInfo.typeDef();
@@ -65,28 +60,22 @@ private String toConstantTypeExpr(ConstantField constantField, TypeDef contextTy
6560
}
6661

6762
private String toConstantValue(ConstantField constantField) {
68-
if (targetType == SharedType.TargetType.RUST) {
69-
ConcreteTypeInfo type = constantField.value().getValueType();
70-
ValueHolder value = constantField.value();
71-
if (value instanceof EnumConstantValue && value.getValueType().getKind() == ConcreteTypeInfo.Kind.ENUM) {
72-
EnumConstantValue enumConstantValue = (EnumConstantValue) value;
73-
return String.format("%s::%s",type.simpleName(), enumConstantValue.getEnumConstantName());
74-
}
63+
ConcreteTypeInfo type = constantField.value().getValueType();
64+
ValueHolder value = constantField.value();
65+
if (value instanceof EnumConstantValue && value.getValueType().getKind() == ConcreteTypeInfo.Kind.ENUM) {
66+
EnumConstantValue enumConstantValue = (EnumConstantValue) value;
67+
return String.format("%s::%s",type.simpleName(), enumConstantValue.getEnumConstantName());
7568
}
7669
return constantField.value().literalValue();
7770
}
7871

79-
@RequiredArgsConstructor
80-
static final class ConstantNamespaceExpr extends AbstractTypeExpr {
81-
final String name;
82-
final List<ConstantExpr> constants;
83-
}
84-
85-
final class ConstantExpr extends AbstractFieldExpr {
72+
static final class ConstantExpr extends AbstractFieldExpr {
73+
final String keyword;
8674
final String type;
8775
final String value;
88-
ConstantExpr(ComponentInfo componentInfo, String type, String value) {
89-
super(componentInfo, targetType);
76+
ConstantExpr(ComponentInfo componentInfo, String keyword, String type, String value) {
77+
super(componentInfo, SharedType.TargetType.RUST);
78+
this.keyword = keyword;
9079
this.type = type;
9180
this.value = value;
9281
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static Set<TemplateDataConverter> typescript(Context ctx) {
2020
Set<TemplateDataConverter> converters = new HashSet<>(3);
2121
converters.add(new TypescriptInterfaceConverter(ctx, TypeExpressionConverter.typescript(ctx)));
2222
converters.add(new TypescriptEnumConverter(ctx));
23-
converters.add(new ConstantConverter(ctx, TypeExpressionConverter.nullOp(), SharedType.TargetType.TYPESCRIPT));
23+
converters.add(new DefaultConstantConverter(ctx, TypeExpressionConverter.nullOp(), SharedType.TargetType.TYPESCRIPT));
2424
return converters;
2525
}
2626

@@ -29,7 +29,7 @@ static Set<TemplateDataConverter> go(Context ctx) {
2929
TypeExpressionConverter typeExpressionConverter = TypeExpressionConverter.go(ctx);
3030
converters.add(new GoStructConverter(typeExpressionConverter));
3131
converters.add(new GoEnumConverter(ctx, typeExpressionConverter));
32-
converters.add(new ConstantConverter(ctx, typeExpressionConverter, SharedType.TargetType.GO));
32+
converters.add(new DefaultConstantConverter(ctx, typeExpressionConverter, SharedType.TargetType.GO));
3333
return converters;
3434
}
3535

@@ -40,7 +40,7 @@ static Set<TemplateDataConverter> rust(Context ctx) {
4040
Set<TemplateDataConverter> converters = new HashSet<>(3);
4141
converters.add(new RustStructConverter(ctx, rustTypeExpressionConverter, rustMacroTraitsGenerator));
4242
converters.add(new RustEnumConverter(ctx, rustLiteralTypeExpressionConverter, rustMacroTraitsGenerator));
43-
converters.add(new ConstantConverter(ctx, rustLiteralTypeExpressionConverter, SharedType.TargetType.RUST));
43+
converters.add(new RustConstantConverter(ctx, rustLiteralTypeExpressionConverter));
4444
return converters;
4545
}
4646
}

processor/src/main/resources/templates/rust/constant-inline.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
{{#tagLiteralsAbove}}
33
{{{.}}}
44
{{/tagLiteralsAbove}}
5-
pub const {{name}}: {{{type}}} = {{{value}}};{{#tagLiteralsInlineAfter}} {{{.}}}{{/tagLiteralsInlineAfter}}
5+
pub {{keyword}} {{name}}: {{{type}}} = {{{value}}};{{#tagLiteralsInlineAfter}} {{{.}}}{{/tagLiteralsInlineAfter}}
66
{{/constants}}
77

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub mod {{name}} {
33
{{#tagLiteralsAbove}}
44
{{{.}}}
55
{{/tagLiteralsAbove}}
6-
pub const {{name}}: {{{type}}} = {{{value}}};{{#tagLiteralsInlineAfter}} {{{.}}}{{/tagLiteralsInlineAfter}}
6+
pub {{keyword}} {{name}}: {{{type}}} = {{{value}}};{{#tagLiteralsInlineAfter}} {{{.}}}{{/tagLiteralsInlineAfter}}
77
{{/constants}}
88
}
99

0 commit comments

Comments
 (0)