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

adding enum support for golang #5631

Closed
wants to merge 3 commits into from
Closed
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 @@ -499,4 +499,50 @@ 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;
}
}
}
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 @@ -274,6 +274,52 @@ public String toModelFilename(String name) {
return camelize(name);
}

@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 getTypeDeclaration(Property p) {
if(p instanceof ArrayProperty) {
Expand Down Expand Up @@ -306,7 +352,7 @@ else if (p instanceof MapProperty) {

return toModelName(swaggerType);
}

@Override
public String getSwaggerType(Property p) {
String swaggerType = super.getSwaggerType(p);
Expand Down
16 changes: 11 additions & 5 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,18 @@ package {{packageName}}
import ({{/imports}}{{#imports}}
"{{import}}"{{/imports}}{{#imports}}
)
{{/imports}}{{#model}}{{#description}}
// {{{description}}}{{/description}}
{{/imports}}{{#model}}{{#isEnum}}{{#description}}// {{{name}}} : {{{description}}}{{/description}}
type {{{name}}} {{^format}}string{{/format}}{{#format}}{{{format}}}{{/format}}

// List of {{{name}}}
const (
{{#allowableValues}}{{#values}}{{this}} {{name}} = "{{this}}"
{{/values}}{{/allowableValues}}
){{/isEnum}}{{^isEnum}}{{#description}}
// {{classname}} : {{{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}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.3.0-SNAPSHOT
2 changes: 1 addition & 1 deletion samples/client/petstore-security-test/go/model_return.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package swagger

// Model for testing reserved words *_/ ' \" =end -- \\r\\n \\n \\r
// ModelReturn : Model for testing reserved words *_/ ' \" =end -- \\r\\n \\n \\r
type ModelReturn struct {

// property description *_/ ' \" =end -- \\r\\n \\n \\r
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.3.0-SNAPSHOT
2 changes: 1 addition & 1 deletion samples/client/petstore/go/go-petstore/class_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package petstore

// Model for testing model with \"_class\" property
// ClassModel : Model for testing model with \"_class\" property
type ClassModel struct {

Class string `json:"_class,omitempty"`
Expand Down
11 changes: 9 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,12 @@

package petstore

type EnumClass struct {
}
type EnumClass string

// List of EnumClass
const (
_abc EnumClass = "_abc"
-efg EnumClass = "-efg"
(xyz) EnumClass = "(xyz)"

)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package petstore

// Model for testing model name starting with number
// Model200Response : Model for testing model name starting with number
type Model200Response struct {

Name int32 `json:"name,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/go/go-petstore/model_return.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package petstore

// Model for testing reserved words
// ModelReturn : Model for testing reserved words
type ModelReturn struct {

Return_ int32 `json:"return,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/go/go-petstore/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package petstore

// Model for testing model name same as property name
// Name : Model for testing model name same as property name
type Name struct {

Name int32 `json:"name"`
Expand Down
11 changes: 9 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,12 @@

package petstore

type OuterEnum struct {
}
type OuterEnum string

// List of OuterEnum
const (
placed OuterEnum = "placed"
approved OuterEnum = "approved"
delivered OuterEnum = "delivered"

)