add prefix, suffix to ts model

This commit is contained in:
wing328
2016-02-29 12:11:54 +08:00
parent 9f3c34dbfa
commit 74d91f4ea1
9 changed files with 400 additions and 224 deletions

View File

@@ -17,12 +17,13 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
supportsInheritance = true;
setReservedWordsLowerCase(Arrays.asList(
// local variable names used in API methods (endpoints)
"path", "queryParameters", "headerParams", "formParams", "useFormData", "deferred",
"varLocalPath", "queryParameters", "headerParams", "formParams", "useFormData", "varLocalDeferred",
"requestOptions",
// Typescript reserved words
"abstract", "await", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "debugger", "default", "delete", "do", "double", "else", "enum", "export", "extends", "false", "final", "finally", "float", "for", "function", "goto", "if", "implements", "import", "in", "instanceof", "int", "interface", "let", "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", "super", "switch", "synchronized", "this", "throw", "transient", "true", "try", "typeof", "var", "void", "volatile", "while", "with", "yield"));
languageSpecificPrimitives = new HashSet<String>(Arrays.asList(
"string",
"String",
"boolean",
"Boolean",
@@ -30,7 +31,12 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
"Integer",
"Long",
"Float",
"Object"));
"Object",
"Array",
"Date",
"number",
"any"
));
instantiationTypes.put("array", "Array");
typeMapping = new HashMap<String, String>();
@@ -116,10 +122,22 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
@Override
public String toModelName(String name) {
// model name cannot use reserved keyword, e.g. return
if (isReservedWord(name))
throw new RuntimeException(name
+ " (reserved word) cannot be used as a model name");
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
if (!StringUtils.isEmpty(modelNamePrefix)) {
name = modelNamePrefix + "_" + name;
}
if (!StringUtils.isEmpty(modelNameSuffix)) {
name = name + "_" + modelNameSuffix;
}
// model name cannot use reserved keyword, e.g. return
if (isReservedWord(name)) {
String modelName = camelize("object_" + name);
LOGGER.warn(name + " (reserved word) cannot be used as model name. Renamed to " + modelName);
return modelName;
}
// camelize the model name
// phone_number => PhoneNumber
@@ -158,7 +176,7 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
return type;
} else
type = swaggerType;
return type;
return toModelName(type);
}
@Override

View File

@@ -39,7 +39,7 @@ namespace {{package}} {
{{#allParams}}* @param {{paramName}} {{description}}
{{/allParams}}*/
public {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}extraHttpRequestParams?: any ) : ng.IHttpPromise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}{}{{/returnType}}> {
const path = this.basePath + '{{path}}'{{#pathParams}}
const localVarPath = this.basePath + '{{path}}'{{#pathParams}}
.replace('{' + '{{baseName}}' + '}', String({{paramName}})){{/pathParams}};
let queryParameters: any = {};
@@ -76,7 +76,7 @@ namespace {{package}} {
{{/formParams}}
let httpRequestParams: any = {
method: '{{httpMethod}}',
url: path,
url: localVarPath,
json: {{#hasFormParams}}false{{/hasFormParams}}{{^hasFormParams}}true{{/hasFormParams}},
{{#bodyParam}}data: {{paramName}},
{{/bodyParam}}

View File

@@ -179,7 +179,7 @@ export class {{classname}} {
{{#allParams}}* @param {{paramName}} {{description}}
{{/allParams}}*/
public {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) : Promise<{ response: http.ClientResponse; {{#returnType}}body: {{{returnType}}}; {{/returnType}}{{^returnType}}body?: any; {{/returnType}} }> {
const path = this.basePath + '{{path}}'{{#pathParams}}
const localVarPath = this.basePath + '{{path}}'{{#pathParams}}
.replace('{' + '{{baseName}}' + '}', String({{paramName}})){{/pathParams}};
let queryParameters: any = {};
let headerParams: any = this.extendObj({}, this.defaultHeaders);
@@ -212,13 +212,13 @@ export class {{classname}} {
{{/isFile}}
{{/formParams}}
let deferred = promise.defer<{ response: http.ClientResponse; {{#returnType}}body: {{{returnType}}}; {{/returnType}}{{^returnType}}body?: any; {{/returnType}} }>();
let localVarDeferred = promise.defer<{ response: http.ClientResponse; {{#returnType}}body: {{{returnType}}}; {{/returnType}}{{^returnType}}body?: any; {{/returnType}} }>();
let requestOptions: request.Options = {
method: '{{httpMethod}}',
qs: queryParameters,
headers: headerParams,
uri: path,
uri: localVarPath,
json: true,
{{#bodyParam}}
body: {{paramName}},
@@ -241,17 +241,17 @@ export class {{classname}} {
request(requestOptions, (error, response, body) => {
if (error) {
deferred.reject(error);
localVarDeferred.reject(error);
} else {
if (response.statusCode >= 200 && response.statusCode <= 299) {
deferred.resolve({ response: response, body: body });
localVarDeferred.resolve({ response: response, body: body });
} else {
deferred.reject({ response: response, body: body });
localVarDeferred.reject({ response: response, body: body });
}
}
});
return deferred.promise;
return localVarDeferred.promise;
}
{{/operation}}
}

View File

@@ -59,7 +59,7 @@ public class TypeScriptAngularModelTest {
final CodegenProperty property3 = cm.vars.get(2);
Assert.assertEquals(property3.baseName, "createdAt");
Assert.assertEquals(property3.complexType, "Date");
Assert.assertEquals(property3.complexType, null);
Assert.assertEquals(property3.datatype, "Date");
Assert.assertEquals(property3.name, "createdAt");
Assert.assertEquals(property3.defaultValue, "null");

View File

@@ -59,7 +59,7 @@ public class TypeScriptNodeModelTest {
final CodegenProperty property3 = cm.vars.get(2);
Assert.assertEquals(property3.baseName, "createdAt");
Assert.assertEquals(property3.complexType, "Date");
Assert.assertEquals(property3.complexType, null);
Assert.assertEquals(property3.datatype, "Date");
Assert.assertEquals(property3.name, "createdAt");
Assert.assertEquals(property3.defaultValue, "null");