use of cz.habarta.typescript.generator.emitter.TsExpression in project typescript-generator by vojtechhabarta.
the class JsonDeserializationExtension method addCopyFnToJaxrsMethod.
private static TsMethodModel addCopyFnToJaxrsMethod(SymbolTable symbolTable, TsModel tsModel, TsMethodModel method) {
final TsType returnType = method.getReturnType();
if (!(returnType instanceof TsType.GenericReferenceType))
return null;
final TsType.GenericReferenceType genericReferenceReturnType = (TsType.GenericReferenceType) returnType;
if (genericReferenceReturnType.symbol != symbolTable.getSyntheticSymbol("RestResponse"))
return null;
final List<TsType> typeArguments = genericReferenceReturnType.typeArguments;
if (typeArguments == null || typeArguments.size() != 1)
return null;
final TsType returnDataType = typeArguments.get(0);
final List<TsStatement> body = method.getBody();
if (body == null || body.size() != 1)
return null;
final TsStatement statement = body.get(0);
if (!(statement instanceof TsReturnStatement))
return null;
final TsReturnStatement returnStatement = (TsReturnStatement) statement;
final TsExpression returnExpression = returnStatement.getExpression();
if (returnExpression == null)
return null;
if (!(returnExpression instanceof TsCallExpression))
return null;
final TsCallExpression callExpression = (TsCallExpression) returnExpression;
final List<TsExpression> arguments = callExpression.getArguments();
if (arguments == null || arguments.isEmpty())
return null;
final TsExpression firstArgument = arguments.get(0);
if (!(firstArgument instanceof TsObjectLiteral))
return null;
final TsObjectLiteral objectLiteral = (TsObjectLiteral) firstArgument;
// todo create changed method instead of modifying existing
final int index = Math.max(objectLiteral.getPropertyDefinitions().size() - 1, 0);
final TsExpression copyFunction = returnDataType == TsType.Void ? TsIdentifierReference.Undefined : getCopyFunctionForTsType(symbolTable, tsModel, returnDataType);
objectLiteral.getPropertyDefinitions().add(index, new TsPropertyDefinition("copyFn", copyFunction));
return method;
}
use of cz.habarta.typescript.generator.emitter.TsExpression in project typescript-generator by vojtechhabarta.
the class OnePossiblePropertyValueAssigningExtension method transformBean.
private static TsBeanModel transformBean(TsBeanModel bean, TsModel model) {
if (!bean.isClass() || bean.getConstructor() != null) {
return bean;
}
List<TsPropertyModel> newProperties = new ArrayList<>();
Collection<TsExpressionStatement> valueAssignmentStatements = new ArrayList<>();
for (TsPropertyModel property : bean.getProperties()) {
TsPropertyModel newProperty = property;
Optional<TsExpression> onlyValue = findOnlyValueForProperty(property, model);
if (onlyValue.isPresent()) {
newProperty = new TsPropertyModel(property.name, property.tsType, TsModifierFlags.None.setReadonly(), property.ownProperty, property.comments);
TsExpressionStatement assignmentStatement = createValueAssignmentStatement(newProperty, onlyValue.get());
valueAssignmentStatements.add(assignmentStatement);
}
newProperties.add(newProperty);
}
TsBeanModel newBean = bean.withProperties(newProperties);
if (!valueAssignmentStatements.isEmpty()) {
TsConstructorModel constructor = createConstructor(bean, valueAssignmentStatements);
newBean = newBean.withConstructor(constructor);
}
return newBean;
}
use of cz.habarta.typescript.generator.emitter.TsExpression in project typescript-generator by vojtechhabarta.
the class OnePossiblePropertyValueAssigningExtension method createValueAssignmentStatement.
private static TsExpressionStatement createValueAssignmentStatement(TsPropertyModel property, TsExpression value) {
TsMemberExpression leftHandSideExpression = new TsMemberExpression(new TsThisExpression(), property.name);
TsExpression assignment = new TsAssignmentExpression(leftHandSideExpression, value);
return new TsExpressionStatement(assignment);
}
use of cz.habarta.typescript.generator.emitter.TsExpression in project typescript-generator by vojtechhabarta.
the class ModelCompiler method processPathTemplate.
private static TsTemplateLiteral processPathTemplate(PathTemplate pathTemplate) {
final List<TsExpression> spans = new ArrayList<>();
for (PathTemplate.Part part : pathTemplate.getParts()) {
if (part instanceof PathTemplate.Literal) {
final PathTemplate.Literal literal = (PathTemplate.Literal) part;
spans.add(new TsStringLiteral(literal.getLiteral()));
}
if (part instanceof PathTemplate.Parameter) {
final PathTemplate.Parameter parameter = (PathTemplate.Parameter) part;
spans.add(new TsIdentifierReference(parameter.getValidName()));
}
}
return new TsTaggedTemplateLiteral(new TsIdentifierReference("uriEncoding"), spans);
}
Aggregations