@Override public String toModelName(final String name) { final String sanitizedName = sanitizeName(modelNamePrefix + name + modelNameSuffix); // camelize the model name // phone_number => PhoneNumber final String camelizedName = camelize(sanitizedName); // model name cannot use reserved keyword, e.g. return if (isReservedWord(camelizedName)) { final String modelName = "Model" + camelizedName; LOGGER.warn( camelizedName + " (reserved word) cannot be used as model name. Renamed to " + modelName); return modelName; } // model name starts with number if (name.matches("^\\d.*")) { final String modelName = "Model" + camelizedName; // e.g. 200Response => Model200Response (after camelize) LOGGER.warn( name + " (model name starts with number) cannot be used as model name. Renamed to " + modelName); return modelName; } return camelizedName; }
@Override public String toModelFilename(String name) { name = sanitizeName( name); // FIXME: a parameter should not be assigned. Also declare the methods parameters // as 'final'. // remove dollar sign name = name.replaceAll("$", ""); // model name cannot use reserved keyword, e.g. return if (isReservedWord(name)) { LOGGER.warn( name + " (reserved word) cannot be used as model filename. Renamed to " + underscore(dropDots("model_" + name))); name = "model_" + name; // e.g. return => ModelReturn (after camelize) } // model name starts with number if (name.matches("^\\d.*")) { LOGGER.warn( name + " (model name starts with number) cannot be used as model name. Renamed to " + underscore("model_" + name)); name = "model_" + name; // e.g. 200Response => Model200Response (after camelize) } if (!StringUtils.isEmpty(modelNamePrefix)) { name = modelNamePrefix + "_" + name; } if (!StringUtils.isEmpty(modelNameSuffix)) { name = name + "_" + modelNameSuffix; } // underscore the model file name // PhoneNumber => phone_number return underscore(dropDots(name)); }
@Override public String toOperationId(String operationId) { // throw exception if method name is empty (should not occur as an auto-generated method name // will be used) if (StringUtils.isEmpty(operationId)) { throw new RuntimeException("Empty method name (operationId) not allowed"); } // method name cannot use reserved keyword, e.g. return if (isReservedWord(operationId)) { LOGGER.warn( operationId + " (reserved word) cannot be used as method name. Renamed to " + underscore(sanitizeName("call_" + operationId))); operationId = "call_" + operationId; } return underscore(sanitizeName(operationId)); }
@Override public String toOperationId(String operationId) { // throw exception if method name is empty if (StringUtils.isEmpty(operationId)) { throw new RuntimeException("Empty method/operation name (operationId) not allowed"); } operationId = camelize(sanitizeName(operationId), true); // method name cannot use reserved keyword, e.g. return if (isReservedWord(operationId)) { String newOperationId = camelize("call_" + operationId, true); LOGGER.warn( operationId + " (reserved word) cannot be used as method name. Renamed to " + newOperationId); return newOperationId; } return operationId; }
@Override public void setParameterExampleValue(CodegenParameter p) { String example; if (p.defaultValue == null) { example = p.example; } else { example = p.defaultValue; } String type = p.baseType; if (type == null) { type = p.dataType; } if ("String".equalsIgnoreCase(type) || "str".equalsIgnoreCase(type)) { if (example == null) { example = p.paramName + "_example"; } example = "'" + escapeText(example) + "'"; } else if ("Integer".equals(type) || "int".equals(type)) { if (example == null) { example = "56"; } } else if ("Float".equalsIgnoreCase(type) || "Double".equalsIgnoreCase(type)) { if (example == null) { example = "3.4"; } } else if ("BOOLEAN".equalsIgnoreCase(type) || "bool".equalsIgnoreCase(type)) { if (example == null) { example = "True"; } } else if ("file".equalsIgnoreCase(type)) { if (example == null) { example = "/path/to/file"; } example = "'" + escapeText(example) + "'"; } else if ("Date".equalsIgnoreCase(type)) { if (example == null) { example = "2013-10-20"; } example = "'" + escapeText(example) + "'"; } else if ("DateTime".equalsIgnoreCase(type)) { if (example == null) { example = "2013-10-20T19:20:30+01:00"; } example = "'" + escapeText(example) + "'"; } else if (!languageSpecificPrimitives.contains(type)) { // type is a model class, e.g. User example = this.packageName + "." + type + "()"; } else { LOGGER.warn("Type " + type + " not handled properly in setParameterExampleValue"); } if (example == null) { example = "NULL"; } else if (Boolean.TRUE.equals(p.isListContainer)) { example = "[" + example + "]"; } else if (Boolean.TRUE.equals(p.isMapContainer)) { example = "{'key': " + example + "}"; } p.example = example; }