Search in sources :

Example 1 with SymbolTable

use of cz.habarta.typescript.generator.compiler.SymbolTable in project typescript-generator by vojtechhabarta.

the class NamingTest method testTypeScriptKeywords.

@Test
public void testTypeScriptKeywords() {
    final Settings settings = TestUtils.settings();
    settings.mapPackagesToNamespaces = true;
    final SymbolTable symbolTable = new SymbolTable(settings);
    final String name = symbolTable.getMappedFullName(KeywordInPackage.class);
    Assert.assertEquals("cz.habarta.typescript.generator._yield.KeywordInPackage", name);
}
Also used : SymbolTable(cz.habarta.typescript.generator.compiler.SymbolTable) Test(org.junit.Test)

Example 2 with SymbolTable

use of cz.habarta.typescript.generator.compiler.SymbolTable in project typescript-generator by vojtechhabarta.

the class NamingTest method testCombinations.

@Test
public void testCombinations() {
    final Settings settings = TestUtils.settings();
    settings.customTypeNamingFunction = "function(name, simpleName) { if (name.indexOf('cz.') === 0) return 'Func' + simpleName; }";
    settings.addTypeNamePrefix = "Conf";
    settings.mapPackagesToNamespaces = true;
    final SymbolTable symbolTable = new SymbolTable(settings);
    Assert.assertEquals("FuncA", symbolTable.getMappedFullName(A.class));
    Assert.assertEquals("java.lang.ConfObject", symbolTable.getMappedFullName(Object.class));
}
Also used : SymbolTable(cz.habarta.typescript.generator.compiler.SymbolTable) Test(org.junit.Test)

Example 3 with SymbolTable

use of cz.habarta.typescript.generator.compiler.SymbolTable in project openremote by openremote.

the class JsonSerializeExtension method getTransformers.

@Override
public List<cz.habarta.typescript.generator.Extension.TransformerDefinition> getTransformers() {
    return Arrays.asList(new cz.habarta.typescript.generator.Extension.TransformerDefinition(ModelCompiler.TransformationPhase.BeforeTsModel, new ModelTransformer() {

        @Override
        public TsModel transformModel(SymbolTable symbolTable, TsModel model) {
            return model;
        }

        @Override
        public Model transformModel(SymbolTable symbolTable, Model model) {
            // Look for @JsonSerialize annotation and modify the property type accordingly
            List<BeanModel> beans = model.getBeans();
            beans.replaceAll(bean -> {
                AtomicBoolean modified = new AtomicBoolean(false);
                List<PropertyModel> properties = bean.getProperties().stream().map(p -> {
                    Member member = p.getOriginalMember();
                    JsonSerialize jsonSerialize = null;
                    if (member instanceof Field) {
                        Field field = (Field) member;
                        jsonSerialize = field.getAnnotation(JsonSerialize.class);
                    } else if (member instanceof Method) {
                        Method method = (Method) member;
                        jsonSerialize = method.getAnnotation(JsonSerialize.class);
                    }
                    if (jsonSerialize != null) {
                        // TODO: Add support for other options
                        if (jsonSerialize.as() != Void.class) {
                            modified.set(true);
                            return new PropertyModel(p.getName(), jsonSerialize.as(), p.isOptional(), p.getAccess(), p.getOriginalMember(), p.getPullProperties(), p.getContext(), p.getComments());
                        }
                        if (jsonSerialize.converter() != Converter.None.class) {
                            // Type info is not accessible with reflection so instantiate the converter
                            Method convertMethod = Arrays.stream(jsonSerialize.converter().getMethods()).filter(m -> m.getName().equals("convert")).findFirst().orElse(null);
                            if (convertMethod != null) {
                                modified.set(true);
                                return new PropertyModel(p.getName(), convertMethod.getGenericReturnType(), p.isOptional(), p.getAccess(), p.getOriginalMember(), p.getPullProperties(), p.getContext(), p.getComments());
                            }
                        }
                    }
                    return p;
                }).collect(Collectors.toList());
                if (modified.get()) {
                    return new BeanModel(bean.getOrigin(), bean.getParent(), bean.getTaggedUnionClasses(), bean.getDiscriminantProperty(), bean.getDiscriminantLiteral(), bean.getInterfaces(), properties, bean.getComments());
                }
                return bean;
            });
            return model;
        }
    }));
}
Also used : Arrays(java.util.Arrays) Converter(com.fasterxml.jackson.databind.util.Converter) PropertyModel(cz.habarta.typescript.generator.parser.PropertyModel) EmitterExtensionFeatures(cz.habarta.typescript.generator.emitter.EmitterExtensionFeatures) Member(java.lang.reflect.Member) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Field(java.lang.reflect.Field) TsModel(cz.habarta.typescript.generator.emitter.TsModel) Collectors(java.util.stream.Collectors) List(java.util.List) ModelTransformer(cz.habarta.typescript.generator.compiler.ModelTransformer) JsonSerialize(com.fasterxml.jackson.databind.annotation.JsonSerialize) SymbolTable(cz.habarta.typescript.generator.compiler.SymbolTable) BeanModel(cz.habarta.typescript.generator.parser.BeanModel) Model(cz.habarta.typescript.generator.parser.Model) ModelCompiler(cz.habarta.typescript.generator.compiler.ModelCompiler) Method(java.lang.reflect.Method) BeanModel(cz.habarta.typescript.generator.parser.BeanModel) PropertyModel(cz.habarta.typescript.generator.parser.PropertyModel) SymbolTable(cz.habarta.typescript.generator.compiler.SymbolTable) Method(java.lang.reflect.Method) JsonSerialize(com.fasterxml.jackson.databind.annotation.JsonSerialize) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Field(java.lang.reflect.Field) ModelTransformer(cz.habarta.typescript.generator.compiler.ModelTransformer) TsModel(cz.habarta.typescript.generator.emitter.TsModel) PropertyModel(cz.habarta.typescript.generator.parser.PropertyModel) TsModel(cz.habarta.typescript.generator.emitter.TsModel) BeanModel(cz.habarta.typescript.generator.parser.BeanModel) Model(cz.habarta.typescript.generator.parser.Model) Member(java.lang.reflect.Member)

Example 4 with SymbolTable

use of cz.habarta.typescript.generator.compiler.SymbolTable in project typescript-generator by vojtechhabarta.

the class NamingTest method testTypeNamingFunctionReturnsUndefined.

@Test
public void testTypeNamingFunctionReturnsUndefined() {
    final Settings settings = TestUtils.settings();
    settings.customTypeNamingFunction = "function() {}";
    final SymbolTable symbolTable = new SymbolTable(settings);
    final String name = symbolTable.getMappedFullName(A.class);
    Assert.assertEquals("A", name);
}
Also used : SymbolTable(cz.habarta.typescript.generator.compiler.SymbolTable) Test(org.junit.Test)

Example 5 with SymbolTable

use of cz.habarta.typescript.generator.compiler.SymbolTable in project typescript-generator by vojtechhabarta.

the class NamingTest method testTypeNamingFunction.

@Test
public void testTypeNamingFunction() {
    final Settings settings = TestUtils.settings();
    settings.customTypeNamingFunction = "function(name, simpleName) { if (name.indexOf('cz.') === 0) return 'Test' + simpleName; }";
    final SymbolTable symbolTable = new SymbolTable(settings);
    final String name = symbolTable.getMappedFullName(A.class);
    Assert.assertEquals("TestA", name);
}
Also used : SymbolTable(cz.habarta.typescript.generator.compiler.SymbolTable) Test(org.junit.Test)

Aggregations

SymbolTable (cz.habarta.typescript.generator.compiler.SymbolTable)6 Test (org.junit.Test)4 ModelTransformer (cz.habarta.typescript.generator.compiler.ModelTransformer)2 TsModel (cz.habarta.typescript.generator.emitter.TsModel)2 Field (java.lang.reflect.Field)2 JsonSerialize (com.fasterxml.jackson.databind.annotation.JsonSerialize)1 Converter (com.fasterxml.jackson.databind.util.Converter)1 EnumMemberModel (cz.habarta.typescript.generator.compiler.EnumMemberModel)1 ModelCompiler (cz.habarta.typescript.generator.compiler.ModelCompiler)1 EmitterExtensionFeatures (cz.habarta.typescript.generator.emitter.EmitterExtensionFeatures)1 TsBeanModel (cz.habarta.typescript.generator.emitter.TsBeanModel)1 TsEnumModel (cz.habarta.typescript.generator.emitter.TsEnumModel)1 BeanModel (cz.habarta.typescript.generator.parser.BeanModel)1 Model (cz.habarta.typescript.generator.parser.Model)1 PropertyModel (cz.habarta.typescript.generator.parser.PropertyModel)1 Member (java.lang.reflect.Member)1 Method (java.lang.reflect.Method)1 Arrays (java.util.Arrays)1 List (java.util.List)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1