Search in sources :

Example 1 with TSType

use of io.crnk.gen.typescript.model.TSType in project crnk-framework by crnk-project.

the class TSMetaDataObjectTransformation method setupParent.

private static void setupParent(TSMetaTransformationContext context, TSInterfaceType interfaceType, MetaDataObject metaDataObject) {
    TSContainerElement parent = null;
    // move links and meta information to the resource itself
    boolean isMeta = TypescriptUtils.isInstance(metaDataObject.getImplementationClass(), "io.crnk.core.resource.meta.MetaInformation");
    boolean isLinks = TypescriptUtils.isInstance(metaDataObject.getImplementationClass(), "io.crnk.core.resource.links.LinksInformation");
    if ((isMeta || isLinks) && metaDataObject.getImplementationClass().getEnclosingClass() != null) {
        MetaElement enclosingMeta = context.getMeta(metaDataObject.getImplementationClass().getEnclosingClass());
        if (enclosingMeta instanceof MetaResource) {
            TSType enclosingType = (TSType) context.transform(enclosingMeta, TSMetaTransformationOptions.EMPTY);
            TSModule module = TypescriptUtils.getNestedTypeContainer(enclosingType, true);
            interfaceType.setName(isLinks ? "Links" : "Meta");
            parent = module;
        }
    }
    if (parent == null) {
        TSSource source = new TSSource();
        source.setName(TypescriptUtils.toFileName(metaDataObject.getName()));
        source.setNpmPackage(context.getNpmPackage(metaDataObject));
        source.setDirectory(context.getDirectory(metaDataObject));
        context.addSource(source);
        parent = source;
    }
    parent.getElements().add(interfaceType);
    interfaceType.setParent(parent);
}
Also used : TSContainerElement(io.crnk.gen.typescript.model.TSContainerElement) TSModule(io.crnk.gen.typescript.model.TSModule) MetaElement(io.crnk.meta.model.MetaElement) MetaResource(io.crnk.meta.model.resource.MetaResource) TSSource(io.crnk.gen.typescript.model.TSSource) TSType(io.crnk.gen.typescript.model.TSType)

Example 2 with TSType

use of io.crnk.gen.typescript.model.TSType in project crnk-framework by crnk-project.

the class TSMetaResourceRepositoryTransformation method transform.

@Override
public TSElement transform(MetaElement element, TSMetaTransformationContext context, TSMetaTransformationOptions options) {
    MetaResourceRepository metaRepository = (MetaResourceRepository) element;
    MetaResource metaResource = metaRepository.getResourceType();
    TSType resourceType = context.transform(metaResource, TSMetaTransformationOptions.EMPTY).asType();
    TSContainerElement parent = (TSContainerElement) resourceType.getParent();
    MetaDataObject metaListLinks = metaRepository.getListLinksType();
    MetaDataObject metaListMeta = metaRepository.getListMetaType();
    TSInterfaceType oneResultType = new TSInterfaceType();
    oneResultType.setName(metaResource.getName() + "Result");
    oneResultType.setExported(true);
    oneResultType.getImplementedInterfaces().add(NgrxJsonApiLibrary.ONE_QUERY_RESULT);
    oneResultType.addDeclaredMember(newDataField(context, resourceType, false));
    parent.addElement(oneResultType);
    TSInterfaceType manyResultType = new TSInterfaceType();
    manyResultType.setName(metaResource.getName() + "ListResult");
    manyResultType.setExported(true);
    manyResultType.getImplementedInterfaces().add(NgrxJsonApiLibrary.MANY_QUERY_RESULT);
    manyResultType.addDeclaredMember(newDataField(context, resourceType, true));
    parent.addElement(manyResultType);
    if (metaListLinks != null) {
        TSMetaTransformationOptions listOptions = new TSMetaTransformationOptions();
        listOptions.setParent(TypescriptUtils.getNestedTypeContainer(manyResultType, true));
        TSType linksType = context.transform(metaListLinks, listOptions).asType();
        TSField field = new TSField();
        field.setName("links");
        field.setNullable(true);
        field.setType(linksType);
        manyResultType.addDeclaredMember(field);
    }
    if (metaListMeta != null) {
        TSMetaTransformationOptions listOptions = new TSMetaTransformationOptions();
        listOptions.setParent(TypescriptUtils.getNestedTypeContainer(manyResultType, true));
        TSType metaType = context.transform(metaListMeta, listOptions).asType();
        TSField field = new TSField();
        field.setName("meta");
        field.setNullable(true);
        field.setType(metaType);
        manyResultType.addDeclaredMember(field);
    }
    return null;
}
Also used : TSField(io.crnk.gen.typescript.model.TSField) TSContainerElement(io.crnk.gen.typescript.model.TSContainerElement) MetaResourceRepository(io.crnk.meta.model.resource.MetaResourceRepository) MetaDataObject(io.crnk.meta.model.MetaDataObject) MetaResource(io.crnk.meta.model.resource.MetaResource) TSType(io.crnk.gen.typescript.model.TSType) TSInterfaceType(io.crnk.gen.typescript.model.TSInterfaceType)

Example 3 with TSType

use of io.crnk.gen.typescript.model.TSType in project crnk-framework by crnk-project.

the class TSMetaDataObjectTransformation method generateResourceField.

private static void generateResourceField(MetaAttribute attr, TSMetaTransformationContext context, TSInterfaceType interfaceType, TSInterfaceType attributesType, TSInterfaceType relationshipsType) {
    MetaType metaElementType = attr.getType().getElementType();
    TSType elementType = (TSType) context.transform(metaElementType, TSMetaTransformationOptions.EMPTY);
    TSField field = new TSField();
    field.setName(attr.getName());
    field.setType(elementType);
    field.setNullable(true);
    if (attr.isAssociation()) {
        TSType relationshipType = attr.getType().isCollection() ? NgrxJsonApiLibrary.TYPED_MANY_RESOURCE_RELATIONSHIP : NgrxJsonApiLibrary.TYPED_ONE_RESOURCE_RELATIONSHIP;
        field.setType(new TSParameterizedType(relationshipType, elementType));
        relationshipsType.getDeclaredMembers().add(field);
        field.setParent(relationshipsType);
    } else if (attr instanceof MetaResourceField && ((MetaResourceField) attr).isMeta()) {
        field.setName("meta");
        interfaceType.getDeclaredMembers().add(field);
        field.setParent(interfaceType);
    } else if (attr instanceof MetaResourceField && ((MetaResourceField) attr).isLinks()) {
        field.setName("links");
        interfaceType.getDeclaredMembers().add(field);
        field.setParent(interfaceType);
    } else {
        attributesType.getDeclaredMembers().add(field);
        field.setParent(attributesType);
    }
}
Also used : MetaResourceField(io.crnk.meta.model.resource.MetaResourceField) TSField(io.crnk.gen.typescript.model.TSField) TSParameterizedType(io.crnk.gen.typescript.model.TSParameterizedType) TSType(io.crnk.gen.typescript.model.TSType) MetaType(io.crnk.meta.model.MetaType)

Example 4 with TSType

use of io.crnk.gen.typescript.model.TSType in project crnk-framework by crnk-project.

the class TSWriter method visitReference.

public void visitReference(TSType type) {
    if (type instanceof TSParameterizedType) {
        TSParameterizedType paramType = (TSParameterizedType) type;
        visitReference(paramType.getBaseType());
        builder.append("<");
        List<TSType> parameters = paramType.getParameters();
        for (int i = 0; i < parameters.size(); i++) {
            if (i > 0) {
                builder.append(", ");
            }
            visitReference(parameters.get(i));
        }
        builder.append(">");
    } else {
        if (type.getParent() instanceof TSModule) {
            visitReference((TSModule) type.getParent());
            builder.append(".");
        }
        builder.append(type.getName());
    }
}
Also used : TSParameterizedType(io.crnk.gen.typescript.model.TSParameterizedType) TSModule(io.crnk.gen.typescript.model.TSModule) TSType(io.crnk.gen.typescript.model.TSType)

Example 5 with TSType

use of io.crnk.gen.typescript.model.TSType in project crnk-framework by crnk-project.

the class TSImportProcessor method transform.

private static void transform(TSSource source) {
    TSTypeReferenceResolver refResolver = new TSTypeReferenceResolver();
    refResolver.accept(source);
    for (TSType type : refResolver.getTypes()) {
        processType(source, type);
    }
}
Also used : TSTypeReferenceResolver(io.crnk.gen.typescript.writer.TSTypeReferenceResolver) TSType(io.crnk.gen.typescript.model.TSType)

Aggregations

TSType (io.crnk.gen.typescript.model.TSType)5 TSContainerElement (io.crnk.gen.typescript.model.TSContainerElement)2 TSField (io.crnk.gen.typescript.model.TSField)2 TSModule (io.crnk.gen.typescript.model.TSModule)2 TSParameterizedType (io.crnk.gen.typescript.model.TSParameterizedType)2 MetaResource (io.crnk.meta.model.resource.MetaResource)2 TSInterfaceType (io.crnk.gen.typescript.model.TSInterfaceType)1 TSSource (io.crnk.gen.typescript.model.TSSource)1 TSTypeReferenceResolver (io.crnk.gen.typescript.writer.TSTypeReferenceResolver)1 MetaDataObject (io.crnk.meta.model.MetaDataObject)1 MetaElement (io.crnk.meta.model.MetaElement)1 MetaType (io.crnk.meta.model.MetaType)1 MetaResourceField (io.crnk.meta.model.resource.MetaResourceField)1 MetaResourceRepository (io.crnk.meta.model.resource.MetaResourceRepository)1