typescript-angular: add serviceSuffix and serviceFileSuffix parameters suffix (#418)

This commit is contained in:
tomvangreen 2018-07-03 13:38:21 +02:00 committed by Jérémie Bresson
parent be68ef502e
commit dcc0c17a29
2 changed files with 87 additions and 15 deletions

View File

@ -34,6 +34,8 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
private static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm"); private static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm");
private static final String X_DISCRIMINATOR_TYPE = "x-discriminator-value"; private static final String X_DISCRIMINATOR_TYPE = "x-discriminator-value";
private static String CLASS_NAME_SUFFIX_PATTERN = "^[a-zA-Z0-9]*$";
private static String FILE_NAME_SUFFIX_PATTERN = "^[a-zA-Z0-9.-]*$";
public static final String NPM_NAME = "npmName"; public static final String NPM_NAME = "npmName";
public static final String NPM_VERSION = "npmVersion"; public static final String NPM_VERSION = "npmVersion";
@ -45,12 +47,16 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
public static final String PROVIDED_IN_ROOT = "providedInRoot"; public static final String PROVIDED_IN_ROOT = "providedInRoot";
public static final String SERVICE_SUFFIX = "serviceSuffix"; public static final String SERVICE_SUFFIX = "serviceSuffix";
public static final String SERVICE_FILE_SUFFIX = "serviceFileSuffix"; public static final String SERVICE_FILE_SUFFIX = "serviceFileSuffix";
public static final String MODEL_SUFFIX = "modelSuffix";
public static final String MODEL_FILE_SUFFIX = "modelFileSuffix";
protected String npmName = null; protected String npmName = null;
protected String npmVersion = "1.0.0"; protected String npmVersion = "1.0.0";
protected String npmRepository = null; protected String npmRepository = null;
protected String serviceSuffix = "Service"; protected String serviceSuffix = "Service";
protected String serviceFileSuffix = ".service"; protected String serviceFileSuffix = ".service";
protected String modelSuffix = "";
protected String modelFileSuffix = "";
private boolean taggedUnions = false; private boolean taggedUnions = false;
@ -86,6 +92,8 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
this.cliOptions.add(new CliOption(NG_VERSION, "The version of Angular. Default is '4.3'")); this.cliOptions.add(new CliOption(NG_VERSION, "The version of Angular. Default is '4.3'"));
this.cliOptions.add(new CliOption(SERVICE_SUFFIX, "The suffix of the generated service. Default is 'Service'.")); this.cliOptions.add(new CliOption(SERVICE_SUFFIX, "The suffix of the generated service. Default is 'Service'."));
this.cliOptions.add(new CliOption(SERVICE_FILE_SUFFIX, "The suffix of the file of the generated service (service<suffix>.ts). Default is '.service'.")); this.cliOptions.add(new CliOption(SERVICE_FILE_SUFFIX, "The suffix of the file of the generated service (service<suffix>.ts). Default is '.service'."));
this.cliOptions.add(new CliOption(MODEL_SUFFIX, "The suffix of the generated model. Default is ''."));
this.cliOptions.add(new CliOption(MODEL_FILE_SUFFIX, "The suffix of the file of the generated model (model<suffix>.ts). Default is ''."));
} }
@Override @Override
@ -166,9 +174,19 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
} }
if (additionalProperties.containsKey(SERVICE_SUFFIX)) { if (additionalProperties.containsKey(SERVICE_SUFFIX)) {
serviceSuffix = additionalProperties.get(SERVICE_SUFFIX).toString(); serviceSuffix = additionalProperties.get(SERVICE_SUFFIX).toString();
validateClassSuffixArgument("Service", serviceSuffix);
} }
if (additionalProperties.containsKey(SERVICE_FILE_SUFFIX)) { if (additionalProperties.containsKey(SERVICE_FILE_SUFFIX)) {
serviceFileSuffix = additionalProperties.get(SERVICE_FILE_SUFFIX).toString(); serviceFileSuffix = additionalProperties.get(SERVICE_FILE_SUFFIX).toString();
validateFileSuffixArgument("Service", serviceFileSuffix);
}
if (additionalProperties.containsKey(MODEL_SUFFIX)) {
modelSuffix = additionalProperties.get(MODEL_SUFFIX).toString();
validateClassSuffixArgument("Model", modelSuffix);
}
if (additionalProperties.containsKey(MODEL_FILE_SUFFIX)) {
modelFileSuffix = additionalProperties.get(MODEL_FILE_SUFFIX).toString();
validateFileSuffixArgument("Model", modelFileSuffix);
} }
} }
@ -364,6 +382,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
/** /**
* Finds and returns a path parameter of an operation by its name * Finds and returns a path parameter of an operation by its name
*
* @param operation * @param operation
* @param parameterName * @param parameterName
* @return * @return
@ -380,14 +399,12 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
@Override @Override
public Map<String, Object> postProcessModels(Map<String, Object> objs) { public Map<String, Object> postProcessModels(Map<String, Object> objs) {
Map<String, Object> result = super.postProcessModels(objs); Map<String, Object> result = super.postProcessModels(objs);
return postProcessModelsEnum(result); return postProcessModelsEnum(result);
} }
@Override @Override
public Map<String, Object> postProcessAllModels(Map<String, Object> objs) { public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
Map<String, Object> result = super.postProcessAllModels(objs); Map<String, Object> result = super.postProcessAllModels(objs);
for (Map.Entry<String, Object> entry : result.entrySet()) { for (Map.Entry<String, Object> entry : result.entrySet()) {
Map<String, Object> inner = (Map<String, Object>) entry.getValue(); Map<String, Object> inner = (Map<String, Object>) entry.getValue();
List<Map<String, Object>> models = (List<Map<String, Object>>) inner.get("models"); List<Map<String, Object>> models = (List<Map<String, Object>>) inner.get("models");
@ -416,8 +433,9 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
for (String im : imports) { for (String im : imports) {
if (!im.equals(cm.classname)) { if (!im.equals(cm.classname)) {
HashMap<String, String> tsImport = new HashMap<>(); HashMap<String, String> tsImport = new HashMap<>();
// TVG: This is used as class name in the import statements of the model file
tsImport.put("classname", im); tsImport.put("classname", im);
tsImport.put("filename", toModelFilename(im)); tsImport.put("filename", toModelFilename(removeModelSuffixIfNecessary(im)));
tsImports.add(tsImport); tsImports.add(tsImport);
} }
} }
@ -437,7 +455,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
if (name.length() == 0) { if (name.length() == 0) {
return "default.service"; return "default.service";
} }
return camelize(name, true) + serviceFileSuffix; return camelize(removeModelSuffixIfNecessary(name), true) + serviceFileSuffix;
} }
@Override @Override
@ -447,7 +465,8 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
@Override @Override
public String toModelFilename(String name) { public String toModelFilename(String name) {
return camelize(toModelName(name), true); String modelName = toModelName(name);
return camelize(removeModelSuffixIfNecessary(modelName), true) + modelFileSuffix;
} }
@Override @Override
@ -486,7 +505,56 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
private String getModelnameFromModelFilename(String filename) { private String getModelnameFromModelFilename(String filename) {
String name = filename.substring((modelPackage() + "/").length()); String name = filename.substring((modelPackage() + "/").length());
return camelize(name); // Remove the file suffix and add the class suffix.
// This is needed because the model file suffix might not be the same as
// the model suffix.
if (modelFileSuffix.length() > 0) {
name = name.substring(0, name.length() - modelFileSuffix.length());
}
return camelize(name) + modelSuffix;
} }
@Override
public String toModelName(String name) {
String modelName = super.toModelName(name);
if (modelSuffix.length() == 0 || modelName.endsWith(modelSuffix)) {
return modelName;
}
return modelName + modelSuffix;
}
private String removeModelSuffixIfNecessary(String name) {
if (modelSuffix.length() == 0 || !name.endsWith(modelSuffix)) {
return name;
}
return name.substring(0, name.length() - modelSuffix.length());
}
/**
* Validates that the given string value only contains '-', '.' and alpha numeric characters.
* Throws an IllegalArgumentException, if the string contains any other characters.
* @param argument The name of the argument being validated. This is only used for displaying an error message.
* @param value The value that is being validated.
*/
private void validateFileSuffixArgument(String argument, String value) {
if (!value.matches(FILE_NAME_SUFFIX_PATTERN)) {
throw new IllegalArgumentException(
String.format("%s file suffix only allows '.', '-' and alphanumeric characters.", argument)
);
}
}
/**
* Validates that the given string value only contains alpha numeric characters.
* Throws an IllegalArgumentException, if the string contains any other characters.
* @param argument The name of the argument being validated. This is only used for displaying an error message.
* @param value The value that is being validated.
*/
private void validateClassSuffixArgument(String argument, String value) {
if (!value.matches(CLASS_NAME_SUFFIX_PATTERN)) {
throw new IllegalArgumentException(
String.format("%s class suffix only allows alphanumeric characters.", argument)
);
}
}
} }

View File

@ -37,6 +37,8 @@ public class TypeScriptAngularClientOptionsProvider implements OptionsProvider {
public static final String PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = "true"; public static final String PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = "true";
public static String SERVICE_SUFFIX = "Service"; public static String SERVICE_SUFFIX = "Service";
public static String SERVICE_FILE_SUFFIX = ".service"; public static String SERVICE_FILE_SUFFIX = ".service";
public static String MODEL_SUFFIX = "";
public static String MODEL_FILE_SUFFIX = "";
@Override @Override
public String getLanguage() { public String getLanguage() {
@ -60,6 +62,8 @@ public class TypeScriptAngularClientOptionsProvider implements OptionsProvider {
.put(TypeScriptAngularClientCodegen.NG_VERSION, NG_VERSION) .put(TypeScriptAngularClientCodegen.NG_VERSION, NG_VERSION)
.put(TypeScriptAngularClientCodegen.SERVICE_SUFFIX, SERVICE_SUFFIX) .put(TypeScriptAngularClientCodegen.SERVICE_SUFFIX, SERVICE_SUFFIX)
.put(TypeScriptAngularClientCodegen.SERVICE_FILE_SUFFIX, SERVICE_FILE_SUFFIX) .put(TypeScriptAngularClientCodegen.SERVICE_FILE_SUFFIX, SERVICE_FILE_SUFFIX)
.put(TypeScriptAngularClientCodegen.MODEL_SUFFIX, MODEL_SUFFIX)
.put(TypeScriptAngularClientCodegen.MODEL_FILE_SUFFIX, MODEL_FILE_SUFFIX)
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE) .put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
.put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE) .put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)
.build(); .build();