Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Go] Adding enum support #5635

Merged
merged 2 commits into from
May 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void processOpts() {

additionalProperties.put(CodegenConstants.PACKAGE_NAME, packageName);
additionalProperties.put(CodegenConstants.PACKAGE_VERSION, packageVersion);

additionalProperties.put("apiDocPath", apiDocPath);
additionalProperties.put("modelDocPath", modelDocPath);

Expand Down Expand Up @@ -180,10 +180,10 @@ public String escapeReservedWord(String name)
// - 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) + '_';
}

Expand Down Expand Up @@ -465,7 +465,7 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
}
}

return objs;
return postProcessModelsEnum(objs);
}

@Override
Expand Down Expand Up @@ -499,4 +499,65 @@ public Map<String, String> createMapping(String key, String value){

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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -160,8 +160,8 @@ public GoServerCodegen() {
);
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"));
}

Expand Down Expand Up @@ -222,7 +222,7 @@ public String toApiName(String name) {
public String escapeReservedWord(String name) {
if(this.reservedWordsMappings().containsKey(name)) {
return this.reservedWordsMappings().get(name);
}
}
return "_" + name; // add an underscore to the name
}

Expand Down Expand Up @@ -306,7 +306,7 @@ else if (p instanceof MapProperty) {

return toModelName(swaggerType);
}

@Override
public String getSwaggerType(Property p) {
String swaggerType = super.getSwaggerType(p);
Expand Down Expand Up @@ -341,4 +341,69 @@ public String escapeUnsafeCharacters(String input) {
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);
}
}
17 changes: 13 additions & 4 deletions modules/swagger-codegen/src/main/resources/go/model.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
10 changes: 8 additions & 2 deletions samples/client/petstore/go/go-petstore/enum_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
)
10 changes: 8 additions & 2 deletions samples/client/petstore/go/go-petstore/outer_enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)