use of cz.habarta.typescript.generator.emitter.TsIdentifierReference in project typescript-generator by vojtechhabarta.
the class RequiredPropertyConstructorExtension method createConstructor.
private static Optional<TsConstructorModel> createConstructor(TsBeanModel bean, TsModel model, Map<String, TsConstructorModel> generatedConstructors) {
List<TsParameterModel> parameters = new ArrayList<>();
List<TsParameterModel> optionalParameters = new ArrayList<>();
List<TsStatement> body = new ArrayList<>();
TsType parent = bean.getParent();
if (parent != null) {
if (!(parent instanceof TsType.ReferenceType)) {
throw new IllegalStateException("Generating constructor for non-reference parent types is not currently supported");
}
TsType.ReferenceType referenceParent = (TsType.ReferenceType) parent;
TsConstructorModel parentConstructor = generatedConstructors.get(referenceParent.symbol.getFullName());
if (parentConstructor == null) {
throw new IllegalStateException("Generating constructor for class with non-generated constructor is not currently supported");
}
List<TsParameterModel> parentParameters = parentConstructor.getParameters();
TsIdentifierReference[] callParameters = new TsIdentifierReference[parentParameters.size()];
int i = 0;
for (TsParameterModel parentParameter : parentParameters) {
List<TsParameterModel> targetParameterList = parentParameter.tsType instanceof TsType.OptionalType ? optionalParameters : parameters;
targetParameterList.add(parentParameter);
callParameters[i] = new TsIdentifierReference(parentParameter.name);
i++;
}
body.add(new TsExpressionStatement(new TsCallExpression(new TsSuperExpression(), callParameters)));
}
for (TsPropertyModel property : bean.getProperties()) {
if (!property.modifiers.isReadonly) {
continue;
}
TsExpression assignmentExpression;
Optional<TsExpression> predefinedValue = getPredefinedValueForProperty(property, model);
if (predefinedValue.isPresent()) {
assignmentExpression = predefinedValue.get();
} else {
TsParameterModel parameter = new TsParameterModel(property.name, property.tsType);
List<TsParameterModel> targetParameterList = property.tsType instanceof TsType.OptionalType ? optionalParameters : parameters;
targetParameterList.add(parameter);
assignmentExpression = new TsIdentifierReference(property.name);
}
TsMemberExpression leftHandSideExpression = new TsMemberExpression(new TsThisExpression(), property.name);
TsExpression assignment = new TsAssignmentExpression(leftHandSideExpression, assignmentExpression);
TsExpressionStatement assignmentStatement = new TsExpressionStatement(assignment);
body.add(assignmentStatement);
}
parameters.addAll(optionalParameters);
if (parameters.isEmpty() && body.isEmpty()) {
return Optional.empty();
}
TsConstructorModel constructor = new TsConstructorModel(TsModifierFlags.None, parameters, body, null);
return Optional.of(constructor);
}
use of cz.habarta.typescript.generator.emitter.TsIdentifierReference in project typescript-generator by vojtechhabarta.
the class JsonDeserializationExtension method createDeserializationGenericFunctionConstructor.
private static TsMethodModel createDeserializationGenericFunctionConstructor(SymbolTable symbolTable, TsModel tsModel, TsBeanModel bean) {
final Symbol beanIdentifier = symbolTable.getSymbol(bean.getOrigin());
List<TsType.GenericVariableType> typeParameters = getTypeParameters(bean.getOrigin());
final TsType.ReferenceType dataType = new TsType.GenericReferenceType(beanIdentifier, typeParameters);
final List<TsParameterModel> constructorFnOfParameters = getConstructorFnOfParameters(typeParameters);
final List<TsExpression> arguments = new ArrayList<>();
arguments.add(new TsIdentifierReference("data"));
for (TsParameterModel constructorFnOfParameter : constructorFnOfParameters) {
arguments.add(new TsIdentifierReference(constructorFnOfParameter.name));
}
final List<TsStatement> body = new ArrayList<>();
body.add(new TsReturnStatement(new TsArrowFunction(Arrays.asList(new TsParameter("data", null)), new TsCallExpression(new TsMemberExpression(new TsTypeReferenceExpression(new TsType.ReferenceType(beanIdentifier)), "fromData"), null, arguments))));
return new TsMethodModel("fromDataFn", TsModifierFlags.None.setStatic(), typeParameters, constructorFnOfParameters, new TsType.FunctionType(Arrays.asList(new TsParameter("data", dataType)), dataType), body, null);
}
use of cz.habarta.typescript.generator.emitter.TsIdentifierReference 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.TsIdentifierReference in project typescript-generator by vojtechhabarta.
the class JsonDeserializationExtension method getConstructorParameters.
private static List<TsIdentifierReference> getConstructorParameters(TsBeanModel bean) {
TsConstructorModel constructor = bean.getConstructor();
if (constructor == null) {
return null;
}
List<TsIdentifierReference> parameters = new ArrayList<>();
for (TsParameterModel parameter : constructor.getParameters()) {
parameters.add(new TsIdentifierReference(parameter.name));
}
return parameters;
}
use of cz.habarta.typescript.generator.emitter.TsIdentifierReference in project typescript-generator by vojtechhabarta.
the class DecoratorsTest method testDecoratorsOnParameterAndMethod.
@Test
public void testDecoratorsOnParameterAndMethod() {
final Settings settings = TestUtils.settings();
settings.outputFileType = TypeScriptFileType.implementationFile;
settings.outputKind = TypeScriptOutputKind.module;
settings.mapClasses = ClassMapping.asClasses;
settings.generateConstructors = true;
final TypeScriptGenerator typeScriptGenerator = new TypeScriptGenerator(settings);
final Model model = typeScriptGenerator.getModelParser().parseModel(City.class);
final TsModel tsModel = typeScriptGenerator.getModelCompiler().javaToTypeScript(model);
final TsBeanModel bean = tsModel.getBean(City.class);
final TsBeanModel bean2 = bean.withConstructor(bean.getConstructor().withParameters(Arrays.asList(bean.getConstructor().getParameters().get(0).withDecorators(Arrays.asList(new TsDecorator(new TsIdentifierReference("Inject"), Arrays.asList(new TsStringLiteral("token")))))))).withMethods(Arrays.asList(new TsMethodModel("greet", null, null, Collections.emptyList(), TsType.Void, Collections.emptyList(), null).withDecorators(Arrays.asList(new TsDecorator(new TsIdentifierReference("enumerable"), Arrays.asList(new TsBooleanLiteral(false)))))));
final TsModel tsModel2 = tsModel.withBeans(Arrays.asList(bean2));
final String output = emit(typeScriptGenerator.getEmitter(), tsModel2);
Assertions.assertTrue(output.contains("@Inject(\"token\")"));
Assertions.assertTrue(output.contains("@enumerable(false)"));
}
Aggregations