Search in sources :

Example 6 with TSInterfaceType

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

the class TypescriptUtilsTest method getNestedInterfacesReturnsNullIfDoesNotExistsAndNonCreateRequested.

@Test
public void getNestedInterfacesReturnsNullIfDoesNotExistsAndNonCreateRequested() {
    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", false);
    Assert.assertNull(testInterface);
}
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 7 with TSInterfaceType

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

the class TypescriptUtilsTest method getNestedInterfacesReturnsNullIfNoParent.

@Test
public void getNestedInterfacesReturnsNullIfNoParent() {
    TSClassType classType = new TSClassType();
    classType.setName("TestClass");
    TSInterfaceType testInterface = TypescriptUtils.getNestedInterface(classType, "TestInterface", false);
    Assert.assertNull(testInterface);
}
Also used : TSClassType(io.crnk.gen.typescript.model.TSClassType) TSInterfaceType(io.crnk.gen.typescript.model.TSInterfaceType) Test(org.junit.Test)

Example 8 with TSInterfaceType

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

the class TSMetaDataObjectTransformation method transform.

@Override
public TSElement transform(MetaElement element, TSMetaTransformationContext context, TSMetaTransformationOptions options) {
    MetaDataObject metaDataObject = (MetaDataObject) element;
    TSInterfaceType interfaceType = new TSInterfaceType();
    interfaceType.setName(metaDataObject.getName());
    interfaceType.setExported(true);
    if (metaDataObject instanceof MetaResource) {
        MetaResource metaResource = (MetaResource) metaDataObject;
        String resourceType = metaResource.getResourceType();
        interfaceType.setPrivateData(TSMetaDataObjectTransformation.PRIVATE_DATA_RESOURCE_TYPE, resourceType);
    }
    interfaceType.setPrivateData(TSMetaDataObjectTransformation.PRIVATE_DATA_META_ELEMENT_ID, element.getId());
    context.putMapping(metaDataObject, interfaceType);
    if (options.getParent() == null) {
        setupParent(context, interfaceType, metaDataObject);
    } else {
        options.getParent().addElement(interfaceType);
    }
    if (generateAsResource(metaDataObject)) {
        if (metaDataObject.getSuperType() == null) {
            interfaceType.getImplementedInterfaces().add(NgrxJsonApiLibrary.STORE_RESOURCE);
        } else {
            // trigger generation of super type, fully attach during post processing
            MetaDataObject superType = metaDataObject.getSuperType();
            if (generateAsResource(superType)) {
                TSElement superInterface = context.transform(superType, TSMetaTransformationOptions.EMPTY);
                interfaceType.getImplementedInterfaces().add((TSInterfaceType) superInterface);
            }
        }
        generateResourceFields(context, interfaceType, metaDataObject);
    } else {
        if (metaDataObject.getSuperType() != null) {
            TSInterfaceType superInterface = (TSInterfaceType) context.transform(metaDataObject.getSuperType(), TSMetaTransformationOptions.EMPTY);
            interfaceType.getImplementedInterfaces().add(superInterface);
        }
        for (MetaDataObject interfaceMeta : metaDataObject.getInterfaces()) {
            TSInterfaceType implementedinterfaceType = (TSInterfaceType) context.transform(interfaceMeta, TSMetaTransformationOptions.EMPTY);
            interfaceType.getImplementedInterfaces().add(implementedinterfaceType);
        }
        generateAttributes(context, interfaceType, metaDataObject);
    }
    for (MetaDataObject subType : metaDataObject.getSubTypes()) {
        context.transform(subType, TSMetaTransformationOptions.EMPTY);
    }
    return interfaceType;
}
Also used : MetaDataObject(io.crnk.meta.model.MetaDataObject) MetaResource(io.crnk.meta.model.resource.MetaResource) TSInterfaceType(io.crnk.gen.typescript.model.TSInterfaceType) TSElement(io.crnk.gen.typescript.model.TSElement)

Example 9 with TSInterfaceType

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

the class TSMetaDataObjectTransformation method setAttributeSuperType.

private void setAttributeSuperType(TSInterfaceType interfaceType, MetaDataObject metaDataObject, TSMetaTransformationContext context) {
    // iterate over super types till (non-empty) one is found with a attributes 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 attributesType = TypescriptUtils.getNestedInterface(interfaceType, ATTRIBUTES_CLASS_NAME, false);
        if (attributesType != null) {
            TSInterfaceType superAttributeType = TypescriptUtils.getNestedInterface(superInterface, ATTRIBUTES_CLASS_NAME, false);
            if (superAttributeType != null) {
                attributesType.getImplementedInterfaces().add(superAttributeType);
                break;
            }
        }
        current = superType;
    }
}
Also used : MetaDataObject(io.crnk.meta.model.MetaDataObject) TSInterfaceType(io.crnk.gen.typescript.model.TSInterfaceType)

Example 10 with TSInterfaceType

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

the class TSMetaDataObjectTransformation method postTransform.

@Override
public void postTransform(TSElement element, TSMetaTransformationContext context) {
    if (element instanceof TSInterfaceType) {
        TSInterfaceType interfaceType = (TSInterfaceType) element;
        String metaId = interfaceType.getPrivateData(TSMetaDataObjectTransformation.PRIVATE_DATA_META_ELEMENT_ID, String.class);
        MetaElement metaElement = context.getMeta(metaId);
        if (metaElement instanceof MetaDataObject) {
            MetaDataObject metaDataObject = (MetaDataObject) metaElement;
            if (generateAsResource(metaDataObject)) {
                // link to super type, only available once all fields are initialized
                MetaDataObject superType = metaDataObject.getSuperType();
                if (superType != null) {
                    setAttributeSuperType(interfaceType, metaDataObject, context);
                    setRelationshipsSuperType(interfaceType, metaDataObject, context);
                }
            }
        }
    }
}
Also used : MetaElement(io.crnk.meta.model.MetaElement) MetaDataObject(io.crnk.meta.model.MetaDataObject) 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