Add an option to prefix Go struct with the classname (#4564)

* add option to prefix struct with classname

* update usage

* fix test

* update doc
This commit is contained in:
William Cheng
2019-11-21 21:31:35 +08:00
committed by GitHub
parent 1de71a9e16
commit 52384badd2
8 changed files with 33 additions and 13 deletions

View File

@@ -12,4 +12,5 @@ sidebar_label: go-experimental
|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|
|structPrefix|whether to prefix struct with the class name. e.g. DeletePetOpts => PetApiDeletePetOpts| |false|
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|

View File

@@ -12,4 +12,5 @@ sidebar_label: go
|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|
|structPrefix|whether to prefix struct with the class name. e.g. DeletePetOpts => PetApiDeletePetOpts| |false|
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|

View File

@@ -40,6 +40,7 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
protected boolean withGoCodegenComment = false;
protected boolean withXml = false;
protected boolean enumClassPrefix = false;
protected boolean structPrefix = false;
protected String packageName = "openapi";
protected Set<String> numberTypes;
@@ -113,10 +114,10 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
typeMapping.put("object", "map[string]interface{}");
numberTypes = new HashSet<String>(
Arrays.asList(
"uint", "uint8", "uint16", "uint32", "uint64",
"int", "int8", "int16","int32", "int64",
"float32", "float64")
Arrays.asList(
"uint", "uint8", "uint16", "uint32", "uint64",
"int", "int8", "int16", "int32", "int64",
"float32", "float64")
);
importMapping = new HashMap<String, String>();
@@ -505,7 +506,7 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
CodegenModel model = (CodegenModel) v;
for (CodegenProperty param : model.vars) {
if (!addedTimeImport
&& ("time.Time".equals(param.dataType) || ("[]time.Time".equals(param.dataType)))) {
&& ("time.Time".equals(param.dataType) || ("[]time.Time".equals(param.dataType)))) {
imports.add(createMapping("import", "time"));
addedTimeImport = true;
}
@@ -641,6 +642,10 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
this.enumClassPrefix = enumClassPrefix;
}
public void setStructPrefix(boolean structPrefix) {
this.structPrefix = structPrefix;
}
@Override
public String toDefaultValue(Schema schema) {
if (schema.getDefault() != null) {

View File

@@ -36,6 +36,7 @@ public class GoClientCodegen extends AbstractGoCodegen {
protected boolean isGoSubmodule = false;
public static final String WITH_GO_CODEGEN_COMMENT = "withGoCodegenComment";
public static final String WITH_XML = "withXml";
public static final String STRUCT_PREFIX = "structPrefix";
public GoClientCodegen() {
super();
@@ -56,6 +57,7 @@ public class GoClientCodegen extends AbstractGoCodegen {
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));
cliOptions.add(CliOption.newBoolean(STRUCT_PREFIX, "whether to prefix struct with the class name. e.g. DeletePetOpts => PetApiDeletePetOpts"));
// option to change the order of form/body parameter
cliOptions.add(CliOption.newBoolean(
@@ -127,6 +129,13 @@ public class GoClientCodegen extends AbstractGoCodegen {
additionalProperties.put(CodegenConstants.IS_GO_SUBMODULE, "true");
}
}
if (additionalProperties.containsKey(STRUCT_PREFIX)) {
setStructPrefix(Boolean.parseBoolean(additionalProperties.get(STRUCT_PREFIX).toString()));
if (structPrefix) {
additionalProperties.put(STRUCT_PREFIX, "true");
}
}
}
/**

View File

@@ -21,8 +21,8 @@ type {{classname}}Service service
{{#operation}}
{{#hasOptionalParams}}
// {{{nickname}}}Opts Optional parameters for the method '{{{nickname}}}'
type {{{nickname}}}Opts struct {
// {{#structPrefix}}{{&classname}}{{/structPrefix}}{{{nickname}}}Opts Optional parameters for the method '{{{nickname}}}'
type {{#structPrefix}}{{&classname}}{{/structPrefix}}{{{nickname}}}Opts struct {
{{#allParams}}
{{^required}}
{{#isPrimitiveType}}
@@ -53,7 +53,7 @@ type {{{nickname}}}Opts struct {
{{/required}}
{{/allParams}}
{{#hasOptionalParams}}
* @param optional nil or *{{{nickname}}}Opts - Optional Parameters:
* @param optional nil or *{{#structPrefix}}{{&classname}}{{/structPrefix}}{{{nickname}}}Opts - Optional Parameters:
{{#allParams}}
{{^required}}
* @param "{{vendorExtensions.x-exportParamName}}" ({{#isPrimitiveType}}{{^isBinary}}optional.{{vendorExtensions.x-optionalDataType}}{{/isBinary}}{{#isBinary}}optional.Interface of {{dataType}}{{/isBinary}}{{/isPrimitiveType}}{{^isPrimitiveType}}optional.Interface of {{dataType}}{{/isPrimitiveType}}) - {{#description}} {{{.}}}{{/description}}
@@ -64,7 +64,7 @@ type {{{nickname}}}Opts struct {
@return {{{returnType}}}
{{/returnType}}
*/
func (a *{{{classname}}}Service) {{{nickname}}}(ctx _context.Context{{#hasParams}}, {{/hasParams}}{{#allParams}}{{#required}}{{paramName}} {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}localVarOptionals *{{{nickname}}}Opts{{/hasOptionalParams}}) ({{#returnType}}{{{returnType}}}, {{/returnType}}*_nethttp.Response, error) {
func (a *{{{classname}}}Service) {{{nickname}}}(ctx _context.Context{{#hasParams}}, {{/hasParams}}{{#allParams}}{{#required}}{{paramName}} {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}localVarOptionals *{{#structPrefix}}{{&classname}}{{/structPrefix}}{{{nickname}}}Opts{{/hasOptionalParams}}) ({{#returnType}}{{{returnType}}}, {{/returnType}}*_nethttp.Response, error) {
var (
localVarHTTPMethod = _nethttp.Method{{httpMethod}}
localVarPostBody interface{}

View File

@@ -21,8 +21,8 @@ type {{classname}}Service service
{{#operation}}
{{#hasOptionalParams}}
// {{{nickname}}}Opts Optional parameters for the method '{{{nickname}}}'
type {{{nickname}}}Opts struct {
// {{#structPrefix}}{{&classname}}{{/structPrefix}}{{{nickname}}}Opts Optional parameters for the method '{{{nickname}}}'
type {{#structPrefix}}{{&classname}}{{/structPrefix}}{{{nickname}}}Opts struct {
{{#allParams}}
{{^required}}
{{#isPrimitiveType}}
@@ -53,7 +53,7 @@ type {{{nickname}}}Opts struct {
{{/required}}
{{/allParams}}
{{#hasOptionalParams}}
* @param optional nil or *{{{nickname}}}Opts - Optional Parameters:
* @param optional nil or *{{#structPrefix}}{{&classname}}{{/structPrefix}}{{{nickname}}}Opts - Optional Parameters:
{{#allParams}}
{{^required}}
* @param "{{vendorExtensions.x-exportParamName}}" ({{#isPrimitiveType}}{{^isBinary}}optional.{{vendorExtensions.x-optionalDataType}}{{/isBinary}}{{#isBinary}}optional.Interface of {{dataType}}{{/isBinary}}{{/isPrimitiveType}}{{^isPrimitiveType}}optional.Interface of {{dataType}}{{/isPrimitiveType}}) - {{#description}} {{{.}}}{{/description}}
@@ -64,7 +64,7 @@ type {{{nickname}}}Opts struct {
@return {{{returnType}}}
{{/returnType}}
*/
func (a *{{{classname}}}Service) {{{nickname}}}(ctx _context.Context{{#hasParams}}, {{/hasParams}}{{#allParams}}{{#required}}{{paramName}} {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}localVarOptionals *{{{nickname}}}Opts{{/hasOptionalParams}}) ({{#returnType}}{{{returnType}}}, {{/returnType}}*_nethttp.Response, error) {
func (a *{{{classname}}}Service) {{{nickname}}}(ctx _context.Context{{#hasParams}}, {{/hasParams}}{{#allParams}}{{#required}}{{paramName}} {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}localVarOptionals *{{#structPrefix}}{{&classname}}{{/structPrefix}}{{{nickname}}}Opts{{/hasOptionalParams}}) ({{#returnType}}{{{returnType}}}, {{/returnType}}*_nethttp.Response, error) {
var (
localVarHTTPMethod = _nethttp.Method{{httpMethod}}
localVarPostBody interface{}

View File

@@ -56,6 +56,8 @@ public class GoClientOptionsTest extends AbstractOptionsTest {
times = 1;
clientCodegen.setIsGoSubmodule(Boolean.valueOf(GoClientOptionsProvider.IS_GO_SUBMODULE_VALUE));
times = 1;
clientCodegen.setStructPrefix(Boolean.valueOf(GoClientOptionsProvider.STRUCT_PREFIX_VALUE));
times = 1;
}};
}
}

View File

@@ -31,6 +31,7 @@ public class GoClientOptionsProvider implements OptionsProvider {
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;
public static final boolean STRUCT_PREFIX_VALUE = true;
@Override
public String getLanguage() {
@@ -49,6 +50,7 @@ public class GoClientOptionsProvider implements OptionsProvider {
.put(CodegenConstants.ENUM_CLASS_PREFIX, "true")
.put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, "true")
.put(CodegenConstants.IS_GO_SUBMODULE, "true")
.put("structPrefix", "true")
.build();
}