Skip to content

Commit 4575b30

Browse files
authored
[Go] add option to use class as enum prefix (#3675)
* add option to use class as enum prefix * update doc * fix go test * update go xml petstore * fix format
1 parent 7abd2e1 commit 4575b30

File tree

13 files changed

+37
-16
lines changed

13 files changed

+37
-16
lines changed

docs/generators/go-experimental.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ sidebar_label: go-experimental
1313
|isGoSubmodule|whether the generated Go module is a submodule| |false|
1414
|withGoCodegenComment|whether to include Go codegen comment to disable Go Lint and collapse by default GitHub in PRs and diffs| |false|
1515
|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|
16+
|enumClassPrefix|Prefix enum with class name| |false|
1617
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|

docs/generators/go.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ sidebar_label: go
1313
|isGoSubmodule|whether the generated Go module is a submodule| |false|
1414
|withGoCodegenComment|whether to include Go codegen comment to disable Go Lint and collapse by default GitHub in PRs and diffs| |false|
1515
|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|
16+
|enumClassPrefix|Prefix enum with class name| |false|
1617
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,7 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
308308

309309
public static final String EXCEPTION_ON_FAILURE = "returnExceptionOnFailure";
310310
public static final String EXCEPTION_ON_FAILURE_DESC = "Throw an exception on non success response codes";
311+
312+
public static final String ENUM_CLASS_PREFIX = "enumClassPrefix";
313+
public static final String ENUM_CLASS_PREFIX_DESC = "Prefix enum with class name";
311314
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
3838

3939
protected boolean withGoCodegenComment = false;
4040
protected boolean withXml = false;
41+
protected boolean enumClassPrefix = false;
4142

4243
protected String packageName = "openapi";
4344

@@ -618,6 +619,10 @@ public void setWithXml(boolean withXml) {
618619
this.withXml = withXml;
619620
}
620621

622+
public void setEnumClassPrefix(boolean enumClassPrefix) {
623+
this.enumClassPrefix = enumClassPrefix;
624+
}
625+
621626
@Override
622627
public String toDefaultValue(Schema schema) {
623628
if (schema.getDefault() != null) {

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public GoClientCodegen() {
5555
cliOptions.add(CliOption.newBoolean(CodegenConstants.IS_GO_SUBMODULE, CodegenConstants.IS_GO_SUBMODULE_DESC));
5656
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"));
5757
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)"));
58-
58+
cliOptions.add(CliOption.newBoolean(CodegenConstants.ENUM_CLASS_PREFIX, CodegenConstants.ENUM_CLASS_PREFIX_DESC));
5959

6060
// option to change the order of form/body parameter
6161
cliOptions.add(CliOption.newBoolean(
@@ -114,6 +114,13 @@ public void processOpts() {
114114
}
115115
}
116116

117+
if (additionalProperties.containsKey(CodegenConstants.ENUM_CLASS_PREFIX)) {
118+
setEnumClassPrefix(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.ENUM_CLASS_PREFIX).toString()));
119+
if (enumClassPrefix) {
120+
additionalProperties.put(CodegenConstants.ENUM_CLASS_PREFIX, "true");
121+
}
122+
}
123+
117124
if (additionalProperties.containsKey(CodegenConstants.IS_GO_SUBMODULE)) {
118125
setIsGoSubmodule(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.IS_GO_SUBMODULE).toString()));
119126
if (isGoSubmodule) {

modules/openapi-generator/src/main/resources/go-experimental/model.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const (
2323
{{#enumVars}}
2424
{{^-first}}
2525
{{/-first}}
26-
{{{classname.toUpperCase}}}_{{name}} {{{classname}}} = "{{{value}}}"
26+
{{#enumClassPrefix}}{{{classname.toUpperCase}}}_{{/enumClassPrefix}}{{name}} {{{classname}}} = "{{{value}}}"
2727
{{/enumVars}}
2828
{{/allowableValues}}
2929
){{/isEnum}}{{^isEnum}}{{#description}}
@@ -36,7 +36,7 @@ type {{classname}} struct {
3636
// {{{description}}}
3737
{{/description}}
3838
{{name}} *{{{dataType}}} `json:"{{baseName}},omitempty"{{#withXml}} xml:"{{baseName}}{{#isXmlAttribute}},attr{{/isXmlAttribute}}"{{/withXml}}{{#vendorExtensions.x-go-custom-tag}} {{{.}}}{{/vendorExtensions.x-go-custom-tag}}`
39-
{{#isNullable}} isExplicitNull{{name}} bool `json:"-"{{#withXml}} xml:"-"{{/withXml}}`{{/isNullable}}
39+
{{#isNullable}} isExplicitNull{{name}} bool `json:"-"{{#withXml}} xml:"-"{{/withXml}}`{{/isNullable}}
4040
{{/vars}}
4141
}
4242
{{/isEnum}}

modules/openapi-generator/src/main/resources/go/model.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const (
2323
{{#enumVars}}
2424
{{^-first}}
2525
{{/-first}}
26-
{{{classname.toUpperCase}}}_{{name}} {{{classname}}} = "{{{value}}}"
26+
{{#enumClassPrefix}}{{{classname.toUpperCase}}}_{{/enumClassPrefix}}{{name}} {{{classname}}} = "{{{value}}}"
2727
{{/enumVars}}
2828
{{/allowableValues}}
2929
){{/isEnum}}{{^isEnum}}{{#description}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientOptionsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ protected void setExpectations() {
5050
times = 1;
5151
clientCodegen.setWithXml(GoClientOptionsProvider.WITH_XML_VALUE);
5252
times = 1;
53+
clientCodegen.setWithXml(GoClientOptionsProvider.ENUM_CLASS_PREFIX_VALUE);
54+
times = 1;
5355
clientCodegen.setPrependFormOrBodyParameters(Boolean.valueOf(GoClientOptionsProvider.PREPEND_FORM_OR_BODY_PARAMETERS_VALUE));
5456
times = 1;
5557
clientCodegen.setIsGoSubmodule(Boolean.valueOf(GoClientOptionsProvider.IS_GO_SUBMODULE_VALUE));

modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class GoClientOptionsProvider implements OptionsProvider {
2828
public static final String PACKAGE_NAME_VALUE = "Go";
2929
public static final boolean WITH_GO_CODEGEN_COMMENT_VALUE = true;
3030
public static final boolean WITH_XML_VALUE = true;
31+
public static final boolean ENUM_CLASS_PREFIX_VALUE = true;
3132
public static final Boolean PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = true;
3233
public static final boolean IS_GO_SUBMODULE_VALUE = true;
3334

@@ -45,6 +46,7 @@ public Map<String, String> createOptions() {
4546
.put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, "true")
4647
.put(CodegenConstants.WITH_GO_CODEGEN_COMMENT, "true")
4748
.put(CodegenConstants.WITH_XML, "true")
49+
.put(CodegenConstants.ENUM_CLASS_PREFIX, "true")
4850
.put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, "true")
4951
.put(CodegenConstants.IS_GO_SUBMODULE, "true")
5052
.build();

samples/client/petstore/go/go-petstore-withXml/model_enum_class.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/client/petstore/go/go-petstore-withXml/model_outer_enum.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/client/petstore/go/go-petstore/model_enum_class.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type EnumClass string
1212

1313
// List of EnumClass
1414
const (
15-
ENUMCLASS_ABC EnumClass = "_abc"
16-
ENUMCLASS_EFG EnumClass = "-efg"
17-
ENUMCLASS_XYZ EnumClass = "(xyz)"
15+
ABC EnumClass = "_abc"
16+
EFG EnumClass = "-efg"
17+
XYZ EnumClass = "(xyz)"
1818
)

samples/client/petstore/go/go-petstore/model_outer_enum.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type OuterEnum string
1212

1313
// List of OuterEnum
1414
const (
15-
OUTERENUM_PLACED OuterEnum = "placed"
16-
OUTERENUM_APPROVED OuterEnum = "approved"
17-
OUTERENUM_DELIVERED OuterEnum = "delivered"
15+
PLACED OuterEnum = "placed"
16+
APPROVED OuterEnum = "approved"
17+
DELIVERED OuterEnum = "delivered"
1818
)

0 commit comments

Comments
 (0)