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