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

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

Merged
merged 4 commits into from
Nov 21, 2019
Merged
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
1 change: 1 addition & 0 deletions docs/generators/go-experimental.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
1 change: 1 addition & 0 deletions docs/generators/go.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -113,10 +114,10 @@ public AbstractGoCodegen() {
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>();
Expand Down Expand Up @@ -505,7 +506,7 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
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;
}
Expand Down Expand Up @@ -641,6 +642,10 @@ public void setEnumClassPrefix(boolean enumClassPrefix) {
this.enumClassPrefix = enumClassPrefix;
}

public void setStructPrefix(boolean structPrefix) {
this.structPrefix = structPrefix;
}

@Override
public String toDefaultValue(Schema schema) {
if (schema.getDefault() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -56,6 +57,7 @@ public GoClientCodegen() {
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(
Expand Down Expand Up @@ -127,6 +129,13 @@ public void processOpts() {
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");
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down Expand Up @@ -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}}
Expand All @@ -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{}
Expand Down
8 changes: 4 additions & 4 deletions modules/openapi-generator/src/main/resources/go/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down Expand Up @@ -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}}
Expand All @@ -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{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ protected void setExpectations() {
times = 1;
clientCodegen.setIsGoSubmodule(Boolean.valueOf(GoClientOptionsProvider.IS_GO_SUBMODULE_VALUE));
times = 1;
clientCodegen.setStructPrefix(Boolean.valueOf(GoClientOptionsProvider.STRUCT_PREFIX_VALUE));
times = 1;
}};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -49,6 +50,7 @@ public Map<String, String> createOptions() {
.put(CodegenConstants.ENUM_CLASS_PREFIX, "true")
.put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, "true")
.put(CodegenConstants.IS_GO_SUBMODULE, "true")
.put("structPrefix", "true")
.build();
}

Expand Down