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