Search in sources :

Example 1 with TSModule

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

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

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

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

Example 5 with TSModule

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

the class TypescriptUtils method getModule.

public static TSModule getModule(TSContainerElement parent, String name, int insertionIndex, boolean create) {
    for (TSElement element : parent.getElements()) {
        if (element instanceof TSModule && ((TSModule) element).getName().equals(name)) {
            return (TSModule) element;
        }
    }
    if (create) {
        TSModule module = new TSModule();
        module.setName(name);
        module.setParent(parent);
        module.setExported(true);
        // module needs to come before type definition
        parent.getElements().add(insertionIndex, module);
        return module;
    } else {
        return null;
    }
}
Also used : TSModule(io.crnk.gen.typescript.model.TSModule) TSElement(io.crnk.gen.typescript.model.TSElement)

Aggregations

TSModule (io.crnk.gen.typescript.model.TSModule)8 TSInterfaceType (io.crnk.gen.typescript.model.TSInterfaceType)4 TSClassType (io.crnk.gen.typescript.model.TSClassType)2 TSElement (io.crnk.gen.typescript.model.TSElement)2 TSSource (io.crnk.gen.typescript.model.TSSource)2 TSType (io.crnk.gen.typescript.model.TSType)2 Test (org.junit.Test)2 TSContainerElement (io.crnk.gen.typescript.model.TSContainerElement)1 TSField (io.crnk.gen.typescript.model.TSField)1 TSIndexSignature (io.crnk.gen.typescript.model.TSIndexSignature)1 TSNamedElement (io.crnk.gen.typescript.model.TSNamedElement)1 TSParameterizedType (io.crnk.gen.typescript.model.TSParameterizedType)1 TSPrimitiveType (io.crnk.gen.typescript.model.TSPrimitiveType)1 MetaAttribute (io.crnk.meta.model.MetaAttribute)1 MetaElement (io.crnk.meta.model.MetaElement)1 MetaKey (io.crnk.meta.model.MetaKey)1 MetaResource (io.crnk.meta.model.resource.MetaResource)1