use of cz.habarta.typescript.generator.emitter.TsStringLiteral in project typescript-generator by vojtechhabarta.
the class OnePossiblePropertyValueAssigningExtension method findOnlyValueForEnumReferenceType.
private static Optional<TsExpression> findOnlyValueForEnumReferenceType(TsModel model, TsType.EnumReferenceType propertyType) {
Symbol symbol = propertyType.symbol;
Optional<TsEnumModel> enumModelOption = model.getOriginalStringEnums().stream().filter(candidate -> candidate.getName().getFullName().equals(symbol.getFullName())).findAny();
if (!enumModelOption.isPresent()) {
return Optional.empty();
}
TsEnumModel enumModel = enumModelOption.get();
if (enumModel.getMembers().size() != 1) {
return Optional.empty();
}
EnumMemberModel singleElement = enumModel.getMembers().iterator().next();
Object enumValue = singleElement.getEnumValue();
TsStringLiteral expression = new TsStringLiteral((String) enumValue);
return Optional.of(expression);
}
use of cz.habarta.typescript.generator.emitter.TsStringLiteral in project typescript-generator by vojtechhabarta.
the class RequiredPropertyConstructorExtension method getPredefinedValueForProperty.
private static Optional<TsExpression> getPredefinedValueForProperty(TsPropertyModel property, TsModel model) {
if (property.tsType instanceof TsType.UnionType) {
List<TsType> unionTypeElements = ((TsType.UnionType) property.tsType).types;
if (unionTypeElements.size() != 1) {
return Optional.empty();
}
TsType onlyElement = unionTypeElements.iterator().next();
if (!(onlyElement instanceof TsType.StringLiteralType)) {
return Optional.empty();
}
TsType.StringLiteralType onlyValue = (TsType.StringLiteralType) onlyElement;
TsStringLiteral expression = new TsStringLiteral(onlyValue.literal);
return Optional.of(expression);
}
if (property.tsType instanceof TsType.EnumReferenceType) {
Symbol symbol = ((TsType.EnumReferenceType) property.tsType).symbol;
Optional<TsEnumModel> enumModelOption = model.getOriginalStringEnums().stream().filter(candidate -> candidate.getName().getFullName().equals(symbol.getFullName())).findAny();
if (!enumModelOption.isPresent()) {
return Optional.empty();
}
TsEnumModel enumModel = enumModelOption.get();
if (enumModel.getMembers().size() != 1) {
return Optional.empty();
}
EnumMemberModel singleElement = enumModel.getMembers().iterator().next();
Object enumValue = singleElement.getEnumValue();
TsStringLiteral expression = new TsStringLiteral((String) enumValue);
return Optional.of(expression);
}
return Optional.empty();
}
use of cz.habarta.typescript.generator.emitter.TsStringLiteral 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)"));
}
use of cz.habarta.typescript.generator.emitter.TsStringLiteral in project typescript-generator by vojtechhabarta.
the class ModelCompiler method processRestMethod.
private TsMethodModel processRestMethod(TsModel tsModel, SymbolTable symbolTable, String pathPrefix, Symbol responseSymbol, RestMethodModel method, boolean createLongName, TsType optionsType, boolean implement) {
final String path = Utils.joinPath(pathPrefix, method.getPath());
final PathTemplate pathTemplate = PathTemplate.parse(path);
final List<String> comments = Utils.concat(method.getComments(), Arrays.asList("HTTP " + method.getHttpMethod() + " /" + path, "Java method: " + method.getOriginClass().getName() + "." + method.getName()));
final List<TsParameterModel> parameters = new ArrayList<>();
// path params
for (MethodParameterModel parameter : method.getPathParams()) {
parameters.add(processParameter(symbolTable, method, parameter));
}
// entity param
if (method.getEntityParam() != null) {
parameters.add(processParameter(symbolTable, method, method.getEntityParam()));
}
// query params
final List<RestQueryParam> queryParams = method.getQueryParams();
final TsParameterModel queryParameter;
if (queryParams != null && !queryParams.isEmpty()) {
final List<TsType> types = new ArrayList<>();
if (queryParams.stream().anyMatch(param -> param instanceof RestQueryParam.Map)) {
types.add(new TsType.IndexedArrayType(TsType.String, TsType.Any));
} else {
final List<TsProperty> currentSingles = new ArrayList<>();
final Runnable flushSingles = () -> {
if (!currentSingles.isEmpty()) {
types.add(new TsType.ObjectType(currentSingles));
currentSingles.clear();
}
};
for (RestQueryParam restQueryParam : queryParams) {
if (restQueryParam instanceof RestQueryParam.Single) {
final MethodParameterModel queryParam = ((RestQueryParam.Single) restQueryParam).getQueryParam();
final TsType type = typeFromJava(symbolTable, queryParam.getType(), method.getName(), method.getOriginClass());
currentSingles.add(new TsProperty(queryParam.getName(), restQueryParam.required ? type : new TsType.OptionalType(type)));
}
if (restQueryParam instanceof RestQueryParam.Bean) {
final BeanModel queryBean = ((RestQueryParam.Bean) restQueryParam).getBean();
flushSingles.run();
final Symbol queryParamsSymbol = symbolTable.getSymbol(queryBean.getOrigin(), "QueryParams");
if (tsModel.getBean(queryParamsSymbol) == null) {
tsModel.getBeans().add(new TsBeanModel(queryBean.getOrigin(), TsBeanCategory.Data, /*isClass*/
false, queryParamsSymbol, /*typeParameters*/
null, /*parent*/
null, /*extendsList*/
null, /*implementsList*/
null, processProperties(symbolTable, null, queryBean), /*constructor*/
null, /*methods*/
null, /*comments*/
null));
}
types.add(new TsType.ReferenceType(queryParamsSymbol));
}
}
flushSingles.run();
}
boolean allQueryParamsOptional = queryParams.stream().noneMatch(queryParam -> queryParam.required);
TsType.IntersectionType queryParamType = new TsType.IntersectionType(types);
queryParameter = new TsParameterModel("queryParams", allQueryParamsOptional ? new TsType.OptionalType(queryParamType) : queryParamType);
parameters.add(queryParameter);
} else {
queryParameter = null;
}
if (optionsType != null) {
final TsParameterModel optionsParameter = new TsParameterModel("options", new TsType.OptionalType(optionsType));
parameters.add(optionsParameter);
}
// return type
final TsType returnType = typeFromJava(symbolTable, method.getReturnType(), method.getName(), method.getOriginClass());
final TsType wrappedReturnType = new TsType.GenericReferenceType(responseSymbol, returnType);
// method name
final String nameSuffix;
if (createLongName) {
nameSuffix = "$" + method.getHttpMethod() + "$" + pathTemplate.format("", "", false).replaceAll("/", "_").replaceAll("\\W", "");
} else {
nameSuffix = "";
}
// implementation
final List<TsStatement> body;
if (implement) {
body = new ArrayList<>();
body.add(new TsReturnStatement(new TsCallExpression(new TsMemberExpression(new TsMemberExpression(new TsThisExpression(), "httpClient"), "request"), new TsObjectLiteral(new TsPropertyDefinition("method", new TsStringLiteral(method.getHttpMethod())), new TsPropertyDefinition("url", processPathTemplate(pathTemplate)), queryParameter != null ? new TsPropertyDefinition("queryParams", new TsIdentifierReference("queryParams")) : null, method.getEntityParam() != null ? new TsPropertyDefinition("data", new TsIdentifierReference(method.getEntityParam().getName())) : null, optionsType != null ? new TsPropertyDefinition("options", new TsIdentifierReference("options")) : null))));
} else {
body = null;
}
// method
final TsMethodModel tsMethodModel = new TsMethodModel(method.getName() + nameSuffix, TsModifierFlags.None, null, parameters, wrappedReturnType, body, comments);
return tsMethodModel;
}
use of cz.habarta.typescript.generator.emitter.TsStringLiteral 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