Search in sources :

Example 1 with TsBeanModel

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

the class CustomExtension method getTransformers.

@Override
public List<TransformerDefinition> getTransformers() {
    return Arrays.asList(new TransformerDefinition(ModelCompiler.TransformationPhase.BeforeEnums, (TsModelTransformer) (context, model) -> {
        // Special processing for AssetModelInfo meta item value descriptors as JsonSerialize extension doesn't support
        // @JsonSerialize(contentConverter=...)
        TsBeanModel assetTypeInfoBean = model.getBean(AssetTypeInfo.class);
        if (assetTypeInfoBean != null) {
            assetTypeInfoBean.getProperties().replaceAll(p -> p.getName().equals("metaItemDescriptors") || p.getName().equals("valueDescriptors") ? new TsPropertyModel(p.getName(), new TsType.BasicArrayType(TsType.String), p.modifiers, p.ownProperty, p.comments) : p);
        }
        Constants.IGNORE_TYPE_PARAMS_ON_CLASSES.forEach(beanClass -> {
            TsBeanModel bean = model.getBean(beanClass);
            if (bean != null && bean.getTypeParameters() != null) {
                // Remove the type parameter - this works in conjunction with the CustomTypeProcessor which replaces
                // field references
                bean.getTypeParameters().clear();
            }
        });
        return model;
    }));
}
Also used : TsPropertyModel(cz.habarta.typescript.generator.emitter.TsPropertyModel) TsType(cz.habarta.typescript.generator.TsType) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsModelTransformer(cz.habarta.typescript.generator.compiler.TsModelTransformer)

Example 2 with TsBeanModel

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

the class ModelCompiler method sortTypeDeclarations.

private TsModel sortTypeDeclarations(SymbolTable symbolTable, TsModel tsModel) {
    final List<TsBeanModel> beans = tsModel.getBeans();
    final List<TsAliasModel> aliases = tsModel.getTypeAliases();
    final List<TsEnumModel> enums = tsModel.getEnums();
    if (settings.sortDeclarations || settings.sortTypeDeclarations) {
        Collections.sort(beans);
        Collections.sort(aliases);
        Collections.sort(enums);
    }
    final LinkedHashSet<TsBeanModel> orderedBeans = new LinkedHashSet<>();
    for (TsBeanModel bean : beans) {
        addOrderedClass(symbolTable, tsModel, bean, orderedBeans);
    }
    return tsModel.withBeans(new ArrayList<>(orderedBeans)).withTypeAliases(aliases).withEnums(enums);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TsAliasModel(cz.habarta.typescript.generator.emitter.TsAliasModel) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsEnumModel(cz.habarta.typescript.generator.emitter.TsEnumModel)

Example 3 with TsBeanModel

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

the class ModelCompiler method createRestClients.

private void createRestClients(TsModel tsModel, SymbolTable symbolTable, List<RestApplicationModel> restApplications, Symbol responseSymbol, TsType.GenericVariableType optionsGenericVariable, TsType optionsType) {
    final Symbol httpClientSymbol = symbolTable.getSyntheticSymbol("HttpClient");
    final List<TsType.GenericVariableType> typeParameters = Utils.listFromNullable(optionsGenericVariable);
    // HttpClient interface
    final TsType.GenericVariableType returnGenericVariable = new TsType.GenericVariableType("R");
    tsModel.getBeans().add(new TsBeanModel(null, TsBeanCategory.ServicePrerequisite, false, httpClientSymbol, typeParameters, null, null, null, null, null, Arrays.asList(new TsMethodModel("request", TsModifierFlags.None, Arrays.asList(returnGenericVariable), Arrays.asList(new TsParameterModel("requestConfig", new TsType.ObjectType(new TsProperty("method", TsType.String), new TsProperty("url", TsType.String), new TsProperty("queryParams", new TsType.OptionalType(TsType.Any)), new TsProperty("data", new TsType.OptionalType(TsType.Any)), new TsProperty("copyFn", new TsType.OptionalType(new TsType.FunctionType(Arrays.asList(new TsParameter("data", returnGenericVariable)), returnGenericVariable))), optionsType != null ? new TsProperty("options", new TsType.OptionalType(optionsType)) : null))), new TsType.GenericReferenceType(responseSymbol, returnGenericVariable), null, null)), null));
    // application client classes
    final TsType.ReferenceType httpClientType = optionsGenericVariable != null ? new TsType.GenericReferenceType(httpClientSymbol, optionsGenericVariable) : new TsType.ReferenceType(httpClientSymbol);
    final TsConstructorModel constructor = new TsConstructorModel(TsModifierFlags.None, Arrays.asList(new TsParameterModel(TsAccessibilityModifier.Protected, "httpClient", httpClientType)), Collections.<TsStatement>emptyList(), null);
    final boolean bothInterfacesAndClients = settings.generateJaxrsApplicationInterface || settings.generateSpringApplicationInterface;
    final String groupingSuffix = bothInterfacesAndClients ? null : "Client";
    final Map<Symbol, List<TsMethodModel>> groupedMethods = processRestMethods(tsModel, restApplications, symbolTable, groupingSuffix, responseSymbol, optionsType, true);
    for (Map.Entry<Symbol, List<TsMethodModel>> entry : groupedMethods.entrySet()) {
        final Symbol symbol = bothInterfacesAndClients ? symbolTable.addSuffixToSymbol(entry.getKey(), "Client") : entry.getKey();
        final TsType interfaceType = bothInterfacesAndClients ? new TsType.ReferenceType(entry.getKey()) : null;
        final TsBeanModel clientModel = new TsBeanModel(null, TsBeanCategory.Service, true, symbol, typeParameters, null, null, Utils.listFromNullable(interfaceType), null, constructor, entry.getValue(), null);
        tsModel.getBeans().add(clientModel);
    }
    // helper
    tsModel.getHelpers().add(TsHelper.loadFromResource("/helpers/uriEncoding.ts"));
}
Also used : TsType(cz.habarta.typescript.generator.TsType) TsProperty(cz.habarta.typescript.generator.TsProperty) TsParameter(cz.habarta.typescript.generator.TsParameter) TsConstructorModel(cz.habarta.typescript.generator.emitter.TsConstructorModel) List(java.util.List) ArrayList(java.util.ArrayList) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsMethodModel(cz.habarta.typescript.generator.emitter.TsMethodModel) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TsParameterModel(cz.habarta.typescript.generator.emitter.TsParameterModel)

Example 4 with TsBeanModel

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

the class BeanPropertyPathExtension method writeBeanAndParentsFieldSpecs.

/**
 * Emits a bean and its parent beans before if needed.
 * Returns the list of beans that were emitted.
 */
private static Set<TsBeanModel> writeBeanAndParentsFieldSpecs(Writer writer, Settings settings, TsModel model, Set<TsBeanModel> emittedSoFar, TsBeanModel bean) {
    if (emittedSoFar.contains(bean)) {
        return new HashSet<>();
    }
    final TsBeanModel parentBean = getBeanModelByType(model, bean.getParent());
    final Set<TsBeanModel> emittedBeans = parentBean != null ? writeBeanAndParentsFieldSpecs(writer, settings, model, emittedSoFar, parentBean) : new HashSet<TsBeanModel>();
    final String parentClassName = parentBean != null ? getBeanModelClassName(parentBean) + "Fields" : "Fields";
    writer.writeIndentedLine("");
    writer.writeIndentedLine("class " + getBeanModelClassName(bean) + "Fields extends " + parentClassName + " {");
    writer.writeIndentedLine(settings.indentString + "constructor(parent?: Fields, name?: string) { super(parent, name); }");
    for (TsPropertyModel property : bean.getProperties()) {
        writeBeanProperty(writer, settings, model, bean, property);
    }
    writer.writeIndentedLine("}");
    emittedBeans.add(bean);
    return emittedBeans;
}
Also used : TsPropertyModel(cz.habarta.typescript.generator.emitter.TsPropertyModel) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) HashSet(java.util.HashSet)

Example 5 with TsBeanModel

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

the class BeanPropertyPathExtension method writeBeanProperty.

private static void writeBeanProperty(Writer writer, Settings settings, TsModel model, TsBeanModel bean, TsPropertyModel property) {
    TsBeanModel fieldBeanModel = getBeanModelByType(model, property.getTsType());
    String fieldClassName = fieldBeanModel != null ? getBeanModelClassName(fieldBeanModel) : "";
    // if a class has a field of its own type, we get stackoverflow exception
    if (fieldClassName.equals(bean.getName().getSimpleName())) {
        fieldClassName = "";
    }
    writer.writeIndentedLine(settings.indentString + property.getName() + " = new " + fieldClassName + "Fields(this, \"" + property.getName() + "\");");
}
Also used : TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel)

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