use of cz.habarta.typescript.generator.emitter.TsConstructorModel 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.TsConstructorModel in project typescript-generator by vojtechhabarta.
the class RequiredPropertyConstructorExtension method transformBean.
private TsBeanModel transformBean(TsBeanModel bean, TsModel model, Map<String, TsConstructorModel> generatedConstructors) {
if (classes != null && !classes.contains(bean.getOrigin().getCanonicalName())) {
return bean;
}
if (!bean.isClass() || bean.getConstructor() != null) {
return bean;
}
Optional<TsConstructorModel> constructorOption = createConstructor(bean, model, generatedConstructors);
if (!constructorOption.isPresent()) {
return bean;
}
TsConstructorModel constructor = constructorOption.get();
generatedConstructors.put(bean.getName().getFullName(), constructor);
return bean.withConstructor(constructor);
}
use of cz.habarta.typescript.generator.emitter.TsConstructorModel in project typescript-generator by vojtechhabarta.
the class ModelCompiler method createRestClients.
private void createRestClients(TsModel tsModel, SymbolTable symbolTable, List<RestApplicationModel> restApplications, Symbol responseSymbol, TsType.GenericVariableType optionsGenericVariable, TsType optionsType) {
final Symbol httpClientSymbol = symbolTable.getSyntheticSymbol("HttpClient");
final List<TsType.GenericVariableType> typeParameters = Utils.listFromNullable(optionsGenericVariable);
// HttpClient interface
final TsType.GenericVariableType returnGenericVariable = new TsType.GenericVariableType("R");
tsModel.getBeans().add(new TsBeanModel(null, TsBeanCategory.ServicePrerequisite, false, httpClientSymbol, typeParameters, null, null, null, null, null, Arrays.asList(new TsMethodModel("request", TsModifierFlags.None, Arrays.asList(returnGenericVariable), Arrays.asList(new TsParameterModel("requestConfig", new TsType.ObjectType(new TsProperty("method", TsType.String), new TsProperty("url", TsType.String), new TsProperty("queryParams", new TsType.OptionalType(TsType.Any)), new TsProperty("data", new TsType.OptionalType(TsType.Any)), new TsProperty("copyFn", new TsType.OptionalType(new TsType.FunctionType(Arrays.asList(new TsParameter("data", returnGenericVariable)), returnGenericVariable))), optionsType != null ? new TsProperty("options", new TsType.OptionalType(optionsType)) : null))), new TsType.GenericReferenceType(responseSymbol, returnGenericVariable), null, null)), null));
// application client classes
final TsType.ReferenceType httpClientType = optionsGenericVariable != null ? new TsType.GenericReferenceType(httpClientSymbol, optionsGenericVariable) : new TsType.ReferenceType(httpClientSymbol);
final TsConstructorModel constructor = new TsConstructorModel(TsModifierFlags.None, Arrays.asList(new TsParameterModel(TsAccessibilityModifier.Protected, "httpClient", httpClientType)), Collections.<TsStatement>emptyList(), null);
final boolean bothInterfacesAndClients = settings.generateJaxrsApplicationInterface || settings.generateSpringApplicationInterface;
final String groupingSuffix = bothInterfacesAndClients ? null : "Client";
final Map<Symbol, List<TsMethodModel>> groupedMethods = processRestMethods(tsModel, restApplications, symbolTable, groupingSuffix, responseSymbol, optionsType, true);
for (Map.Entry<Symbol, List<TsMethodModel>> entry : groupedMethods.entrySet()) {
final Symbol symbol = bothInterfacesAndClients ? symbolTable.addSuffixToSymbol(entry.getKey(), "Client") : entry.getKey();
final TsType interfaceType = bothInterfacesAndClients ? new TsType.ReferenceType(entry.getKey()) : null;
final TsBeanModel clientModel = new TsBeanModel(null, TsBeanCategory.Service, true, symbol, typeParameters, null, null, Utils.listFromNullable(interfaceType), null, constructor, entry.getValue(), null);
tsModel.getBeans().add(clientModel);
}
// helper
tsModel.getHelpers().add(TsHelper.loadFromResource("/helpers/uriEncoding.ts"));
}
use of cz.habarta.typescript.generator.emitter.TsConstructorModel 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.TsConstructorModel in project typescript-generator by vojtechhabarta.
the class RequiredPropertyConstructorExtension method getTransformers.
@Override
public List<TransformerDefinition> getTransformers() {
return Arrays.asList(new TransformerDefinition(ModelCompiler.TransformationPhase.AfterDeclarationSorting, new ModelTransformer() {
@Override
public TsModel transformModel(SymbolTable symbolTable, TsModel model) {
List<TsBeanModel> beans = new ArrayList<>();
Map<String, TsConstructorModel> generatedConstructors = new HashMap<>();
for (TsBeanModel bean : model.getBeans()) {
TsBeanModel newBean = transformBean(bean, model, generatedConstructors);
beans.add(newBean);
}
return model.withBeans(beans);
}
}));
}
Aggregations