Search in sources :

Example 1 with TSField

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

the class TSMetaDataObjectTransformation method generateResourceFields.

private static void generateResourceFields(TSMetaTransformationContext context, TSInterfaceType interfaceType, MetaDataObject meta) {
    TSInterfaceType attributesType = new TSInterfaceType();
    attributesType.setName(ATTRIBUTES_CLASS_NAME);
    attributesType.setExported(true);
    TSInterfaceType relationshipsType = new TSInterfaceType();
    relationshipsType.setName(RELATIONSHIPS_CLASS_NAME);
    relationshipsType.setExported(true);
    TSIndexSignature relationshipsIndexSignature = new TSIndexSignature();
    relationshipsIndexSignature.setKeyType(TSPrimitiveType.STRING);
    relationshipsIndexSignature.setValueType(NgrxJsonApiLibrary.RESOURCE_RELATIONSHIP);
    relationshipsIndexSignature.setParent(relationshipsType);
    relationshipsType.setIndexSignature(relationshipsIndexSignature);
    // TODO remo: interface support
    MetaKey primaryKey = meta.getPrimaryKey();
    for (MetaAttribute attr : meta.getDeclaredAttributes()) {
        if (primaryKey != null && primaryKey.getUniqueElement().equals(attr)) {
            continue;
        }
        generateResourceField(attr, context, interfaceType, attributesType, relationshipsType);
    }
    if (!isEmpty(relationshipsType)) {
        TSModule module = TypescriptUtils.getNestedTypeContainer(interfaceType, true);
        module.getElements().add(relationshipsType);
        relationshipsType.setParent(module);
        TSField relationshipsField = new TSField();
        relationshipsField.setName("relationships");
        relationshipsField.setType(relationshipsType);
        relationshipsField.setNullable(true);
        interfaceType.getDeclaredMembers().add(relationshipsField);
    }
    if (!isEmpty(attributesType)) {
        TSModule module = TypescriptUtils.getNestedTypeContainer(interfaceType, true);
        module.getElements().add(attributesType);
        attributesType.setParent(module);
        TSField attributesField = new TSField();
        attributesField.setName("attributes");
        attributesField.setType(attributesType);
        attributesField.setNullable(true);
        interfaceType.getDeclaredMembers().add(attributesField);
    }
}
Also used : TSIndexSignature(io.crnk.gen.typescript.model.TSIndexSignature) TSField(io.crnk.gen.typescript.model.TSField) MetaKey(io.crnk.meta.model.MetaKey) TSModule(io.crnk.gen.typescript.model.TSModule) MetaAttribute(io.crnk.meta.model.MetaAttribute) TSInterfaceType(io.crnk.gen.typescript.model.TSInterfaceType)

Example 2 with TSField

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

the class TSMetaDataObjectTransformation method generateAttributes.

private static void generateAttributes(TSMetaTransformationContext context, TSInterfaceType interfaceType, MetaDataObject element) {
    for (MetaAttribute attr : element.getDeclaredAttributes()) {
        MetaType elementType = attr.getType().getElementType();
        TSField field = new TSField();
        field.setName(attr.getName());
        field.setType((TSType) context.transform(elementType, TSMetaTransformationOptions.EMPTY));
        field.setNullable(true);
        interfaceType.getDeclaredMembers().add(field);
    }
}
Also used : TSField(io.crnk.gen.typescript.model.TSField) MetaAttribute(io.crnk.meta.model.MetaAttribute) MetaType(io.crnk.meta.model.MetaType)

Example 3 with TSField

use of io.crnk.gen.typescript.model.TSField 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 4 with TSField

use of io.crnk.gen.typescript.model.TSField 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 5 with TSField

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

the class TSMetaResourceRepositoryTransformation method newDataField.

private TSMember newDataField(TSMetaTransformationContext context, TSType resourceType, boolean isArray) {
    TSField field = new TSField();
    field.setName("data");
    field.setNullable(true);
    if (isArray) {
        field.setType(new TSArrayType(resourceType));
    } else {
        field.setType(resourceType);
    }
    return field;
}
Also used : TSField(io.crnk.gen.typescript.model.TSField) TSArrayType(io.crnk.gen.typescript.model.TSArrayType)

Aggregations

TSField (io.crnk.gen.typescript.model.TSField)5 TSInterfaceType (io.crnk.gen.typescript.model.TSInterfaceType)2 TSType (io.crnk.gen.typescript.model.TSType)2 MetaAttribute (io.crnk.meta.model.MetaAttribute)2 MetaType (io.crnk.meta.model.MetaType)2 TSArrayType (io.crnk.gen.typescript.model.TSArrayType)1 TSContainerElement (io.crnk.gen.typescript.model.TSContainerElement)1 TSIndexSignature (io.crnk.gen.typescript.model.TSIndexSignature)1 TSModule (io.crnk.gen.typescript.model.TSModule)1 TSParameterizedType (io.crnk.gen.typescript.model.TSParameterizedType)1 MetaDataObject (io.crnk.meta.model.MetaDataObject)1 MetaKey (io.crnk.meta.model.MetaKey)1 MetaResource (io.crnk.meta.model.resource.MetaResource)1 MetaResourceField (io.crnk.meta.model.resource.MetaResourceField)1 MetaResourceRepository (io.crnk.meta.model.resource.MetaResourceRepository)1