Skip to content

Commit 8258db1

Browse files
danielkaneidervojtechhabarta
authored andcommitted
Add extension point before TsModel generation (#255)
* Add extension point before TsModel * sort imports
1 parent 357c566 commit 8258db1

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/compiler/ModelCompiler.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ public ModelCompiler(Settings settings, TypeProcessor typeProcessor) {
4242
}
4343

4444
public enum TransformationPhase {
45+
BeforeTsModel,
4546
BeforeEnums,
4647
BeforeSymbolResolution,
4748
}
4849

4950
public TsModel javaToTypeScript(Model model) {
5051
final SymbolTable symbolTable = new SymbolTable(settings);
5152
final List<Extension.TransformerDefinition> extensionTransformers = getExtensionTransformers();
53+
model = applyExtensionModelTransformers(symbolTable, model, extensionTransformers);
5254
TsModel tsModel = processModel(symbolTable, model);
5355
tsModel = removeInheritedProperties(symbolTable, tsModel);
5456
tsModel = addImplementedProperties(symbolTable, tsModel);
@@ -112,6 +114,15 @@ private List<Extension.TransformerDefinition> getExtensionTransformers() {
112114
return transformers;
113115
}
114116

117+
private static Model applyExtensionModelTransformers(SymbolTable symbolTable, Model model, List<Extension.TransformerDefinition> transformerDefinitions) {
118+
for (Extension.TransformerDefinition definition : transformerDefinitions) {
119+
if (definition.phase == TransformationPhase.BeforeTsModel) {
120+
model = definition.transformer.transformModel(symbolTable, model);
121+
}
122+
}
123+
return model;
124+
}
125+
115126
private static TsModel applyExtensionTransformers(SymbolTable symbolTable, TsModel model, TransformationPhase phase, List<Extension.TransformerDefinition> transformerDefinitions) {
116127
for (Extension.TransformerDefinition definition : transformerDefinitions) {
117128
if (definition.phase == phase) {

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/compiler/ModelTransformer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
package cz.habarta.typescript.generator.compiler;
33

44
import cz.habarta.typescript.generator.emitter.TsModel;
5+
import cz.habarta.typescript.generator.parser.Model;
56

67

78
public interface ModelTransformer {
89

910
public TsModel transformModel(SymbolTable symbolTable, TsModel model);
1011

12+
default Model transformModel(SymbolTable symbolTable, Model model) {
13+
return model;
14+
}
15+
1116
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package cz.habarta.typescript.generator;
2+
3+
import cz.habarta.typescript.generator.compiler.ModelCompiler;
4+
import cz.habarta.typescript.generator.compiler.ModelCompiler.TransformationPhase;
5+
import cz.habarta.typescript.generator.compiler.ModelTransformer;
6+
import cz.habarta.typescript.generator.compiler.SymbolTable;
7+
import cz.habarta.typescript.generator.emitter.EmitterExtensionFeatures;
8+
import cz.habarta.typescript.generator.emitter.TsModel;
9+
import cz.habarta.typescript.generator.parser.BeanModel;
10+
import cz.habarta.typescript.generator.parser.Jackson2Parser;
11+
import cz.habarta.typescript.generator.parser.Model;
12+
import java.util.ArrayList;
13+
import java.util.Collections;
14+
import java.util.List;
15+
import org.hamcrest.CoreMatchers;
16+
import org.junit.Assert;
17+
import org.junit.Test;
18+
19+
public class ExtensionTest {
20+
21+
@Test
22+
public void testBeforeTsExtension() throws Exception {
23+
final Settings settings = TestUtils.settings();
24+
25+
settings.extensions.add(new Extension() {
26+
27+
@Override
28+
public EmitterExtensionFeatures getFeatures() {
29+
return new EmitterExtensionFeatures();
30+
}
31+
32+
@Override
33+
public List<TransformerDefinition> getTransformers() {
34+
return Collections.singletonList(new TransformerDefinition(TransformationPhase.BeforeTsModel, new ModelTransformer() {
35+
36+
@Override
37+
public TsModel transformModel(SymbolTable symbolTable, TsModel model) {
38+
return model;
39+
}
40+
41+
@Override
42+
public Model transformModel(SymbolTable symbolTable, Model model) {
43+
List<BeanModel> beans = new ArrayList<>(model.getBeans());
44+
45+
BeanModel implementationBean = model.getBean(Implementation.class);
46+
BeanModel beanWithComments = implementationBean.withComments(Collections.singletonList("My new comment"));
47+
48+
beans.remove(implementationBean);
49+
beans.add(beanWithComments);
50+
51+
return new Model(beans, model.getEnums(), model.getJaxrsApplication());
52+
}
53+
}));
54+
}
55+
});
56+
57+
final Jackson2Parser jacksonParser = new Jackson2Parser(settings, new DefaultTypeProcessor());
58+
final Model model = jacksonParser.parseModel(Implementation.class);
59+
final ModelCompiler modelCompiler = new TypeScriptGenerator(settings).getModelCompiler();
60+
61+
final TsModel result = modelCompiler.javaToTypeScript(model);
62+
63+
Assert.assertEquals(1, result.getBean(Implementation.class).getComments().size());
64+
Assert.assertThat(result.getBean(Implementation.class).getComments().get(0), CoreMatchers.containsString("My new comment"));
65+
}
66+
67+
private static class Implementation { }
68+
69+
}

0 commit comments

Comments
 (0)