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;
}
}
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);
}
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;
}
}
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;
}
}
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;
}
Aggregations