use of cz.habarta.typescript.generator.emitter.TsBeanModel in project typescript-generator by vojtechhabarta.
the class BeanPropertyPathExtension method getBeanModelByType.
private static TsBeanModel getBeanModelByType(TsModel model, TsType type) {
TsType originalType = extractOriginalTsType(type);
if (!(originalType instanceof TsType.ReferenceType)) {
return null;
}
TsType.ReferenceType originalTypeBean = (TsType.ReferenceType) originalType;
for (TsBeanModel curBean : model.getBeans()) {
if (curBean.getName().equals(originalTypeBean.symbol)) {
return curBean;
}
}
return null;
}
use of cz.habarta.typescript.generator.emitter.TsBeanModel in project typescript-generator by vojtechhabarta.
the class JsonDeserializationExtension method useDeserializationMethodsInJaxrs.
private TsModel useDeserializationMethodsInJaxrs(SymbolTable symbolTable, TsModel tsModel) {
final List<TsBeanModel> beans = new ArrayList<>();
for (TsBeanModel bean : tsModel.getBeans()) {
if (bean.isJaxrsApplicationClientBean()) {
final List<TsMethodModel> methods = new ArrayList<>();
for (TsMethodModel method : bean.getMethods()) {
final TsMethodModel changedMethod = addCopyFnToJaxrsMethod(symbolTable, tsModel, method);
methods.add(changedMethod != null ? changedMethod : method);
}
beans.add(bean.withMethods(methods));
} else {
beans.add(bean);
}
}
return tsModel.withBeans(beans);
}
use of cz.habarta.typescript.generator.emitter.TsBeanModel in project typescript-generator by vojtechhabarta.
the class JsonDeserializationExtension method getCopyFunctionForTsType.
private static TsExpression getCopyFunctionForTsType(SymbolTable symbolTable, TsModel tsModel, TsType tsType) {
if (tsType instanceof TsType.GenericReferenceType) {
final TsType.GenericReferenceType genericReferenceType = (TsType.GenericReferenceType) tsType;
// Class.fromDataFn<T1...>(constructorFnOfT1...)
final List<TsExpression> arguments = new ArrayList<>();
for (TsType typeArgument : genericReferenceType.typeArguments) {
arguments.add(getCopyFunctionForTsType(symbolTable, tsModel, typeArgument));
}
return new TsCallExpression(new TsMemberExpression(new TsTypeReferenceExpression(new TsType.ReferenceType(genericReferenceType.symbol)), "fromDataFn"), genericReferenceType.typeArguments, arguments);
}
if (tsType instanceof TsType.ReferenceType) {
final TsType.ReferenceType referenceType = (TsType.ReferenceType) tsType;
final TsBeanModel referencedBean = tsModel.getBean(symbolTable.getSymbolClass(referenceType.symbol));
if (referencedBean != null && referencedBean.isClass()) {
if (referencedBean.getTaggedUnionAlias() != null) {
// Class.fromDataUnion (tagged union)
return new TsMemberExpression(new TsTypeReferenceExpression(new TsType.ReferenceType(referencedBean.getName())), "fromDataUnion");
} else {
// Class.fromData
return new TsMemberExpression(new TsTypeReferenceExpression(referenceType), "fromData");
}
}
}
if (tsType instanceof TsType.BasicArrayType) {
// __getCopyArrayFn
final TsType.BasicArrayType arrayType = (TsType.BasicArrayType) tsType;
return new TsCallExpression(new TsIdentifierReference("__getCopyArrayFn"), getCopyFunctionForTsType(symbolTable, tsModel, arrayType.elementType));
}
if (tsType instanceof TsType.IndexedArrayType) {
// __getCopyObjectFn
final TsType.IndexedArrayType objectType = (TsType.IndexedArrayType) tsType;
return new TsCallExpression(new TsIdentifierReference("__getCopyObjectFn"), getCopyFunctionForTsType(symbolTable, tsModel, objectType.elementType));
}
if (tsType instanceof TsType.GenericVariableType) {
// constructorFnOfT
final TsType.GenericVariableType genericVariableType = (TsType.GenericVariableType) tsType;
return new TsIdentifierReference("constructorFnOf" + genericVariableType.name);
}
// __identity
return new TsCallExpression(new TsIdentifierReference("__identity"), Arrays.asList(tsType), Collections.<TsExpression>emptyList());
}
use of cz.habarta.typescript.generator.emitter.TsBeanModel in project typescript-generator by vojtechhabarta.
the class JsonDeserializationExtension method createDeserializationMethods.
private static TsModel createDeserializationMethods(SymbolTable symbolTable, TsModel tsModel) {
tsModel.getHelpers().add(TsHelper.loadFromResource("/helpers/jsonDeserialization.ts"));
final List<TsBeanModel> beans = new ArrayList<>();
for (TsBeanModel bean : tsModel.getBeans()) {
if (bean.isDataClass()) {
final List<TsMethodModel> methods = new ArrayList<>(bean.getMethods());
final TsMethodModel deserializationMethod = createDeserializationMethod(symbolTable, tsModel, bean);
methods.add(0, deserializationMethod);
if (!bean.getTypeParameters().isEmpty()) {
final TsMethodModel genericFunctionConstructor = createDeserializationGenericFunctionConstructor(symbolTable, tsModel, bean);
methods.add(0, genericFunctionConstructor);
}
if (bean.getTaggedUnionAlias() != null) {
final TsMethodModel unionDeserializationMethod = createDeserializationMethodForTaggedUnion(symbolTable, tsModel, bean);
methods.add(1, unionDeserializationMethod);
}
beans.add(bean.withMethods(methods));
} else {
beans.add(bean);
}
}
return tsModel.withBeans(beans);
}
use of cz.habarta.typescript.generator.emitter.TsBeanModel in project typescript-generator by vojtechhabarta.
the class ModelCompiler method createRestInterfaces.
private void createRestInterfaces(TsModel tsModel, SymbolTable symbolTable, List<RestApplicationModel> restApplications, Symbol responseSymbol, TsType.GenericVariableType optionsGenericVariable, TsType optionsType) {
final List<TsType.GenericVariableType> typeParameters = Utils.listFromNullable(optionsGenericVariable);
final Map<Symbol, List<TsMethodModel>> groupedMethods = processRestMethods(tsModel, restApplications, symbolTable, null, responseSymbol, optionsType, false);
for (Map.Entry<Symbol, List<TsMethodModel>> entry : groupedMethods.entrySet()) {
final TsBeanModel interfaceModel = new TsBeanModel(null, TsBeanCategory.Service, false, entry.getKey(), typeParameters, null, null, null, null, null, entry.getValue(), null);
tsModel.getBeans().add(interfaceModel);
}
}
Aggregations