Search in sources :

Example 1 with TSEnumLiteral

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

the class TSMetaEnumTypeTransformation method transform.

@Override
public TSElement transform(MetaElement elementObj, TSMetaTransformationContext context, TSMetaTransformationOptions options) {
    MetaEnumType element = (MetaEnumType) elementObj;
    TSSource source = new TSSource();
    source.setName(TypescriptUtils.toFileName(element.getName()));
    source.setNpmPackage(context.getNpmPackage(element));
    source.setDirectory(context.getDirectory(element));
    Class<?> implementationClass = element.getImplementationClass();
    TSEnumType enumType = new TSEnumType();
    enumType.setExported(true);
    enumType.setParent(source);
    enumType.setName(element.getName());
    for (Object literal : implementationClass.getEnumConstants()) {
        enumType.getLiterals().add(new TSEnumLiteral(literal.toString()));
    }
    source.getElements().add(enumType);
    context.putMapping(element, enumType);
    context.addSource(source);
    return enumType;
}
Also used : TSEnumLiteral(io.crnk.gen.typescript.model.TSEnumLiteral) TSSource(io.crnk.gen.typescript.model.TSSource) MetaEnumType(io.crnk.meta.model.MetaEnumType) TSEnumType(io.crnk.gen.typescript.model.TSEnumType)

Example 2 with TSEnumLiteral

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

the class TSWriter method visit.

@Override
public void visit(TSEnumType element) {
    // we make use of string literal types since enums are number-based in Typescript
    appendLine();
    appendExported(element);
    builder.append("type ");
    builder.append(element.getName());
    builder.append(" = ");
    List<TSEnumLiteral> literals = element.getLiterals();
    for (int i = 0; i < literals.size(); i++) {
        if (i > 0) {
            builder.append(" | ");
        }
        TSEnumLiteral literal = literals.get(i);
        builder.append('\'');
        builder.append(literal.getValue());
        builder.append('\'');
    }
    builder.append(";");
}
Also used : TSEnumLiteral(io.crnk.gen.typescript.model.TSEnumLiteral)

Aggregations

TSEnumLiteral (io.crnk.gen.typescript.model.TSEnumLiteral)2 TSEnumType (io.crnk.gen.typescript.model.TSEnumType)1 TSSource (io.crnk.gen.typescript.model.TSSource)1 MetaEnumType (io.crnk.meta.model.MetaEnumType)1