Search in sources :

Example 1 with TsPropertyModel

use of cz.habarta.typescript.generator.emitter.TsPropertyModel 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 TsPropertyModel

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

the class RequiredPropertyConstructorExtension method createConstructor.

private static Optional<TsConstructorModel> createConstructor(TsBeanModel bean, TsModel model, Map<String, TsConstructorModel> generatedConstructors) {
    List<TsParameterModel> parameters = new ArrayList<>();
    List<TsParameterModel> optionalParameters = new ArrayList<>();
    List<TsStatement> body = new ArrayList<>();
    TsType parent = bean.getParent();
    if (parent != null) {
        if (!(parent instanceof TsType.ReferenceType)) {
            throw new IllegalStateException("Generating constructor for non-reference parent types is not currently supported");
        }
        TsType.ReferenceType referenceParent = (TsType.ReferenceType) parent;
        TsConstructorModel parentConstructor = generatedConstructors.get(referenceParent.symbol.getFullName());
        if (parentConstructor == null) {
            throw new IllegalStateException("Generating constructor for class with non-generated constructor is not currently supported");
        }
        List<TsParameterModel> parentParameters = parentConstructor.getParameters();
        TsIdentifierReference[] callParameters = new TsIdentifierReference[parentParameters.size()];
        int i = 0;
        for (TsParameterModel parentParameter : parentParameters) {
            List<TsParameterModel> targetParameterList = parentParameter.tsType instanceof TsType.OptionalType ? optionalParameters : parameters;
            targetParameterList.add(parentParameter);
            callParameters[i] = new TsIdentifierReference(parentParameter.name);
            i++;
        }
        body.add(new TsExpressionStatement(new TsCallExpression(new TsSuperExpression(), callParameters)));
    }
    for (TsPropertyModel property : bean.getProperties()) {
        if (!property.modifiers.isReadonly) {
            continue;
        }
        TsExpression assignmentExpression;
        Optional<TsExpression> predefinedValue = getPredefinedValueForProperty(property, model);
        if (predefinedValue.isPresent()) {
            assignmentExpression = predefinedValue.get();
        } else {
            TsParameterModel parameter = new TsParameterModel(property.name, property.tsType);
            List<TsParameterModel> targetParameterList = property.tsType instanceof TsType.OptionalType ? optionalParameters : parameters;
            targetParameterList.add(parameter);
            assignmentExpression = new TsIdentifierReference(property.name);
        }
        TsMemberExpression leftHandSideExpression = new TsMemberExpression(new TsThisExpression(), property.name);
        TsExpression assignment = new TsAssignmentExpression(leftHandSideExpression, assignmentExpression);
        TsExpressionStatement assignmentStatement = new TsExpressionStatement(assignment);
        body.add(assignmentStatement);
    }
    parameters.addAll(optionalParameters);
    if (parameters.isEmpty() && body.isEmpty()) {
        return Optional.empty();
    }
    TsConstructorModel constructor = new TsConstructorModel(TsModifierFlags.None, parameters, body, null);
    return Optional.of(constructor);
}
Also used : TsStatement(cz.habarta.typescript.generator.emitter.TsStatement) TsMemberExpression(cz.habarta.typescript.generator.emitter.TsMemberExpression) TsThisExpression(cz.habarta.typescript.generator.emitter.TsThisExpression) TsExpression(cz.habarta.typescript.generator.emitter.TsExpression) TsSuperExpression(cz.habarta.typescript.generator.emitter.TsSuperExpression) ArrayList(java.util.ArrayList) TsPropertyModel(cz.habarta.typescript.generator.emitter.TsPropertyModel) TsType(cz.habarta.typescript.generator.TsType) TsCallExpression(cz.habarta.typescript.generator.emitter.TsCallExpression) TsAssignmentExpression(cz.habarta.typescript.generator.emitter.TsAssignmentExpression) TsConstructorModel(cz.habarta.typescript.generator.emitter.TsConstructorModel) TsIdentifierReference(cz.habarta.typescript.generator.emitter.TsIdentifierReference) TsExpressionStatement(cz.habarta.typescript.generator.emitter.TsExpressionStatement) TsParameterModel(cz.habarta.typescript.generator.emitter.TsParameterModel)

Example 3 with TsPropertyModel

use of cz.habarta.typescript.generator.emitter.TsPropertyModel 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 4 with TsPropertyModel

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

the class ModelCompiler method removeInheritedProperties.

private TsModel removeInheritedProperties(SymbolTable symbolTable, TsModel tsModel) {
    final List<TsBeanModel> beans = new ArrayList<>();
    for (TsBeanModel bean : tsModel.getBeans()) {
        final Map<String, TsType> inheritedPropertyTypes = getInheritedProperties(symbolTable, tsModel, bean.getAllParents());
        final List<TsPropertyModel> properties = new ArrayList<>();
        for (TsPropertyModel property : bean.getProperties()) {
            if (property.isOwnProperty() || !Objects.equals(property.getTsType(), inheritedPropertyTypes.get(property.getName()))) {
                properties.add(property);
            }
        }
        beans.add(bean.withProperties(properties));
    }
    return tsModel.withBeans(beans);
}
Also used : ArrayList(java.util.ArrayList) TsPropertyModel(cz.habarta.typescript.generator.emitter.TsPropertyModel) TsType(cz.habarta.typescript.generator.TsType) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel)

Example 5 with TsPropertyModel

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

the class ModelCompiler method getImplementedProperties.

private static List<TsPropertyModel> getImplementedProperties(SymbolTable symbolTable, TsModel tsModel, List<TsType> interfaces) {
    final List<TsPropertyModel> properties = new ArrayList<>();
    for (TsType aInterface : interfaces) {
        final TsBeanModel bean = tsModel.getBean(getOriginClass(symbolTable, aInterface));
        if (bean != null) {
            properties.addAll(getImplementedProperties(symbolTable, tsModel, bean.getExtendsList()));
            properties.addAll(bean.getProperties());
        }
    }
    return properties;
}
Also used : TsPropertyModel(cz.habarta.typescript.generator.emitter.TsPropertyModel) ArrayList(java.util.ArrayList) TsType(cz.habarta.typescript.generator.TsType) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel)

Aggregations

TsPropertyModel (cz.habarta.typescript.generator.emitter.TsPropertyModel)17 TsBeanModel (cz.habarta.typescript.generator.emitter.TsBeanModel)13 ArrayList (java.util.ArrayList)12 TsType (cz.habarta.typescript.generator.TsType)11 TsExpressionStatement (cz.habarta.typescript.generator.emitter.TsExpressionStatement)5 TsParameterModel (cz.habarta.typescript.generator.emitter.TsParameterModel)5 TsAssignmentExpression (cz.habarta.typescript.generator.emitter.TsAssignmentExpression)4 TsCallExpression (cz.habarta.typescript.generator.emitter.TsCallExpression)4 TsConstructorModel (cz.habarta.typescript.generator.emitter.TsConstructorModel)4 TsIdentifierReference (cz.habarta.typescript.generator.emitter.TsIdentifierReference)4 TsMemberExpression (cz.habarta.typescript.generator.emitter.TsMemberExpression)4 TsStatement (cz.habarta.typescript.generator.emitter.TsStatement)4 TsSuperExpression (cz.habarta.typescript.generator.emitter.TsSuperExpression)4 Symbol (cz.habarta.typescript.generator.compiler.Symbol)3 TsExpression (cz.habarta.typescript.generator.emitter.TsExpression)3 TsModel (cz.habarta.typescript.generator.emitter.TsModel)3 TsModifierFlags (cz.habarta.typescript.generator.emitter.TsModifierFlags)3 TsThisExpression (cz.habarta.typescript.generator.emitter.TsThisExpression)3 BeanModel (cz.habarta.typescript.generator.parser.BeanModel)3 List (java.util.List)3