|
| 1 | +package cz.habarta.typescript.generator.ext; |
| 2 | + |
| 3 | +import cz.habarta.typescript.generator.Extension; |
| 4 | +import cz.habarta.typescript.generator.TsType; |
| 5 | +import cz.habarta.typescript.generator.compiler.EnumMemberModel; |
| 6 | +import cz.habarta.typescript.generator.compiler.ModelCompiler; |
| 7 | +import cz.habarta.typescript.generator.compiler.Symbol; |
| 8 | +import cz.habarta.typescript.generator.compiler.SymbolTable; |
| 9 | +import cz.habarta.typescript.generator.emitter.EmitterExtensionFeatures; |
| 10 | +import cz.habarta.typescript.generator.emitter.TsAssignmentExpression; |
| 11 | +import cz.habarta.typescript.generator.emitter.TsBeanModel; |
| 12 | +import cz.habarta.typescript.generator.emitter.TsCallExpression; |
| 13 | +import cz.habarta.typescript.generator.emitter.TsConstructorModel; |
| 14 | +import cz.habarta.typescript.generator.emitter.TsEnumModel; |
| 15 | +import cz.habarta.typescript.generator.emitter.TsExpression; |
| 16 | +import cz.habarta.typescript.generator.emitter.TsExpressionStatement; |
| 17 | +import cz.habarta.typescript.generator.emitter.TsMemberExpression; |
| 18 | +import cz.habarta.typescript.generator.emitter.TsModel; |
| 19 | +import cz.habarta.typescript.generator.emitter.TsModifierFlags; |
| 20 | +import cz.habarta.typescript.generator.emitter.TsPropertyModel; |
| 21 | +import cz.habarta.typescript.generator.emitter.TsStatement; |
| 22 | +import cz.habarta.typescript.generator.emitter.TsStringLiteral; |
| 23 | +import cz.habarta.typescript.generator.emitter.TsSuperExpression; |
| 24 | +import cz.habarta.typescript.generator.emitter.TsThisExpression; |
| 25 | + |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.Collection; |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Optional; |
| 31 | +import java.util.stream.Collectors; |
| 32 | + |
| 33 | +/** |
| 34 | + * The extension marks all properties which type allows only one possible value (for instance, enum with only one value |
| 35 | + * or {@link cz.habarta.typescript.generator.TsType.UnionType} with only one option) as read only and sets their |
| 36 | + * value in the constructor. |
| 37 | + * It may be useful while generating code for class hierarchy where each subclass has a discriminator property that can |
| 38 | + * only have one value. Hence, using the TypeScript code it will not be necessary to set this value manually. |
| 39 | + * |
| 40 | + * @author krzs |
| 41 | + */ |
| 42 | +public class OnePossiblePropertyValueAssigningExtension extends Extension { |
| 43 | + |
| 44 | + @Override |
| 45 | + public EmitterExtensionFeatures getFeatures() { |
| 46 | + EmitterExtensionFeatures features = new EmitterExtensionFeatures(); |
| 47 | + features.generatesRuntimeCode = true; |
| 48 | + return features; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public List<TransformerDefinition> getTransformers() { |
| 53 | + return Collections.singletonList( |
| 54 | + new TransformerDefinition(ModelCompiler.TransformationPhase.AfterDeclarationSorting, |
| 55 | + OnePossiblePropertyValueAssigningExtension::transformModel) |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + private static TsModel transformModel(SymbolTable symbolTable, TsModel model) { |
| 60 | + List<TsBeanModel> beans = model.getBeans().stream() |
| 61 | + .map(bean -> transformBean(bean, model)) |
| 62 | + .collect(Collectors.toList()); |
| 63 | + return model.withBeans(beans); |
| 64 | + } |
| 65 | + |
| 66 | + private static TsBeanModel transformBean(TsBeanModel bean, TsModel model) { |
| 67 | + if (!bean.isClass() || bean.getConstructor() != null) { |
| 68 | + return bean; |
| 69 | + } |
| 70 | + |
| 71 | + List<TsPropertyModel> newProperties = new ArrayList<>(); |
| 72 | + Collection<TsExpressionStatement> valueAssignmentStatements = new ArrayList<>(); |
| 73 | + |
| 74 | + for (TsPropertyModel property : bean.getProperties()) { |
| 75 | + TsPropertyModel newProperty = property; |
| 76 | + |
| 77 | + Optional<TsExpression> onlyValue = findOnlyValueForProperty(property, model); |
| 78 | + if (onlyValue.isPresent()) { |
| 79 | + newProperty = new TsPropertyModel(property.name, property.tsType, |
| 80 | + TsModifierFlags.None.setReadonly(), property.ownProperty, property.comments); |
| 81 | + |
| 82 | + TsExpressionStatement assignmentStatement = createValueAssignmentStatement(newProperty, onlyValue.get()); |
| 83 | + valueAssignmentStatements.add(assignmentStatement); |
| 84 | + } |
| 85 | + |
| 86 | + newProperties.add(newProperty); |
| 87 | + } |
| 88 | + |
| 89 | + TsBeanModel newBean = bean.withProperties(newProperties); |
| 90 | + if (!valueAssignmentStatements.isEmpty()) { |
| 91 | + TsConstructorModel constructor = createConstructor(bean, valueAssignmentStatements); |
| 92 | + newBean = newBean.withConstructor(constructor); |
| 93 | + } |
| 94 | + return newBean; |
| 95 | + } |
| 96 | + |
| 97 | + private static TsConstructorModel createConstructor(TsBeanModel bean, |
| 98 | + Collection<TsExpressionStatement> valueAssignmentStatements) { |
| 99 | + List<TsStatement> body = new ArrayList<>(); |
| 100 | + if (bean.getParent() != null) { |
| 101 | + body.add(new TsExpressionStatement(new TsCallExpression(new TsSuperExpression()))); |
| 102 | + } |
| 103 | + |
| 104 | + body.addAll(valueAssignmentStatements); |
| 105 | + |
| 106 | + return new TsConstructorModel(TsModifierFlags.None, Collections.emptyList(), body, null); |
| 107 | + } |
| 108 | + |
| 109 | + private static TsExpressionStatement createValueAssignmentStatement(TsPropertyModel property, TsExpression value) { |
| 110 | + TsMemberExpression leftHandSideExpression = new TsMemberExpression(new TsThisExpression(), property.name); |
| 111 | + TsExpression assignment = new TsAssignmentExpression(leftHandSideExpression, value); |
| 112 | + return new TsExpressionStatement(assignment); |
| 113 | + } |
| 114 | + |
| 115 | + private static Optional<TsExpression> findOnlyValueForProperty(TsPropertyModel property, TsModel model) { |
| 116 | + TsType propertyType = property.tsType; |
| 117 | + if (propertyType instanceof TsType.UnionType) { |
| 118 | + return findOnlyValueForUnionType((TsType.UnionType) propertyType); |
| 119 | + } |
| 120 | + if (propertyType instanceof TsType.EnumReferenceType) { |
| 121 | + return findOnlyValueForEnumReferenceType(model, (TsType.EnumReferenceType) propertyType); |
| 122 | + } |
| 123 | + return Optional.empty(); |
| 124 | + } |
| 125 | + |
| 126 | + private static Optional<TsExpression> findOnlyValueForUnionType(TsType.UnionType unionType) { |
| 127 | + List<TsType> unionTypeElements = unionType.types; |
| 128 | + if (unionTypeElements.size() != 1) { |
| 129 | + return Optional.empty(); |
| 130 | + } |
| 131 | + TsType onlyElement = unionTypeElements.iterator().next(); |
| 132 | + if (!(onlyElement instanceof TsType.StringLiteralType)) { |
| 133 | + return Optional.empty(); |
| 134 | + } |
| 135 | + TsType.StringLiteralType onlyValue = (TsType.StringLiteralType) onlyElement; |
| 136 | + TsStringLiteral expression = new TsStringLiteral(onlyValue.literal); |
| 137 | + return Optional.of(expression); |
| 138 | + } |
| 139 | + |
| 140 | + private static Optional<TsExpression> findOnlyValueForEnumReferenceType(TsModel model, |
| 141 | + TsType.EnumReferenceType propertyType) { |
| 142 | + Symbol symbol = propertyType.symbol; |
| 143 | + Optional<TsEnumModel> enumModelOption = model.getOriginalStringEnums().stream() |
| 144 | + .filter(candidate -> candidate.getName().getFullName().equals(symbol.getFullName())) |
| 145 | + .findAny(); |
| 146 | + if (!enumModelOption.isPresent()) { |
| 147 | + return Optional.empty(); |
| 148 | + } |
| 149 | + TsEnumModel enumModel = enumModelOption.get(); |
| 150 | + if (enumModel.getMembers().size() != 1) { |
| 151 | + return Optional.empty(); |
| 152 | + } |
| 153 | + EnumMemberModel singleElement = enumModel.getMembers().iterator().next(); |
| 154 | + Object enumValue = singleElement.getEnumValue(); |
| 155 | + TsStringLiteral expression = new TsStringLiteral((String) enumValue); |
| 156 | + return Optional.of(expression); |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments