Skip to content

Commit b90b294

Browse files
authored
90 ts non const enum option (#91)
1 parent 8e8c883 commit b90b294

12 files changed

Lines changed: 28 additions & 19 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160

161161
/**
162162
* Format of enum in Typescript.
163-
* @return either "union" (union types) or "const_enum" (const enum). If empty, fallback to global default.
163+
* @return one of "const_enum" (const enum), "enum" (enum), or "union" (union types). If empty, fallback to global default.
164164
*/
165165
String typescriptEnumFormat() default "";
166166

doc/Usage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ You can customize the path by config `maven-compiler-plugin`:
8282
See [Default Properties](../processor/src/main/resources/sharedtype-default.properties) for details.
8383

8484
#### Per annotation options
85-
See Javadoc on [@SharedType](../annotation/src/main/java/online/sharedtype/annotation/SharedType.java) for details.
85+
See Javadoc on [@SharedType](../annotation/src/main/java/online/sharedtype/SharedType.java) for details.
8686

8787
### Limitations
8888
* Current design only retain `@SharedType` on source level. That means they are not visible if it is in a dependency jar during its dependent's compilation.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import lombok.Getter;
44
import online.sharedtype.SharedType;
55

6-
@SharedType(typescriptEnumFormat = "const_enum")
6+
@SharedType(typescriptEnumFormat = "enum")
77
@Getter
88
public enum EnumTShirt {
99
S(EnumSize.SMALL, "S"),
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package online.sharedtype.it.java8;
22

3-
public class TempClass {
3+
import online.sharedtype.SharedType;
4+
5+
import java.time.LocalDate;
46

7+
public class TempClass {
8+
LocalDate localDate;
59
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,38 +67,41 @@ private static boolean evaluateOptionalBool(SharedType.OptionalBool optionalBool
6767
}
6868
}
6969

70-
private static Set<Props.Typescript.OptionalFieldFormat> parseTsOptionalFieldFormats(SharedType anno, Context ctx) {
70+
private Set<Props.Typescript.OptionalFieldFormat> parseTsOptionalFieldFormats(SharedType anno, Context ctx) {
7171
if (anno.typescriptOptionalFieldFormat().length > 0) {
7272
List<String> values = Arrays.asList(anno.typescriptOptionalFieldFormat());
7373
try {
7474
return EnumParsingUtils.parseEnumSet(values, Props.Typescript.OptionalFieldFormat.class, Props.Typescript.OptionalFieldFormat::fromString);
7575
} catch (IllegalArgumentException e) {
7676
throw new SharedTypeException(String.format(
77-
"Invalid value for SharedType.typescriptOptionalFieldFormat: %s, only '?', 'null', 'undefined' are allowed.", values), e);
77+
"Invalid value for SharedType.typescriptOptionalFieldFormat: %s, only '?', 'null', 'undefined' are allowed. " +
78+
"When parsing annotation for '%s'.", values, qualifiedName), e);
7879
}
7980
}
8081
return ctx.getProps().getTypescript().getOptionalFieldFormats();
8182
}
8283

83-
private static Props.Typescript.EnumFormat parseTsEnumFormat(SharedType anno, Context ctx) {
84+
private Props.Typescript.EnumFormat parseTsEnumFormat(SharedType anno, Context ctx) {
8485
if (anno.typescriptEnumFormat() != null && !anno.typescriptEnumFormat().isEmpty()) {
8586
try {
8687
return Props.Typescript.EnumFormat.fromString(anno.typescriptEnumFormat());
8788
} catch (IllegalArgumentException e) {
8889
throw new SharedTypeException(String.format(
89-
"Invalid value for SharedType.typescriptEnumFormat: '%s', only 'union' or 'const_enum' is allowed.", anno.typescriptEnumFormat()), e);
90+
"Invalid value for SharedType.typescriptEnumFormat: '%s', only one of 'union', 'const_enum', 'enum' is allowed. " +
91+
"When parsing annotation for '%s'.", anno.typescriptEnumFormat(), qualifiedName), e);
9092
}
9193
}
9294
return ctx.getProps().getTypescript().getEnumFormat();
9395
}
9496

95-
private static Props.Typescript.FieldReadonlyType parseTsFieldReadonlyType(SharedType anno, Context ctx) {
97+
private Props.Typescript.FieldReadonlyType parseTsFieldReadonlyType(SharedType anno, Context ctx) {
9698
if (anno.typescriptFieldReadonlyType() != null && !anno.typescriptFieldReadonlyType().isEmpty()) {
9799
try {
98100
return Props.Typescript.FieldReadonlyType.fromString(anno.typescriptFieldReadonlyType());
99101
} catch (IllegalArgumentException e) {
100102
throw new SharedTypeException(String.format(
101-
"Invalid value for SharedType.typescriptFieldReadonlyType: '%s', only 'all', 'acyclic', 'none' is allowed.", anno.typescriptFieldReadonlyType()), e);
103+
"Invalid value for SharedType.typescriptFieldReadonlyType: '%s', only 'all', 'acyclic', 'none' is allowed. " +
104+
"When parsing annotation for '%s'.", anno.typescriptFieldReadonlyType(), qualifiedName), e);
102105
}
103106
}
104107
return ctx.getProps().getTypescript().getFieldReadonlyType();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static OptionalFieldFormat fromString(String value) {
5959
}
6060

6161
public enum EnumFormat {
62-
UNION, CONST_ENUM,
62+
UNION, CONST_ENUM, ENUM,
6363
;
6464
public static EnumFormat fromString(String value) {
6565
return EnumFormat.valueOf(value.toUpperCase());

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,24 @@ public Tuple<Template, Object> convert(TypeDef typeDef) {
3434
values.add(ConversionUtils.literalValue(component.value()));
3535
}
3636
return Tuple.of(Template.TEMPLATE_TYPESCRIPT_UNION_TYPE_ENUM, new EnumUnionExpr(enumDef.simpleName(), values));
37-
} else if (config.getTypescriptEnumFormat() == Props.Typescript.EnumFormat.CONST_ENUM) {
37+
} else {
3838
EnumExpr value = new EnumExpr(
3939
enumDef.simpleName(),
40+
config.getTypescriptEnumFormat() == Props.Typescript.EnumFormat.CONST_ENUM,
4041
enumDef.components().stream().map(component -> new EnumValueExpr(
4142
component.name(),
4243
ConversionUtils.literalValue(component.value())
4344
)).collect(Collectors.toList())
4445
);
45-
return Tuple.of(Template.TEMPLATE_TYPESCRIPT_CONST_ENUM, value);
46+
return Tuple.of(Template.TEMPLATE_TYPESCRIPT_ENUM, value);
4647
}
47-
throw new SharedTypeInternalError("Unknown typescript enum format: " + config.getTypescriptEnumFormat());
4848
}
4949

5050
@SuppressWarnings("unused")
5151
@RequiredArgsConstructor
5252
static final class EnumExpr {
5353
final String name;
54+
final boolean isConst;
5455
final List<EnumValueExpr> values;
5556
}
5657

processor/src/main/java/online/sharedtype/processor/writer/render/Template.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public final class Template {
1515
public static final Template TEMPLATE_TYPESCRIPT_HEADER = new Template(OutputTarget.TYPESCRIPT, "header");
1616
public static final Template TEMPLATE_TYPESCRIPT_INTERFACE = new Template(OutputTarget.TYPESCRIPT, "interface");
1717
public static final Template TEMPLATE_TYPESCRIPT_UNION_TYPE_ENUM = new Template(OutputTarget.TYPESCRIPT, "union-type-enum");
18-
public static final Template TEMPLATE_TYPESCRIPT_CONST_ENUM = new Template(OutputTarget.TYPESCRIPT, "const-enum");
18+
public static final Template TEMPLATE_TYPESCRIPT_ENUM = new Template(OutputTarget.TYPESCRIPT, "enum");
1919
public static final Template TEMPLATE_RUST_HEADER = new Template(OutputTarget.RUST, "header");
2020
public static final Template TEMPLATE_RUST_STRUCT = new Template(OutputTarget.RUST, "struct");
2121
public static final Template TEMPLATE_RUST_ENUM = new Template(OutputTarget.RUST, "enum");

processor/src/main/resources/sharedtype-default.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ sharedtype.typescript.interface-property-delimiter=;
5151
## optional field format, can be combination of "?", "null", "undefined", the latter 2 are rendered as union type. Cannot be empty.
5252
sharedtype.typescript.optional-field-format=?
5353

54-
## enum format, can be "const_enum" (const enum) or "union" (union type).
54+
## enum format, can be "const_enum" (const enum), "enum" (enum), or "union" (union type).
5555
sharedtype.typescript.enum-format=union
5656

5757
## whether to mark field as readonly, value can be one of:

processor/src/main/resources/templates/typescript/const-enum.mustache renamed to processor/src/main/resources/templates/typescript/enum.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const enum {{name}} {
1+
export {{#isConst}}const {{/isConst}}enum {{name}} {
22
{{#values}}
33
{{name}} = {{{value}}},
44
{{/values}}

0 commit comments

Comments
 (0)