This commit is contained in:
wing328 2017-05-15 22:12:16 +08:00
commit 9936018090
5 changed files with 164 additions and 17 deletions

View File

@ -151,7 +151,7 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig {
additionalProperties.put(CodegenConstants.PACKAGE_NAME, packageName);
additionalProperties.put(CodegenConstants.PACKAGE_VERSION, packageVersion);
additionalProperties.put("apiDocPath", apiDocPath);
additionalProperties.put("modelDocPath", modelDocPath);
@ -180,10 +180,10 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig {
// - XName
// - X_Name
// ... or maybe a suffix?
// - Name_ ... think this will work.
// - Name_ ... think this will work.
if(this.reservedWordsMappings().containsKey(name)) {
return this.reservedWordsMappings().get(name);
}
}
return camelize(name) + '_';
}
@ -465,7 +465,7 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig {
}
}
return objs;
return postProcessModelsEnum(objs);
}
@Override
@ -499,4 +499,65 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig {
return customImport;
}
@Override
public String toEnumValue(String value, String datatype) {
if ("int".equals(datatype) || "double".equals(datatype) || "float".equals(datatype)) {
return value;
} else {
return escapeText(value);
}
}
@Override
public String toEnumDefaultValue(String value, String datatype) {
return datatype + "_" + value;
}
@Override
public String toEnumVarName(String name, String datatype) {
if (name.length() == 0) {
return "EMPTY";
}
// number
if ("int".equals(datatype) || "double".equals(datatype) || "float".equals(datatype)) {
String varName = name;
varName = varName.replaceAll("-", "MINUS_");
varName = varName.replaceAll("\\+", "PLUS_");
varName = varName.replaceAll("\\.", "_DOT_");
return varName;
}
// for symbol, e.g. $, #
if (getSymbolName(name) != null) {
return getSymbolName(name).toUpperCase();
}
// string
String enumName = sanitizeName(underscore(name).toUpperCase());
enumName = enumName.replaceFirst("^_", "");
enumName = enumName.replaceFirst("_$", "");
if (isReservedWord(enumName) || enumName.matches("\\d.*")) { // reserved word or starts with number
return escapeReservedWord(enumName);
} else {
return enumName;
}
}
@Override
public String toEnumName(CodegenProperty property) {
String enumName = underscore(toModelName(property.name)).toUpperCase();
// remove [] for array or map of enum
enumName = enumName.replace("[]", "");
if (enumName.matches("\\d.*")) { // starts with number
return "_" + enumName;
} else {
return enumName;
}
}
}

View File

@ -34,7 +34,7 @@ public class GoServerCodegen extends DefaultCodegen implements CodegenConfig {
protected int serverPort = 8080;
protected String projectName = "swagger-server";
protected String apiPath = "go";
public GoServerCodegen() {
super();
@ -160,8 +160,8 @@ public class GoServerCodegen extends DefaultCodegen implements CodegenConfig {
);
supportingFiles.add(new SupportingFile("main.mustache", "", "main.go"));
supportingFiles.add(new SupportingFile("routers.mustache", apiPath, "routers.go"));
supportingFiles.add(new SupportingFile("logger.mustache", apiPath, "logger.go"));
supportingFiles.add(new SupportingFile("app.mustache", apiPath, "app.yaml"));
supportingFiles.add(new SupportingFile("logger.mustache", apiPath, "logger.go"));
supportingFiles.add(new SupportingFile("app.mustache", apiPath, "app.yaml"));
writeOptional(outputFolder, new SupportingFile("README.mustache", apiPath, "README.md"));
}
@ -222,7 +222,7 @@ public class GoServerCodegen extends DefaultCodegen implements CodegenConfig {
public String escapeReservedWord(String name) {
if(this.reservedWordsMappings().containsKey(name)) {
return this.reservedWordsMappings().get(name);
}
}
return "_" + name; // add an underscore to the name
}
@ -306,7 +306,7 @@ public class GoServerCodegen extends DefaultCodegen implements CodegenConfig {
return toModelName(swaggerType);
}
@Override
public String getSwaggerType(Property p) {
String swaggerType = super.getSwaggerType(p);
@ -341,4 +341,69 @@ public class GoServerCodegen extends DefaultCodegen implements CodegenConfig {
return input.replace("*/", "*_/").replace("/*", "/_*");
}
@Override
public String toEnumValue(String value, String datatype) {
if ("int".equals(datatype) || "double".equals(datatype) || "float".equals(datatype)) {
return value;
} else {
return "\'" + escapeText(value) + "\'";
}
}
@Override
public String toEnumDefaultValue(String value, String datatype) {
return datatype + "_" + value;
}
@Override
public String toEnumVarName(String name, String datatype) {
if (name.length() == 0) {
return "EMPTY";
}
// number
if ("int".equals(datatype) || "double".equals(datatype) || "float".equals(datatype)) {
String varName = name;
varName = varName.replaceAll("-", "MINUS_");
varName = varName.replaceAll("\\+", "PLUS_");
varName = varName.replaceAll("\\.", "_DOT_");
return varName;
}
// for symbol, e.g. $, #
if (getSymbolName(name) != null) {
return getSymbolName(name).toUpperCase();
}
// string
String enumName = sanitizeName(underscore(name).toUpperCase());
enumName = enumName.replaceFirst("^_", "");
enumName = enumName.replaceFirst("_$", "");
if (isReservedWord(enumName) || enumName.matches("\\d.*")) { // reserved word or starts with number
return escapeReservedWord(enumName);
} else {
return enumName;
}
}
@Override
public String toEnumName(CodegenProperty property) {
String enumName = underscore(toModelName(property.name)).toUpperCase();
// remove [] for array or map of enum
enumName = enumName.replace("[]", "");
if (enumName.matches("\\d.*")) { // starts with number
return "_" + enumName;
} else {
return enumName;
}
}
@Override
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
// process enum in models
return postProcessModelsEnum(objs);
}
}

View File

@ -4,12 +4,21 @@ package {{packageName}}
import ({{/imports}}{{#imports}}
"{{import}}"{{/imports}}{{#imports}}
)
{{/imports}}{{#model}}{{#description}}
{{/imports}}{{#model}}{{#isEnum}}{{#description}}// {{{classname}}} : {{{description}}}{{/description}}
type {{{name}}} {{^format}}{{dataType}}{{/format}}{{#format}}{{{format}}}{{/format}}
// List of {{{name}}}
const (
{{#allowableValues}}
{{#enumVars}}
{{name}} {{{classname}}} = "{{{value}}}"
{{/enumVars}}
{{/allowableValues}}
){{/isEnum}}{{^isEnum}}{{#description}}
// {{{description}}}{{/description}}
type {{classname}} struct {
{{#vars}}{{#description}}
// {{{description}}}{{/description}}
{{name}} {{^isPrimitiveType}}{{^isContainer}}{{^isDateTime}}*{{/isDateTime}}{{/isContainer}}{{/isPrimitiveType}}{{{datatype}}} `json:"{{baseName}}{{^required}},omitempty{{/required}}"`
{{name}} {{^isEnum}}{{^isPrimitiveType}}{{^isContainer}}{{^isDateTime}}*{{/isDateTime}}{{/isContainer}}{{/isPrimitiveType}}{{/isEnum}}{{{datatype}}} `json:"{{baseName}}{{^required}},omitempty{{/required}}"`
{{/vars}}
}
{{/model}}{{/models}}
}{{/isEnum}}{{/model}}{{/models}}

View File

@ -10,5 +10,11 @@
package petstore
type EnumClass struct {
}
type EnumClass string
// List of EnumClass
const (
ABC EnumClass = "_abc"
EFG EnumClass = "-efg"
XYZ EnumClass = "(xyz)"
)

View File

@ -10,5 +10,11 @@
package petstore
type OuterEnum struct {
}
type OuterEnum string
// List of OuterEnum
const (
PLACED OuterEnum = "placed"
APPROVED OuterEnum = "approved"
DELIVERED OuterEnum = "delivered"
)