Skip to content

Commit

Permalink
[Go] add option to use class as enum prefix (#3675)
Browse files Browse the repository at this point in the history
* add option to use class as enum prefix

* update doc

* fix go test

* update go xml petstore

* fix format
  • Loading branch information
wing328 authored Aug 19, 2019
1 parent 7abd2e1 commit 4575b30
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 16 deletions.
1 change: 1 addition & 0 deletions docs/generators/go-experimental.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ sidebar_label: go-experimental
|isGoSubmodule|whether the generated Go module is a submodule| |false|
|withGoCodegenComment|whether to include Go codegen comment to disable Go Lint and collapse by default GitHub in PRs and diffs| |false|
|withXml|whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)| |false|
|enumClassPrefix|Prefix enum with class name| |false|
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
1 change: 1 addition & 0 deletions docs/generators/go.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ sidebar_label: go
|isGoSubmodule|whether the generated Go module is a submodule| |false|
|withGoCodegenComment|whether to include Go codegen comment to disable Go Lint and collapse by default GitHub in PRs and diffs| |false|
|withXml|whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)| |false|
|enumClassPrefix|Prefix enum with class name| |false|
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,7 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,

public static final String EXCEPTION_ON_FAILURE = "returnExceptionOnFailure";
public static final String EXCEPTION_ON_FAILURE_DESC = "Throw an exception on non success response codes";

public static final String ENUM_CLASS_PREFIX = "enumClassPrefix";
public static final String ENUM_CLASS_PREFIX_DESC = "Prefix enum with class name";
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege

protected boolean withGoCodegenComment = false;
protected boolean withXml = false;
protected boolean enumClassPrefix = false;

protected String packageName = "openapi";

Expand Down Expand Up @@ -618,6 +619,10 @@ public void setWithXml(boolean withXml) {
this.withXml = withXml;
}

public void setEnumClassPrefix(boolean enumClassPrefix) {
this.enumClassPrefix = enumClassPrefix;
}

@Override
public String toDefaultValue(Schema schema) {
if (schema.getDefault() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public GoClientCodegen() {
cliOptions.add(CliOption.newBoolean(CodegenConstants.IS_GO_SUBMODULE, CodegenConstants.IS_GO_SUBMODULE_DESC));
cliOptions.add(CliOption.newBoolean(WITH_GO_CODEGEN_COMMENT, "whether to include Go codegen comment to disable Go Lint and collapse by default GitHub in PRs and diffs"));
cliOptions.add(CliOption.newBoolean(WITH_XML, "whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)"));

cliOptions.add(CliOption.newBoolean(CodegenConstants.ENUM_CLASS_PREFIX, CodegenConstants.ENUM_CLASS_PREFIX_DESC));

// option to change the order of form/body parameter
cliOptions.add(CliOption.newBoolean(
Expand Down Expand Up @@ -114,6 +114,13 @@ public void processOpts() {
}
}

if (additionalProperties.containsKey(CodegenConstants.ENUM_CLASS_PREFIX)) {
setEnumClassPrefix(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.ENUM_CLASS_PREFIX).toString()));
if (enumClassPrefix) {
additionalProperties.put(CodegenConstants.ENUM_CLASS_PREFIX, "true");
}
}

if (additionalProperties.containsKey(CodegenConstants.IS_GO_SUBMODULE)) {
setIsGoSubmodule(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.IS_GO_SUBMODULE).toString()));
if (isGoSubmodule) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (
{{#enumVars}}
{{^-first}}
{{/-first}}
{{{classname.toUpperCase}}}_{{name}} {{{classname}}} = "{{{value}}}"
{{#enumClassPrefix}}{{{classname.toUpperCase}}}_{{/enumClassPrefix}}{{name}} {{{classname}}} = "{{{value}}}"
{{/enumVars}}
{{/allowableValues}}
){{/isEnum}}{{^isEnum}}{{#description}}
Expand All @@ -36,7 +36,7 @@ type {{classname}} struct {
// {{{description}}}
{{/description}}
{{name}} *{{{dataType}}} `json:"{{baseName}},omitempty"{{#withXml}} xml:"{{baseName}}{{#isXmlAttribute}},attr{{/isXmlAttribute}}"{{/withXml}}{{#vendorExtensions.x-go-custom-tag}} {{{.}}}{{/vendorExtensions.x-go-custom-tag}}`
{{#isNullable}} isExplicitNull{{name}} bool `json:"-"{{#withXml}} xml:"-"{{/withXml}}`{{/isNullable}}
{{#isNullable}} isExplicitNull{{name}} bool `json:"-"{{#withXml}} xml:"-"{{/withXml}}`{{/isNullable}}
{{/vars}}
}
{{/isEnum}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (
{{#enumVars}}
{{^-first}}
{{/-first}}
{{{classname.toUpperCase}}}_{{name}} {{{classname}}} = "{{{value}}}"
{{#enumClassPrefix}}{{{classname.toUpperCase}}}_{{/enumClassPrefix}}{{name}} {{{classname}}} = "{{{value}}}"
{{/enumVars}}
{{/allowableValues}}
){{/isEnum}}{{^isEnum}}{{#description}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ protected void setExpectations() {
times = 1;
clientCodegen.setWithXml(GoClientOptionsProvider.WITH_XML_VALUE);
times = 1;
clientCodegen.setWithXml(GoClientOptionsProvider.ENUM_CLASS_PREFIX_VALUE);
times = 1;
clientCodegen.setPrependFormOrBodyParameters(Boolean.valueOf(GoClientOptionsProvider.PREPEND_FORM_OR_BODY_PARAMETERS_VALUE));
times = 1;
clientCodegen.setIsGoSubmodule(Boolean.valueOf(GoClientOptionsProvider.IS_GO_SUBMODULE_VALUE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class GoClientOptionsProvider implements OptionsProvider {
public static final String PACKAGE_NAME_VALUE = "Go";
public static final boolean WITH_GO_CODEGEN_COMMENT_VALUE = true;
public static final boolean WITH_XML_VALUE = true;
public static final boolean ENUM_CLASS_PREFIX_VALUE = true;
public static final Boolean PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = true;
public static final boolean IS_GO_SUBMODULE_VALUE = true;

Expand All @@ -45,6 +46,7 @@ public Map<String, String> createOptions() {
.put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, "true")
.put(CodegenConstants.WITH_GO_CODEGEN_COMMENT, "true")
.put(CodegenConstants.WITH_XML, "true")
.put(CodegenConstants.ENUM_CLASS_PREFIX, "true")
.put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, "true")
.put(CodegenConstants.IS_GO_SUBMODULE, "true")
.build();
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions samples/client/petstore/go/go-petstore/model_enum_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type EnumClass string

// List of EnumClass
const (
ENUMCLASS_ABC EnumClass = "_abc"
ENUMCLASS_EFG EnumClass = "-efg"
ENUMCLASS_XYZ EnumClass = "(xyz)"
ABC EnumClass = "_abc"
EFG EnumClass = "-efg"
XYZ EnumClass = "(xyz)"
)
6 changes: 3 additions & 3 deletions samples/client/petstore/go/go-petstore/model_outer_enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type OuterEnum string

// List of OuterEnum
const (
OUTERENUM_PLACED OuterEnum = "placed"
OUTERENUM_APPROVED OuterEnum = "approved"
OUTERENUM_DELIVERED OuterEnum = "delivered"
PLACED OuterEnum = "placed"
APPROVED OuterEnum = "approved"
DELIVERED OuterEnum = "delivered"
)

0 comments on commit 4575b30

Please sign in to comment.