Skip to content

Commit 2fa8add

Browse files
custom type mapping (closes #60)
1 parent f557835 commit 2fa8add

File tree

10 files changed

+128
-3
lines changed

10 files changed

+128
-3
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ release.properties
1010
**.classpath
1111
**.project
1212
**.settings
13-
*.d.ts
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
package cz.habarta.typescript.generator;
3+
4+
import java.lang.reflect.Type;
5+
import java.util.*;
6+
7+
8+
public class CustomMappingTypeProcessor implements TypeProcessor {
9+
10+
private final Map<String, String> customMappings;
11+
12+
public CustomMappingTypeProcessor(Map<String, String> customMappings) {
13+
this.customMappings = customMappings;
14+
}
15+
16+
@Override
17+
public Result processType(Type javaType, Context context) {
18+
final Class<?> rawClass = Utils.getRawClassOrNull(javaType);
19+
if (rawClass != null) {
20+
final String tsTypeName = customMappings.get(rawClass.getName());
21+
if (tsTypeName != null) {
22+
return new Result(new TsType.BasicType(tsTypeName));
23+
}
24+
}
25+
return null;
26+
}
27+
28+
}

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/Settings.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public class Settings {
2828
public String addTypeNamePrefix = null;
2929
public String addTypeNameSuffix = null;
3030
public Map<String, String> customTypeNaming = new LinkedHashMap<>();
31+
public List<String> referencedFiles = new ArrayList<>();
32+
public List<String> importDeclarations = new ArrayList<>();
33+
public Map<String, String> customTypeMappings = new LinkedHashMap<>();
3134
public DateMapping mapDate = DateMapping.asDate;
3235
public TypeProcessor customTypeProcessor = null;
3336
public boolean sortDeclarations = false;

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/TypeScriptGenerator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public TypeProcessor getTypeProcessor() {
5858
if (settings.customTypeProcessor != null) {
5959
processors.add(settings.customTypeProcessor);
6060
}
61+
processors.add(new CustomMappingTypeProcessor(settings.customTypeMappings));
6162
processors.add(new GenericsTypeProcessor());
6263
processors.add(new DefaultTypeProcessor());
6364
typeProcessor = new TypeProcessor.Chain(processors);

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/emitter/Emitter.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public void emit(TsModel model, Writer output, String outputName, boolean closeO
2626
System.out.println("Writing declarations to: " + outputName);
2727
}
2828
emitFileComment();
29+
emitReferences();
30+
emitImports();
2931
emitModule(model);
3032
if (closeOutput) {
3133
close();
@@ -39,10 +41,28 @@ private void emitFileComment() {
3941
}
4042
}
4143

44+
private void emitReferences() {
45+
if (settings.referencedFiles != null && !settings.referencedFiles.isEmpty()) {
46+
writeNewLine();
47+
for (String reference : settings.referencedFiles) {
48+
writeIndentedLine("/// <reference path=" + quote(reference) + " />");
49+
}
50+
}
51+
}
52+
53+
private void emitImports() {
54+
if (settings.importDeclarations != null && !settings.importDeclarations.isEmpty()) {
55+
writeNewLine();
56+
for (String importDeclaration : settings.importDeclarations) {
57+
writeIndentedLine(importDeclaration + ";");
58+
}
59+
}
60+
}
61+
4262
private void emitModule(TsModel model) {
4363
if (settings.outputKind == TypeScriptOutputKind.ambientModule) {
4464
writeNewLine();
45-
writeIndentedLine("declare module " + settings.quotes + settings.module + settings.quotes + " {");
65+
writeIndentedLine("declare module " + quote(settings.module) + " {");
4666
indent++;
4767
emitNamespace(model);
4868
indent--;
@@ -122,7 +142,7 @@ private void emitProperty(TsPropertyModel property) {
122142
}
123143

124144
private String toPropertyName(String name) {
125-
return isValidIdentifierName(name) ? name : (settings.quotes + name + settings.quotes);
145+
return isValidIdentifierName(name) ? name : quote(name);
126146
}
127147

128148
// https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#2.2.2
@@ -192,6 +212,10 @@ private void writeNewLine() {
192212
}
193213
}
194214

215+
private String quote(String value) {
216+
return settings.quotes + value + settings.quotes;
217+
}
218+
195219
private void close() {
196220
try {
197221
writer.close();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
package cz.habarta.typescript.generator;
3+
4+
import java.util.*;
5+
import static org.junit.Assert.*;
6+
import org.junit.Test;
7+
8+
9+
public class CustomTypeMappingTest {
10+
11+
@Test
12+
public void test() {
13+
final Settings settings = TestUtils.settings();
14+
settings.quotes = "'";
15+
settings.referencedFiles.add("../src/test/ts/my-custom-types.d.ts");
16+
settings.importDeclarations.add("import * as myModule from '../src/test/ts/my-module.d.ts'");
17+
settings.customTypeMappings.put("java.util.Date", "MyDate");
18+
settings.customTypeMappings.put("java.util.Calendar", "myModule.MyCalendar");
19+
// new TypeScriptGenerator(settings).generateTypeScript(Input.from(CustomTypesUsage.class), Output.to(new File("target/CustomTypeMappingTest.d.ts")));
20+
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(CustomTypesUsage.class));
21+
assertTrue(output.contains("/// <reference path='../src/test/ts/my-custom-types.d.ts' />"));
22+
assertTrue(output.contains("import * as myModule from '../src/test/ts/my-module.d.ts';"));
23+
assertTrue(output.contains("date1: MyDate;"));
24+
assertTrue(output.contains("calendar1: myModule.MyCalendar;"));
25+
}
26+
27+
private static class CustomTypesUsage {
28+
public Date date1;
29+
public Calendar calendar1;
30+
}
31+
32+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// used in CustomTypeMappingTest
2+
3+
type MyDate = string;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// used in CustomTypeMappingTest
2+
3+
export type MyCalendar = string;

typescript-generator-gradle-plugin/src/main/java/cz/habarta/typescript/generator/gradle/GenerateTask.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public class GenerateTask extends DefaultTask {
2828
public String addTypeNamePrefix;
2929
public String addTypeNameSuffix;
3030
public List<String> customTypeNaming;
31+
public List<String> referencedFiles;
32+
public List<String> importDeclarations;
33+
public List<String> customTypeMappings;
3134
public DateMapping mapDate;
3235
public String customTypeProcessor;
3336
public boolean sortDeclarations;
@@ -77,6 +80,9 @@ public void generate() throws Exception {
7780
settings.addTypeNamePrefix = addTypeNamePrefix;
7881
settings.addTypeNameSuffix = addTypeNameSuffix;
7982
settings.customTypeNaming = Settings.convertToMap(customTypeNaming);
83+
settings.referencedFiles = referencedFiles;
84+
settings.importDeclarations = importDeclarations;
85+
settings.customTypeMappings = Settings.convertToMap(customTypeMappings);
8086
settings.mapDate = mapDate;
8187
settings.loadCustomTypeProcessor(classLoader, customTypeProcessor);
8288
settings.sortDeclarations = sortDeclarations;

typescript-generator-maven-plugin/src/main/java/cz/habarta/typescript/generator/maven/GenerateMojo.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,29 @@ public class GenerateMojo extends AbstractMojo {
137137
@Parameter
138138
private List<String> customTypeNaming;
139139

140+
/**
141+
* List of files which will be referenced using triple-slash directive: /// &lt;reference path="file" />.
142+
* This can be used with "customTypeMappings" to provide needed TypeScript types.
143+
*/
144+
@Parameter
145+
private List<String> referencedFiles;
146+
147+
/**
148+
* List of import declarations which will be added to generated output.
149+
* This can be used with "customTypeMappings" to provide needed TypeScript types.
150+
*/
151+
@Parameter
152+
private List<String> importDeclarations;
153+
154+
/**
155+
* List of custom mappings.
156+
* Each item specifies TypeScript type which will be used for particular Java class.
157+
* Item format is: "javaClass:typescriptType".
158+
* For example mapping "ZonedDateTime" to "string" would be added as "java.time.ZonedDateTime:string".
159+
*/
160+
@Parameter
161+
private List<String> customTypeMappings;
162+
140163
/**
141164
* Specifies how {@link java.util.Date} will be mapped.
142165
* Supported values are 'asDate', 'asNumber, 'asString'.
@@ -238,6 +261,9 @@ public void execute() {
238261
settings.addTypeNamePrefix = addTypeNamePrefix;
239262
settings.addTypeNameSuffix = addTypeNameSuffix;
240263
settings.customTypeNaming = Settings.convertToMap(customTypeNaming);
264+
settings.referencedFiles = referencedFiles;
265+
settings.importDeclarations = importDeclarations;
266+
settings.customTypeMappings = Settings.convertToMap(customTypeMappings);
241267
settings.mapDate = mapDate;
242268
settings.loadCustomTypeProcessor(classLoader, customTypeProcessor);
243269
settings.sortDeclarations = sortDeclarations;

0 commit comments

Comments
 (0)