Skip to content

Commit 0077a05

Browse files
readonly properties - declarePropertiesAsReadOnly
1 parent 49e1e06 commit 0077a05

File tree

7 files changed

+28
-5
lines changed

7 files changed

+28
-5
lines changed

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/Settings.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class Settings {
2727
public JsonLibrary jsonLibrary = null;
2828
private Predicate<String> excludeFilter = null;
2929
public boolean declarePropertiesAsOptional = false;
30+
public boolean declarePropertiesAsReadOnly = false;
3031
public String removeTypeNamePrefix = null;
3132
public String removeTypeNameSuffix = null;
3233
public String addTypeNamePrefix = null;

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/compiler/ModelCompiler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private <T> TsBeanModel processBean(SymbolTable symbolTable, Map<Type, List<Bean
150150
final TsType discriminantType = literals.isEmpty()
151151
? TsType.String
152152
: new TsType.UnionType(literals);
153-
properties.add(0, new TsPropertyModel(bean.getDiscriminantProperty(), discriminantType, null));
153+
properties.add(0, new TsPropertyModel(bean.getDiscriminantProperty(), discriminantType, settings.declarePropertiesAsReadOnly, null));
154154
}
155155

156156
return new TsBeanModel(bean.getOrigin(), isClass, beanIdentifier, typeParameters, parentType, bean.getTaggedUnionClasses(), interfaces, properties, null, null, bean.getComments());
@@ -180,7 +180,7 @@ private static boolean containsProperty(List<TsPropertyModel> properties, String
180180
private TsPropertyModel processProperty(SymbolTable symbolTable, BeanModel bean, PropertyModel property) {
181181
final TsType type = typeFromJava(symbolTable, property.getType(), property.getName(), bean.getOrigin());
182182
final TsType tsType = property.isOptional() ? type.optional() : type;
183-
return new TsPropertyModel(property.getName(), tsType, property.getComments());
183+
return new TsPropertyModel(property.getName(), tsType, settings.declarePropertiesAsReadOnly, property.getComments());
184184
}
185185

186186
private TsEnumModel<?> processEnum(SymbolTable symbolTable, EnumModel<?> enumModel) {

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/emitter/Emitter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ private void emitBeans(TsModel model, boolean exportKeyword) {
145145
private void emitProperty(TsPropertyModel property) {
146146
emitComments(property.getComments());
147147
final TsType tsType = property.getTsType();
148+
final String readonly = property.readonly ? "readonly " : "";
148149
final String questionMark = settings.declarePropertiesAsOptional || (tsType instanceof TsType.OptionalType) ? "?" : "";
149-
writeIndentedLine(quoteIfNeeded(property.getName(), settings) + questionMark + ": " + tsType.format(settings) + ";");
150+
writeIndentedLine(readonly + quoteIfNeeded(property.getName(), settings) + questionMark + ": " + tsType.format(settings) + ";");
150151
}
151152

152153
public static String quoteIfNeeded(String name, Settings settings) {

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/emitter/TsPropertyModel.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88

99
public class TsPropertyModel extends TsProperty implements Comparable<TsProperty> {
1010

11+
public final boolean readonly;
1112
public final List<String> comments;
1213

13-
public TsPropertyModel(String name, TsType tsType, List<String> comments) {
14+
public TsPropertyModel(String name, TsType tsType, boolean readonly, List<String> comments) {
1415
super(name, tsType);
16+
this.readonly = readonly;
1517
this.comments = comments;
1618
}
1719

@@ -20,7 +22,7 @@ public List<String> getComments() {
2022
}
2123

2224
public TsPropertyModel setTsType(TsType type) {
23-
return new TsPropertyModel(getName(), type, getComments());
25+
return new TsPropertyModel(getName(), type, readonly, getComments());
2426
}
2527

2628
@Override

typescript-generator-core/src/test/java/cz/habarta/typescript/generator/InterfaceTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ public void test() {
1616
Assert.assertTrue(output.contains("interface Author"));
1717
}
1818

19+
@Test
20+
public void testReadOnlyProperties() {
21+
final Settings settings = TestUtils.settings();
22+
settings.declarePropertiesAsReadOnly = true;
23+
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(Book.class));
24+
Assert.assertTrue(output.contains("readonly author: Author;"));
25+
Assert.assertTrue(output.contains("readonly title: string;"));
26+
Assert.assertTrue(output.contains("readonly name: string;"));
27+
}
28+
1929
static interface Book {
2030
Author getAuthor();
2131
String getTitle();

typescript-generator-gradle-plugin/src/main/java/cz/habarta/typescript/generator/gradle/GenerateTask.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class GenerateTask extends DefaultTask {
2727
public List<String> includePropertyAnnotations;
2828
public JsonLibrary jsonLibrary;
2929
public boolean declarePropertiesAsOptional;
30+
public boolean declarePropertiesAsReadOnly;
3031
public String removeTypeNamePrefix;
3132
public String removeTypeNameSuffix;
3233
public String addTypeNamePrefix;
@@ -93,6 +94,7 @@ public void generate() throws Exception {
9394
settings.setExcludeFilter(excludeClasses, excludeClassPatterns);
9495
settings.jsonLibrary = jsonLibrary;
9596
settings.declarePropertiesAsOptional = declarePropertiesAsOptional;
97+
settings.declarePropertiesAsReadOnly = declarePropertiesAsReadOnly;
9698
settings.removeTypeNamePrefix = removeTypeNamePrefix;
9799
settings.removeTypeNameSuffix = removeTypeNameSuffix;
98100
settings.addTypeNamePrefix = addTypeNamePrefix;

typescript-generator-maven-plugin/src/main/java/cz/habarta/typescript/generator/maven/GenerateMojo.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ public class GenerateMojo extends AbstractMojo {
130130
@Parameter
131131
private boolean declarePropertiesAsOptional;
132132

133+
/**
134+
* If true declared properties will be <code>readonly</code>.
135+
*/
136+
@Parameter
137+
private boolean declarePropertiesAsReadOnly;
138+
133139
/**
134140
* Prefix which will be removed from names of classes, interfaces, enums.
135141
* For example if set to "Json" then mapping for "JsonData" will be "Data".
@@ -390,6 +396,7 @@ public void execute() {
390396
settings.setExcludeFilter(excludeClasses, excludeClassPatterns);
391397
settings.jsonLibrary = jsonLibrary;
392398
settings.declarePropertiesAsOptional = declarePropertiesAsOptional;
399+
settings.declarePropertiesAsReadOnly = declarePropertiesAsReadOnly;
393400
settings.removeTypeNamePrefix = removeTypeNamePrefix;
394401
settings.removeTypeNameSuffix = removeTypeNameSuffix;
395402
settings.addTypeNamePrefix = addTypeNamePrefix;

0 commit comments

Comments
 (0)