Search in sources :

Example 6 with TsModel

use of cz.habarta.typescript.generator.emitter.TsModel in project typescript-generator by vojtechhabarta.

the class BeanPropertyPathExtensionTest method basicTest.

@Test
public void basicTest() throws Exception {
    final StringBuilder data = new StringBuilder();
    final EmitterExtension.Writer writer = new EmitterExtension.Writer() {

        @Override
        public void writeIndentedLine(String line) {
            data.append(line + "\n");
        }
    };
    final Settings settings = new Settings();
    settings.sortDeclarations = true;
    final TypeProcessor typeProcessor = new DefaultTypeProcessor();
    final Model model = new Jackson2Parser(settings, typeProcessor).parseModel(ClassA.class);
    final TsModel tsModel = new ModelCompiler(settings, typeProcessor).javaToTypeScript(model);
    new BeanPropertyPathExtension().emitElements(writer, settings, false, tsModel);
    String dataStr = data.toString();
    final String expected = Utils.readString(getClass().getResourceAsStream("/ext/expected.ts"), "\n");
    Assert.assertEquals(expected.trim(), dataStr.trim());
}
Also used : Jackson2Parser(cz.habarta.typescript.generator.parser.Jackson2Parser) EmitterExtension(cz.habarta.typescript.generator.emitter.EmitterExtension) DefaultTypeProcessor(cz.habarta.typescript.generator.DefaultTypeProcessor) TsModel(cz.habarta.typescript.generator.emitter.TsModel) ModelCompiler(cz.habarta.typescript.generator.compiler.ModelCompiler) TypeProcessor(cz.habarta.typescript.generator.TypeProcessor) DefaultTypeProcessor(cz.habarta.typescript.generator.DefaultTypeProcessor) Model(cz.habarta.typescript.generator.parser.Model) TsModel(cz.habarta.typescript.generator.emitter.TsModel) Settings(cz.habarta.typescript.generator.Settings) Test(org.junit.Test)

Example 7 with TsModel

use of cz.habarta.typescript.generator.emitter.TsModel in project typescript-generator by vojtechhabarta.

the class ClassEnumExtension method getTransformers.

@Override
public List<TransformerDefinition> getTransformers() {
    return Arrays.asList(new TransformerDefinition(ModelCompiler.TransformationPhase.BeforeEnums, new ModelTransformer() {

        @Override
        public TsModel transformModel(SymbolTable symbolTable, TsModel model) {
            List<TsBeanModel> beans = model.getBeans();
            List<TsBeanModel> classEnums = new ArrayList<>();
            for (TsBeanModel bean : beans) {
                if (bean.getName().getSimpleName().contains(classEnumPattern))
                    classEnums.add(bean);
            }
            List<TsEnumModel> stringEnums = new ArrayList<>();
            for (TsBeanModel tsBeanModel : classEnums) {
                List<EnumMemberModel> members = new ArrayList<>();
                for (Field declaredField : tsBeanModel.getOrigin().getDeclaredFields()) {
                    if (declaredField.getType().getName().equals(tsBeanModel.getOrigin().getName())) {
                        members.add(new EnumMemberModel(declaredField.getName(), declaredField.getName(), null));
                    }
                }
                TsEnumModel temp = new TsEnumModel(tsBeanModel.getOrigin(), tsBeanModel.getName(), EnumKind.StringBased, members, null);
                stringEnums.add(temp);
            }
            return model.withEnums(stringEnums).withoutBeans(classEnums);
        }
    }));
}
Also used : EnumMemberModel(cz.habarta.typescript.generator.compiler.EnumMemberModel) Field(java.lang.reflect.Field) ModelTransformer(cz.habarta.typescript.generator.compiler.ModelTransformer) TsModel(cz.habarta.typescript.generator.emitter.TsModel) SymbolTable(cz.habarta.typescript.generator.compiler.SymbolTable) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsEnumModel(cz.habarta.typescript.generator.emitter.TsEnumModel)

Aggregations

TsModel (cz.habarta.typescript.generator.emitter.TsModel)7 ModelCompiler (cz.habarta.typescript.generator.compiler.ModelCompiler)5 Model (cz.habarta.typescript.generator.parser.Model)5 Jackson2Parser (cz.habarta.typescript.generator.parser.Jackson2Parser)4 Settings (cz.habarta.typescript.generator.Settings)2 ModelTransformer (cz.habarta.typescript.generator.compiler.ModelTransformer)2 SymbolTable (cz.habarta.typescript.generator.compiler.SymbolTable)2 EmitterExtension (cz.habarta.typescript.generator.emitter.EmitterExtension)2 EmitterExtensionFeatures (cz.habarta.typescript.generator.emitter.EmitterExtensionFeatures)2 Field (java.lang.reflect.Field)2 Test (org.junit.Test)2 JsonSerialize (com.fasterxml.jackson.databind.annotation.JsonSerialize)1 Converter (com.fasterxml.jackson.databind.util.Converter)1 DefaultTypeProcessor (cz.habarta.typescript.generator.DefaultTypeProcessor)1 Extension (cz.habarta.typescript.generator.Extension)1 TypeProcessor (cz.habarta.typescript.generator.TypeProcessor)1 EnumMemberModel (cz.habarta.typescript.generator.compiler.EnumMemberModel)1 TsBeanModel (cz.habarta.typescript.generator.emitter.TsBeanModel)1 TsEnumModel (cz.habarta.typescript.generator.emitter.TsEnumModel)1 EnumConstantsExtension (cz.habarta.typescript.generator.ext.EnumConstantsExtension)1