Search in sources :

Example 1 with TSElement

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

the class TSGenerator method transformMetaToTypescript.

public void transformMetaToTypescript() {
    Collection<MetaElement> elements = lookup.getMetaById().values();
    LOGGER.debug("transforming {} elements", elements.size());
    for (MetaElement element : elements) {
        boolean isRoot = isRoot(element);
        boolean isGenerated = isGenerated(element);
        if (isRoot && isGenerated) {
            LOGGER.debug("transforming {}", element.getId());
            transform(element, TSMetaTransformationOptions.EMPTY);
        } else {
            LOGGER.debug("ignoring {}, root={}, generated={}", element.getId(), isRoot, isGenerated);
        }
    }
    try {
        postProcessing = true;
        for (TSElement transformedElement : new ArrayList<>(transformedElements)) {
            for (TSMetaTransformation transformation : transformations) {
                transformation.postTransform(transformedElement, createMetaTransformationContext());
            }
        }
    } finally {
        postProcessing = false;
    }
}
Also used : MetaElement(io.crnk.meta.model.MetaElement) ArrayList(java.util.ArrayList) TSMetaTransformation(io.crnk.gen.typescript.transform.TSMetaTransformation) TSElement(io.crnk.gen.typescript.model.TSElement)

Example 2 with TSElement

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

the class TSGenerator method transform.

protected TSElement transform(MetaElement element, TSMetaTransformationOptions options) {
    if (elementSourceMap.containsKey(element)) {
        return elementSourceMap.get(element);
    }
    if (postProcessing) {
        throw new IllegalStateException("cannot add further element while post processing: " + element.getId());
    }
    for (TSMetaTransformation transformation : transformations) {
        if (transformation.accepts(element)) {
            TSElement tsElement = transformation.transform(element, createMetaTransformationContext(), options);
            transformedElements.add(tsElement);
            return tsElement;
        }
    }
    throw new IllegalStateException("unexpected element: " + element);
}
Also used : TSMetaTransformation(io.crnk.gen.typescript.transform.TSMetaTransformation) TSElement(io.crnk.gen.typescript.model.TSElement)

Example 3 with TSElement

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

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

Example 5 with TSElement

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

Aggregations

TSElement (io.crnk.gen.typescript.model.TSElement)6 TSInterfaceType (io.crnk.gen.typescript.model.TSInterfaceType)2 TSModule (io.crnk.gen.typescript.model.TSModule)2 TSMetaTransformation (io.crnk.gen.typescript.transform.TSMetaTransformation)2 MetaDataObject (io.crnk.meta.model.MetaDataObject)1 MetaElement (io.crnk.meta.model.MetaElement)1 MetaResource (io.crnk.meta.model.resource.MetaResource)1 ArrayList (java.util.ArrayList)1