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