Search in sources :

Example 1 with TsSwitchStatement

use of cz.habarta.typescript.generator.emitter.TsSwitchStatement in project typescript-generator by vojtechhabarta.

the class JsonDeserializationExtension method createDeserializationMethodForTaggedUnion.

private static TsMethodModel createDeserializationMethodForTaggedUnion(SymbolTable symbolTable, TsModel tsModel, TsBeanModel bean) {
    final List<TsSwitchCaseClause> caseClauses = new ArrayList<>();
    for (Class<?> cls : bean.getTaggedUnionClasses()) {
        final TsBeanModel tuBean = tsModel.getBean(cls);
        caseClauses.add(new TsSwitchCaseClause(new TsStringLiteral(tuBean.getDiscriminantLiteral()), Arrays.<TsStatement>asList(new TsReturnStatement(new TsCallExpression(new TsMemberExpression(new TsTypeReferenceExpression(new TsType.ReferenceType(symbolTable.getSymbol(cls))), "fromData"), new TsIdentifierReference("data"))))));
    }
    final List<TsStatement> body = new ArrayList<>();
    body.add(ifUndefinedThenReturnItStatement("data"));
    body.add(new TsSwitchStatement(new TsMemberExpression(new TsIdentifierReference("data"), bean.getDiscriminantProperty()), caseClauses, null));
    final TsType.ReferenceType unionType = new TsType.ReferenceType(bean.getTaggedUnionAlias().getName());
    return new TsMethodModel("fromDataUnion", TsModifierFlags.None.setStatic(), // typeParameters,
    null, Arrays.asList(new TsParameterModel("data", unionType)), unionType, body, null);
}
Also used : TsStatement(cz.habarta.typescript.generator.emitter.TsStatement) TsMemberExpression(cz.habarta.typescript.generator.emitter.TsMemberExpression) ArrayList(java.util.ArrayList) TsType(cz.habarta.typescript.generator.TsType) TsReturnStatement(cz.habarta.typescript.generator.emitter.TsReturnStatement) TsCallExpression(cz.habarta.typescript.generator.emitter.TsCallExpression) TsTypeReferenceExpression(cz.habarta.typescript.generator.emitter.TsTypeReferenceExpression) TsSwitchStatement(cz.habarta.typescript.generator.emitter.TsSwitchStatement) TsSwitchCaseClause(cz.habarta.typescript.generator.emitter.TsSwitchCaseClause) TsStringLiteral(cz.habarta.typescript.generator.emitter.TsStringLiteral) TsIdentifierReference(cz.habarta.typescript.generator.emitter.TsIdentifierReference) TsBeanModel(cz.habarta.typescript.generator.emitter.TsBeanModel) TsMethodModel(cz.habarta.typescript.generator.emitter.TsMethodModel) TsParameterModel(cz.habarta.typescript.generator.emitter.TsParameterModel)

Aggregations

TsType (cz.habarta.typescript.generator.TsType)1 TsBeanModel (cz.habarta.typescript.generator.emitter.TsBeanModel)1 TsCallExpression (cz.habarta.typescript.generator.emitter.TsCallExpression)1 TsIdentifierReference (cz.habarta.typescript.generator.emitter.TsIdentifierReference)1 TsMemberExpression (cz.habarta.typescript.generator.emitter.TsMemberExpression)1 TsMethodModel (cz.habarta.typescript.generator.emitter.TsMethodModel)1 TsParameterModel (cz.habarta.typescript.generator.emitter.TsParameterModel)1 TsReturnStatement (cz.habarta.typescript.generator.emitter.TsReturnStatement)1 TsStatement (cz.habarta.typescript.generator.emitter.TsStatement)1 TsStringLiteral (cz.habarta.typescript.generator.emitter.TsStringLiteral)1 TsSwitchCaseClause (cz.habarta.typescript.generator.emitter.TsSwitchCaseClause)1 TsSwitchStatement (cz.habarta.typescript.generator.emitter.TsSwitchStatement)1 TsTypeReferenceExpression (cz.habarta.typescript.generator.emitter.TsTypeReferenceExpression)1 ArrayList (java.util.ArrayList)1