Search in sources :

Example 1 with TSInterfaceType

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

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

the class TSMetaDataObjectTransformation method setRelationshipsSuperType.

private void setRelationshipsSuperType(TSInterfaceType interfaceType, MetaDataObject metaDataObject, TSMetaTransformationContext context) {
    // iterate over super types till (non-empty) one is found with a relationship interface definition
    MetaDataObject current = metaDataObject;
    while (current.getSuperType() != null && generateAsResource(current.getSuperType())) {
        MetaDataObject superType = current.getSuperType();
        TSInterfaceType superInterface = (TSInterfaceType) context.transform(superType, TSMetaTransformationOptions.EMPTY);
        TSInterfaceType relationshipsType = TypescriptUtils.getNestedInterface(interfaceType, RELATIONSHIPS_CLASS_NAME, false);
        if (relationshipsType != null) {
            TSInterfaceType superRelationshipType = TypescriptUtils.getNestedInterface(superInterface, RELATIONSHIPS_CLASS_NAME, false);
            if (superRelationshipType != null) {
                relationshipsType.getImplementedInterfaces().add(superRelationshipType);
                break;
            }
        }
        current = superType;
    }
}
Also used : MetaDataObject(io.crnk.meta.model.MetaDataObject) TSInterfaceType(io.crnk.gen.typescript.model.TSInterfaceType)

Example 3 with TSInterfaceType

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

the class TypescriptUtilsTest method getNestedInterfaesCreatesNewInterface.

@Test
public void getNestedInterfaesCreatesNewInterface() {
    TSModule module = new TSModule();
    module.setName("TestModule");
    TSClassType classType = new TSClassType();
    classType.setParent(module);
    classType.setName("TestClass");
    module.getElements().add(classType);
    TSInterfaceType testInterface = TypescriptUtils.getNestedInterface(classType, "TestInterface", true);
    Assert.assertEquals("TestInterface", testInterface.getName());
    Assert.assertTrue(testInterface.getParent() instanceof TSModule);
    Assert.assertEquals(module, testInterface.getParent().getParent());
    Assert.assertEquals("TestClass", ((TSModule) testInterface.getParent()).getName());
    Assert.assertEquals(2, module.getElements().size());
}
Also used : TSModule(io.crnk.gen.typescript.model.TSModule) TSClassType(io.crnk.gen.typescript.model.TSClassType) TSInterfaceType(io.crnk.gen.typescript.model.TSInterfaceType) Test(org.junit.Test)

Example 4 with TSInterfaceType

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

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

the class TypescriptUtils method getNestedInterface.

/**
 * Creates a nested interface for the given type (using a module with the same name as the type).
 */
public static TSInterfaceType getNestedInterface(TSType type, String name, boolean create) {
    TSModule module = getNestedTypeContainer(type, create);
    if (module == null && !create) {
        return null;
    } else if (module == null) {
        throw new IllegalStateException("cannot setup interface as no parent container is available");
    }
    for (TSElement element : module.getElements()) {
        if (element instanceof TSInterfaceType && ((TSInterfaceType) element).getName().equals(name)) {
            return (TSInterfaceType) element;
        }
    }
    if (create) {
        TSInterfaceType interfaceType = new TSInterfaceType();
        interfaceType.setExported(true);
        interfaceType.setName(name);
        interfaceType.setParent(module);
        module.getElements().add(interfaceType);
        return interfaceType;
    } else {
        return null;
    }
}
Also used : TSModule(io.crnk.gen.typescript.model.TSModule) TSElement(io.crnk.gen.typescript.model.TSElement) TSInterfaceType(io.crnk.gen.typescript.model.TSInterfaceType)

Aggregations

TSInterfaceType (io.crnk.gen.typescript.model.TSInterfaceType)11 MetaDataObject (io.crnk.meta.model.MetaDataObject)5 TSModule (io.crnk.gen.typescript.model.TSModule)4 TSClassType (io.crnk.gen.typescript.model.TSClassType)3 Test (org.junit.Test)3 TSElement (io.crnk.gen.typescript.model.TSElement)2 TSField (io.crnk.gen.typescript.model.TSField)2 MetaResource (io.crnk.meta.model.resource.MetaResource)2 TSContainerElement (io.crnk.gen.typescript.model.TSContainerElement)1 TSIndexSignature (io.crnk.gen.typescript.model.TSIndexSignature)1 TSType (io.crnk.gen.typescript.model.TSType)1 MetaAttribute (io.crnk.meta.model.MetaAttribute)1 MetaElement (io.crnk.meta.model.MetaElement)1 MetaKey (io.crnk.meta.model.MetaKey)1 MetaResourceRepository (io.crnk.meta.model.resource.MetaResourceRepository)1 ArrayList (java.util.ArrayList)1