From 4575b3074ab6f2628837ca84ed5c287f2afaceee Mon Sep 17 00:00:00 2001 From: William Cheng Date: Tue, 20 Aug 2019 00:33:08 +0800 Subject: [PATCH] [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 --- docs/generators/go-experimental.md | 1 + docs/generators/go.md | 1 + .../java/org/openapitools/codegen/CodegenConstants.java | 3 +++ .../codegen/languages/AbstractGoCodegen.java | 5 +++++ .../openapitools/codegen/languages/GoClientCodegen.java | 9 ++++++++- .../src/main/resources/go-experimental/model.mustache | 4 ++-- .../src/main/resources/go/model.mustache | 2 +- .../org/openapitools/codegen/go/GoClientOptionsTest.java | 2 ++ .../codegen/options/GoClientOptionsProvider.java | 2 ++ .../petstore/go/go-petstore-withXml/model_enum_class.go | 6 +++--- .../petstore/go/go-petstore-withXml/model_outer_enum.go | 6 +++--- .../client/petstore/go/go-petstore/model_enum_class.go | 6 +++--- .../client/petstore/go/go-petstore/model_outer_enum.go | 6 +++--- 13 files changed, 37 insertions(+), 16 deletions(-) diff --git a/docs/generators/go-experimental.md b/docs/generators/go-experimental.md index 23670cfd7b0a..dc84d5336f87 100644 --- a/docs/generators/go-experimental.md +++ b/docs/generators/go-experimental.md @@ -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| diff --git a/docs/generators/go.md b/docs/generators/go.md index ec1ecbebeeb1..4fc2af486dca 100644 --- a/docs/generators/go.md +++ b/docs/generators/go.md @@ -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| diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java index 50f35eff4b60..0cf95af984df 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java @@ -308,4 +308,7 @@ public class CodegenConstants { 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"; } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java index dcdcdee2f631..038414054e6a 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java @@ -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"; @@ -618,6 +619,10 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege this.withXml = withXml; } + public void setEnumClassPrefix(boolean enumClassPrefix) { + this.enumClassPrefix = enumClassPrefix; + } + @Override public String toDefaultValue(Schema schema) { if (schema.getDefault() != null) { diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java index 749ae845c8e4..27bef303b060 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java @@ -55,7 +55,7 @@ public class GoClientCodegen extends AbstractGoCodegen { 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( @@ -114,6 +114,13 @@ public class GoClientCodegen extends AbstractGoCodegen { } } + 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) { diff --git a/modules/openapi-generator/src/main/resources/go-experimental/model.mustache b/modules/openapi-generator/src/main/resources/go-experimental/model.mustache index 5ff3347e1d16..f18a8260d14e 100644 --- a/modules/openapi-generator/src/main/resources/go-experimental/model.mustache +++ b/modules/openapi-generator/src/main/resources/go-experimental/model.mustache @@ -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}} @@ -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}} diff --git a/modules/openapi-generator/src/main/resources/go/model.mustache b/modules/openapi-generator/src/main/resources/go/model.mustache index 05c1f107206d..cff5e7a4d928 100644 --- a/modules/openapi-generator/src/main/resources/go/model.mustache +++ b/modules/openapi-generator/src/main/resources/go/model.mustache @@ -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}} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientOptionsTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientOptionsTest.java index 634fcc455063..8a4d9cff6e61 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientOptionsTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientOptionsTest.java @@ -50,6 +50,8 @@ public class GoClientOptionsTest extends AbstractOptionsTest { 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)); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java index 39dc5d06ab80..28cba608cbbd 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java @@ -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; @@ -45,6 +46,7 @@ public class GoClientOptionsProvider implements OptionsProvider { .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(); diff --git a/samples/client/petstore/go/go-petstore-withXml/model_enum_class.go b/samples/client/petstore/go/go-petstore-withXml/model_enum_class.go index 550c4e58e895..9d3dd60a9469 100644 --- a/samples/client/petstore/go/go-petstore-withXml/model_enum_class.go +++ b/samples/client/petstore/go/go-petstore-withXml/model_enum_class.go @@ -13,7 +13,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)" ) \ No newline at end of file diff --git a/samples/client/petstore/go/go-petstore-withXml/model_outer_enum.go b/samples/client/petstore/go/go-petstore-withXml/model_outer_enum.go index 9568e634660f..c6b28556bf2f 100644 --- a/samples/client/petstore/go/go-petstore-withXml/model_outer_enum.go +++ b/samples/client/petstore/go/go-petstore-withXml/model_outer_enum.go @@ -13,7 +13,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" ) \ No newline at end of file diff --git a/samples/client/petstore/go/go-petstore/model_enum_class.go b/samples/client/petstore/go/go-petstore/model_enum_class.go index 63b983c3b84e..534ce4328817 100644 --- a/samples/client/petstore/go/go-petstore/model_enum_class.go +++ b/samples/client/petstore/go/go-petstore/model_enum_class.go @@ -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)" ) \ No newline at end of file diff --git a/samples/client/petstore/go/go-petstore/model_outer_enum.go b/samples/client/petstore/go/go-petstore/model_outer_enum.go index 2e46b8ae1d28..903d31e82690 100644 --- a/samples/client/petstore/go/go-petstore/model_outer_enum.go +++ b/samples/client/petstore/go/go-petstore/model_outer_enum.go @@ -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" ) \ No newline at end of file