Skip to content

Commit e8ad284

Browse files
sorting emitted types by category
- jaxrs client is emitted before data interfaces
1 parent 5beeb24 commit e8ad284

File tree

6 files changed

+45
-16
lines changed

6 files changed

+45
-16
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ private <T> TsBeanModel processBean(SymbolTable symbolTable, Model model, Map<Ty
152152
properties.add(0, new TsPropertyModel(bean.getDiscriminantProperty(), discriminantType, settings.declarePropertiesAsReadOnly, null));
153153
}
154154

155-
return new TsBeanModel(bean.getOrigin(), isClass, beanIdentifier, typeParameters, parentType, bean.getTaggedUnionClasses(), interfaces, properties, null, null, bean.getComments());
155+
return new TsBeanModel(bean.getOrigin(), TsBeanCategory.Data, isClass, beanIdentifier, typeParameters, parentType, bean.getTaggedUnionClasses(), interfaces, properties, null, null, bean.getComments());
156156
}
157157

158158
private List<TsPropertyModel> processProperties(SymbolTable symbolTable, Model model, BeanModel bean, String prefix, String suffix) {
@@ -318,7 +318,7 @@ private Symbol createJaxrsResponseType(SymbolTable symbolTable, TsModel tsModel)
318318
private TsModel createJaxrsInterface(SymbolTable symbolTable, TsModel tsModel, JaxrsApplicationModel jaxrsApplication, Symbol responseSymbol, TsType optionsType) {
319319
final List<TsMethodModel> methods = processJaxrsMethods(jaxrsApplication, symbolTable, responseSymbol, optionsType, false);
320320
final String applicationName = getApplicationName(jaxrsApplication);
321-
final TsBeanModel interfaceModel = new TsBeanModel(Application.class, false, symbolTable.getSyntheticSymbol(applicationName), null, null, null, null, null, null, methods, null);
321+
final TsBeanModel interfaceModel = new TsBeanModel(Application.class, TsBeanCategory.Service, false, symbolTable.getSyntheticSymbol(applicationName), null, null, null, null, null, null, methods, null);
322322
tsModel.getBeans().add(interfaceModel);
323323
return tsModel;
324324
}
@@ -327,7 +327,7 @@ private TsModel createJaxrsClient(SymbolTable symbolTable, TsModel tsModel, Jaxr
327327
final Symbol httpClientSymbol = symbolTable.getSyntheticSymbol("HttpClient");
328328

329329
// HttpClient interface
330-
tsModel.getBeans().add(new TsBeanModel(null, false, httpClientSymbol, null, null, null, null, null, null, Arrays.asList(
330+
tsModel.getBeans().add(new TsBeanModel(null, TsBeanCategory.Service, false, httpClientSymbol, null, null, null, null, null, null, Arrays.asList(
331331
new TsMethodModel("request", new TsType.GenericReferenceType(responseSymbol, TsType.Any), Arrays.asList(
332332
new TsParameterModel("requestConfig", new TsType.ObjectType(
333333
new TsProperty("method", TsType.String),
@@ -349,7 +349,7 @@ private TsModel createJaxrsClient(SymbolTable symbolTable, TsModel tsModel, Jaxr
349349
final String applicationName = getApplicationName(jaxrsApplication);
350350
final String applicationClientName = applicationName + "Client";
351351
final TsType interfaceType = settings.generateJaxrsApplicationInterface ? new TsType.ReferenceType(symbolTable.getSyntheticSymbol(applicationName)) : null;
352-
final TsBeanModel clientModel = new TsBeanModel(Application.class, true, symbolTable.getSyntheticSymbol(applicationClientName), null, null, null, Utils.listFromNullable(interfaceType), null, constructor, methods, null);
352+
final TsBeanModel clientModel = new TsBeanModel(Application.class, TsBeanCategory.Service, true, symbolTable.getSyntheticSymbol(applicationClientName), null, null, null, Utils.listFromNullable(interfaceType), null, constructor, methods, null);
353353
tsModel.getBeans().add(clientModel);
354354
return tsModel;
355355
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class TsAliasModel extends TsDeclarationModel {
1212
private final TsType definition;
1313

1414
public TsAliasModel(Class<?> origin, Symbol name, List<TsType.GenericVariableType> typeParameters, TsType definition, List<String> comments) {
15-
super(origin, name, comments);
15+
super(origin, null, name, comments);
1616
this.typeParameters = typeParameters != null ? typeParameters : Collections.<TsType.GenericVariableType>emptyList();
1717
this.definition = definition;
1818
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
package cz.habarta.typescript.generator.emitter;
3+
4+
5+
public enum TsBeanCategory {
6+
7+
// order of these constants determines order of emitted declarations
8+
Service,
9+
Data,
10+
11+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public class TsBeanModel extends TsDeclarationModel {
1818
private final TsConstructorModel constructor;
1919
private final List<TsMethodModel> methods;
2020

21-
public TsBeanModel(Class<?> origin, boolean isClass, Symbol name, List<TsType.GenericVariableType> typeParameters, TsType parent, List<Class<?>> taggedUnionClasses, List<TsType> interfaces, List<TsPropertyModel> properties, TsConstructorModel constructor, List<TsMethodModel> methods, List<String> comments) {
22-
super(origin, name, comments);
21+
public TsBeanModel(Class<?> origin, TsBeanCategory category, boolean isClass, Symbol name, List<TsType.GenericVariableType> typeParameters, TsType parent, List<Class<?>> taggedUnionClasses, List<TsType> interfaces, List<TsPropertyModel> properties, TsConstructorModel constructor, List<TsMethodModel> methods, List<String> comments) {
22+
super(origin, category, name, comments);
2323
this.isClass = isClass;
2424
this.typeParameters = Utils.listFromNullable(typeParameters);
2525
this.parent = parent;
@@ -76,7 +76,7 @@ public List<TsPropertyModel> getProperties() {
7676
}
7777

7878
public TsBeanModel withProperties(List<TsPropertyModel> properties) {
79-
return new TsBeanModel(origin, isClass, name, typeParameters, parent, taggedUnionClasses, interfaces, properties, constructor, methods, comments);
79+
return new TsBeanModel(origin, category, isClass, name, typeParameters, parent, taggedUnionClasses, interfaces, properties, constructor, methods, comments);
8080
}
8181

8282
public TsConstructorModel getConstructor() {
@@ -88,7 +88,7 @@ public List<TsMethodModel> getMethods() {
8888
}
8989

9090
public TsBeanModel withMethods(List<TsMethodModel> methods) {
91-
return new TsBeanModel(origin, isClass, name, typeParameters, parent, taggedUnionClasses, interfaces, properties, constructor, methods, comments);
91+
return new TsBeanModel(origin, category, isClass, name, typeParameters, parent, taggedUnionClasses, interfaces, properties, constructor, methods, comments);
9292
}
9393

9494
}

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@
88
public class TsDeclarationModel implements Comparable<TsDeclarationModel> {
99

1010
protected final Class<?> origin;
11+
protected final TsBeanCategory category;
1112
protected final Symbol name;
1213
protected final List<String> comments;
1314

14-
public TsDeclarationModel(Symbol name, List<String> comments) {
15-
this(null, name, comments);
16-
}
17-
18-
public TsDeclarationModel(Class<?> origin, Symbol name, List<String> comments) {
15+
public TsDeclarationModel(Class<?> origin, TsBeanCategory category, Symbol name, List<String> comments) {
1916
this.origin = origin;
17+
this.category = category;
2018
this.name = name;
2119
this.comments = comments;
2220
}
@@ -35,7 +33,27 @@ public List<String> getComments() {
3533

3634
@Override
3735
public int compareTo(TsDeclarationModel o) {
38-
return name.toString().compareTo(o.name.toString());
36+
final int categoryResult = compare(this.category, o.category);
37+
if (categoryResult != 0) {
38+
return categoryResult;
39+
}
40+
final int nameResult = compare(this.name.toString(), o.name.toString());
41+
if (nameResult != 0) {
42+
return nameResult;
43+
}
44+
return 0;
45+
}
46+
47+
/**
48+
* Natural order with null last.
49+
* Remove on Java 8.
50+
*/
51+
private static <T extends Comparable<T>> int compare(T o1, T o2) {
52+
if (o1 != null) {
53+
return o2 != null ? o1.compareTo(o2) : -1;
54+
} else {
55+
return o2 != null ? 1 : 0;
56+
}
3957
}
4058

4159
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class TsEnumModel<T> extends TsDeclarationModel {
1515
private final List<EnumMemberModel<T>> members;
1616

1717
public TsEnumModel(Class<?> origin, Symbol name, EnumKind<T> kind, List<EnumMemberModel<T>> members, List<String> comments) {
18-
super(origin, name, comments);
18+
super(origin, null, name, comments);
1919
this.kind = kind;
2020
this.members = members;
2121
}

0 commit comments

Comments
 (0)