Search in sources :

Example 6 with TsBeanModel

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

the class BeanPropertyPathExtension method getBeanModelByType.

private static TsBeanModel getBeanModelByType(TsModel model, TsType type) {
    TsType originalType = extractOriginalTsType(type);
    if (!(originalType instanceof TsType.ReferenceType)) {
        return null;
    }
    TsType.ReferenceType originalTypeBean = (TsType.ReferenceType) originalType;
    for (TsBeanModel curBean : model.getBeans()) {
        if (curBean.getName().equals(originalTypeBean.symbol)) {
            return curBean;
        }
    }
    return null;
}
Also used : TsType(cz.habarta.typescript.generator.TsType) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel)

Example 7 with TsBeanModel

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

the class JsonDeserializationExtension method useDeserializationMethodsInJaxrs.

private TsModel useDeserializationMethodsInJaxrs(SymbolTable symbolTable, TsModel tsModel) {
    final List<TsBeanModel> beans = new ArrayList<>();
    for (TsBeanModel bean : tsModel.getBeans()) {
        if (bean.isJaxrsApplicationClientBean()) {
            final List<TsMethodModel> methods = new ArrayList<>();
            for (TsMethodModel method : bean.getMethods()) {
                final TsMethodModel changedMethod = addCopyFnToJaxrsMethod(symbolTable, tsModel, method);
                methods.add(changedMethod != null ? changedMethod : method);
            }
            beans.add(bean.withMethods(methods));
        } else {
            beans.add(bean);
        }
    }
    return tsModel.withBeans(beans);
}
Also used : ArrayList(java.util.ArrayList) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsMethodModel(cz.habarta.typescript.generator.emitter.TsMethodModel)

Example 8 with TsBeanModel

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

the class JsonDeserializationExtension method getCopyFunctionForTsType.

private static TsExpression getCopyFunctionForTsType(SymbolTable symbolTable, TsModel tsModel, TsType tsType) {
    if (tsType instanceof TsType.GenericReferenceType) {
        final TsType.GenericReferenceType genericReferenceType = (TsType.GenericReferenceType) tsType;
        // Class.fromDataFn<T1...>(constructorFnOfT1...)
        final List<TsExpression> arguments = new ArrayList<>();
        for (TsType typeArgument : genericReferenceType.typeArguments) {
            arguments.add(getCopyFunctionForTsType(symbolTable, tsModel, typeArgument));
        }
        return new TsCallExpression(new TsMemberExpression(new TsTypeReferenceExpression(new TsType.ReferenceType(genericReferenceType.symbol)), "fromDataFn"), genericReferenceType.typeArguments, arguments);
    }
    if (tsType instanceof TsType.ReferenceType) {
        final TsType.ReferenceType referenceType = (TsType.ReferenceType) tsType;
        final TsBeanModel referencedBean = tsModel.getBean(symbolTable.getSymbolClass(referenceType.symbol));
        if (referencedBean != null && referencedBean.isClass()) {
            if (referencedBean.getTaggedUnionAlias() != null) {
                // Class.fromDataUnion (tagged union)
                return new TsMemberExpression(new TsTypeReferenceExpression(new TsType.ReferenceType(referencedBean.getName())), "fromDataUnion");
            } else {
                // Class.fromData
                return new TsMemberExpression(new TsTypeReferenceExpression(referenceType), "fromData");
            }
        }
    }
    if (tsType instanceof TsType.BasicArrayType) {
        // __getCopyArrayFn
        final TsType.BasicArrayType arrayType = (TsType.BasicArrayType) tsType;
        return new TsCallExpression(new TsIdentifierReference("__getCopyArrayFn"), getCopyFunctionForTsType(symbolTable, tsModel, arrayType.elementType));
    }
    if (tsType instanceof TsType.IndexedArrayType) {
        // __getCopyObjectFn
        final TsType.IndexedArrayType objectType = (TsType.IndexedArrayType) tsType;
        return new TsCallExpression(new TsIdentifierReference("__getCopyObjectFn"), getCopyFunctionForTsType(symbolTable, tsModel, objectType.elementType));
    }
    if (tsType instanceof TsType.GenericVariableType) {
        // constructorFnOfT
        final TsType.GenericVariableType genericVariableType = (TsType.GenericVariableType) tsType;
        return new TsIdentifierReference("constructorFnOf" + genericVariableType.name);
    }
    // __identity
    return new TsCallExpression(new TsIdentifierReference("__identity"), Arrays.asList(tsType), Collections.<TsExpression>emptyList());
}
Also used : TsMemberExpression(cz.habarta.typescript.generator.emitter.TsMemberExpression) TsExpression(cz.habarta.typescript.generator.emitter.TsExpression) ArrayList(java.util.ArrayList) TsType(cz.habarta.typescript.generator.TsType) TsCallExpression(cz.habarta.typescript.generator.emitter.TsCallExpression) TsTypeReferenceExpression(cz.habarta.typescript.generator.emitter.TsTypeReferenceExpression) TsIdentifierReference(cz.habarta.typescript.generator.emitter.TsIdentifierReference) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel)

Example 9 with TsBeanModel

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

the class JsonDeserializationExtension method createDeserializationMethods.

private static TsModel createDeserializationMethods(SymbolTable symbolTable, TsModel tsModel) {
    tsModel.getHelpers().add(TsHelper.loadFromResource("/helpers/jsonDeserialization.ts"));
    final List<TsBeanModel> beans = new ArrayList<>();
    for (TsBeanModel bean : tsModel.getBeans()) {
        if (bean.isDataClass()) {
            final List<TsMethodModel> methods = new ArrayList<>(bean.getMethods());
            final TsMethodModel deserializationMethod = createDeserializationMethod(symbolTable, tsModel, bean);
            methods.add(0, deserializationMethod);
            if (!bean.getTypeParameters().isEmpty()) {
                final TsMethodModel genericFunctionConstructor = createDeserializationGenericFunctionConstructor(symbolTable, tsModel, bean);
                methods.add(0, genericFunctionConstructor);
            }
            if (bean.getTaggedUnionAlias() != null) {
                final TsMethodModel unionDeserializationMethod = createDeserializationMethodForTaggedUnion(symbolTable, tsModel, bean);
                methods.add(1, unionDeserializationMethod);
            }
            beans.add(bean.withMethods(methods));
        } else {
            beans.add(bean);
        }
    }
    return tsModel.withBeans(beans);
}
Also used : ArrayList(java.util.ArrayList) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsMethodModel(cz.habarta.typescript.generator.emitter.TsMethodModel)

Example 10 with TsBeanModel

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

the class ModelCompiler method createRestInterfaces.

private void createRestInterfaces(TsModel tsModel, SymbolTable symbolTable, List<RestApplicationModel> restApplications, Symbol responseSymbol, TsType.GenericVariableType optionsGenericVariable, TsType optionsType) {
    final List<TsType.GenericVariableType> typeParameters = Utils.listFromNullable(optionsGenericVariable);
    final Map<Symbol, List<TsMethodModel>> groupedMethods = processRestMethods(tsModel, restApplications, symbolTable, null, responseSymbol, optionsType, false);
    for (Map.Entry<Symbol, List<TsMethodModel>> entry : groupedMethods.entrySet()) {
        final TsBeanModel interfaceModel = new TsBeanModel(null, TsBeanCategory.Service, false, entry.getKey(), typeParameters, null, null, null, null, null, entry.getValue(), null);
        tsModel.getBeans().add(interfaceModel);
    }
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

TsBeanModel (cz.habarta.typescript.generator.emitter.TsBeanModel)29 ArrayList (java.util.ArrayList)19 TsType (cz.habarta.typescript.generator.TsType)13 TsPropertyModel (cz.habarta.typescript.generator.emitter.TsPropertyModel)12 TsMethodModel (cz.habarta.typescript.generator.emitter.TsMethodModel)7 TsModel (cz.habarta.typescript.generator.emitter.TsModel)7 TsIdentifierReference (cz.habarta.typescript.generator.emitter.TsIdentifierReference)5 TsParameterModel (cz.habarta.typescript.generator.emitter.TsParameterModel)5 List (java.util.List)5 TsCallExpression (cz.habarta.typescript.generator.emitter.TsCallExpression)4 TsConstructorModel (cz.habarta.typescript.generator.emitter.TsConstructorModel)4 TsMemberExpression (cz.habarta.typescript.generator.emitter.TsMemberExpression)4 TsStringLiteral (cz.habarta.typescript.generator.emitter.TsStringLiteral)4 BeanModel (cz.habarta.typescript.generator.parser.BeanModel)4 ModelTransformer (cz.habarta.typescript.generator.compiler.ModelTransformer)3 SymbolTable (cz.habarta.typescript.generator.compiler.SymbolTable)3 TsEnumModel (cz.habarta.typescript.generator.emitter.TsEnumModel)3 TsStatement (cz.habarta.typescript.generator.emitter.TsStatement)3 LinkedHashSet (java.util.LinkedHashSet)3 TsProperty (cz.habarta.typescript.generator.TsProperty)2