Skip to content

Commit 35c1600

Browse files
committed
added go client generator
1 parent 100fd2b commit 35c1600

File tree

4 files changed

+127
-9
lines changed

4 files changed

+127
-9
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package io.swagger.codegen.v3.generators.go;
2+
3+
import io.swagger.codegen.v3.CliOption;
4+
import io.swagger.codegen.v3.CodegenType;
5+
import io.swagger.codegen.v3.SupportingFile;
6+
import org.apache.commons.lang3.StringUtils;
7+
8+
import java.io.File;
9+
import java.util.Arrays;
10+
11+
public class GoClientCodegen extends AbstractGoCodegen {
12+
protected String packageVersion = "1.0.0";
13+
protected String apiDocPath = "docs/";
14+
protected String modelDocPath = "docs/";
15+
public static final String WITH_XML = "withXml";
16+
17+
public GoClientCodegen() {
18+
this.outputFolder = "generated-code/go";
19+
this.modelTemplateFiles.put("model.mustache", ".go");
20+
this.apiTemplateFiles.put("api.mustache", ".go");
21+
this.modelDocTemplateFiles.put("model_doc.mustache", ".md");
22+
this.apiDocTemplateFiles.put("api_doc.mustache", ".md");
23+
this.embeddedTemplateDir = this.templateDir = "go";
24+
this.hideGenerationTimestamp = Boolean.TRUE;
25+
this.setReservedWordsLowerCase(Arrays.asList("string", "bool", "uint", "uint8", "uint16", "uint32", "uint64", "int", "int8", "int16", "int32", "int64", "float32", "float64", "complex64", "complex128", "rune", "byte", "uintptr", "break", "default", "func", "interface", "select", "case", "defer", "go", "map", "struct", "chan", "else", "goto", "package", "switch", "const", "fallthrough", "if", "range", "type", "continue", "for", "import", "return", "var", "error", "ApiResponse", "nil"));
26+
this.cliOptions.add((new CliOption("packageVersion", "Go package version.")).defaultValue("1.0.0"));
27+
this.cliOptions.add(CliOption.newBoolean("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)"));
28+
}
29+
30+
@Override
31+
public String getDefaultTemplateDir() {
32+
return "go";
33+
}
34+
35+
public void processOpts() {
36+
super.processOpts();
37+
38+
if (StringUtils.isBlank(templateDir)) {
39+
embeddedTemplateDir = templateDir = getTemplateDir();
40+
}
41+
42+
if (this.additionalProperties.containsKey("packageName")) {
43+
this.setPackageName((String)this.additionalProperties.get("packageName"));
44+
} else {
45+
this.setPackageName("swagger");
46+
}
47+
48+
if (this.additionalProperties.containsKey("packageVersion")) {
49+
this.setPackageVersion((String)this.additionalProperties.get("packageVersion"));
50+
} else {
51+
this.setPackageVersion("1.0.0");
52+
}
53+
54+
this.additionalProperties.put("packageName", this.packageName);
55+
this.additionalProperties.put("packageVersion", this.packageVersion);
56+
this.additionalProperties.put("apiDocPath", this.apiDocPath);
57+
this.additionalProperties.put("modelDocPath", this.modelDocPath);
58+
this.modelPackage = this.packageName;
59+
this.apiPackage = this.packageName;
60+
this.supportingFiles.add(new SupportingFile("swagger.mustache", "api", "swagger.yaml"));
61+
this.supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
62+
this.supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
63+
this.supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
64+
this.supportingFiles.add(new SupportingFile("configuration.mustache", "", "configuration.go"));
65+
this.supportingFiles.add(new SupportingFile("client.mustache", "", "client.go"));
66+
this.supportingFiles.add(new SupportingFile("response.mustache", "", "response.go"));
67+
this.supportingFiles.add(new SupportingFile(".travis.yml", "", ".travis.yml"));
68+
if (this.additionalProperties.containsKey("withXml")) {
69+
this.setWithXml(Boolean.parseBoolean(this.additionalProperties.get("withXml").toString()));
70+
if (this.withXml) {
71+
this.additionalProperties.put("withXml", "true");
72+
}
73+
}
74+
75+
}
76+
77+
public CodegenType getTag() {
78+
return CodegenType.CLIENT;
79+
}
80+
81+
public String getName() {
82+
return "go";
83+
}
84+
85+
public String getHelp() {
86+
return "Generates a Go client library (beta).";
87+
}
88+
89+
public String apiFileFolder() {
90+
return this.outputFolder + File.separator;
91+
}
92+
93+
public String modelFileFolder() {
94+
return this.outputFolder + File.separator;
95+
}
96+
97+
public String apiDocFileFolder() {
98+
return (this.outputFolder + "/" + this.apiDocPath).replace('/', File.separatorChar);
99+
}
100+
101+
public String modelDocFileFolder() {
102+
return (this.outputFolder + "/" + this.modelDocPath).replace('/', File.separatorChar);
103+
}
104+
105+
public String toModelDocFilename(String name) {
106+
return this.toModelName(name);
107+
}
108+
109+
public String toApiDocFilename(String name) {
110+
return this.toApiName(name);
111+
}
112+
113+
public void setPackageVersion(String packageVersion) {
114+
this.packageVersion = packageVersion;
115+
}
116+
}
117+

src/main/resources/META-INF/services/io.swagger.codegen.v3.CodegenConfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
io.swagger.codegen.v3.generators.dotnet.AspNetCoreServerCodegen
22
io.swagger.codegen.v3.generators.dotnet.CSharpClientCodegen
33
io.swagger.codegen.v3.generators.dotnet.CsharpDotNet2ClientCodegen
4+
io.swagger.codegen.v3.generators.go.GoClientCodegen
45
io.swagger.codegen.v3.generators.go.GoServerCodegen
56
io.swagger.codegen.v3.generators.html.StaticDocCodegen
67
io.swagger.codegen.v3.generators.html.StaticHtmlCodegen

src/main/resources/handlebars/go/api_doc.mustache

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ Method | HTTP request | Description
1717
{{{notes}}}{{/notes}}
1818

1919
### Required Parameters
20-
{{^allParams}}This endpoint does not need any parameter.{{/allParams}}{{#allParams}}{{#-last}}
20+
{{^allParams}}This endpoint does not need any parameter.{{/allParams}}{{#allParams}}{{#@last}}
2121
Name | Type | Description | Notes
2222
------------- | ------------- | ------------- | -------------
23-
**ctx** | **context.Context** | context for authentication, logging, cancellation, deadlines, tracing, etc.{{/-last}}{{/allParams}}{{#allParams}}{{#required}}
23+
**ctx** | **context.Context** | context for authentication, logging, cancellation, deadlines, tracing, etc.{{/@last}}{{/allParams}}{{#allParams}}{{#required}}
2424
**{{paramName}}** | {{#isFile}}**{{dataType}}**{{/isFile}}{{#isPrimitiveType}}**{{dataType}}**{{/isPrimitiveType}}{{^isPrimitiveType}}{{^isFile}}[**{{dataType}}**]({{baseType}}.md){{/isFile}}{{/isPrimitiveType}}| {{description}} | {{#defaultValue}}[default to {{defaultValue}}]{{/defaultValue}}{{/required}}{{/allParams}}{{#hasOptionalParams}}
2525
**optional** | ***{{{classname}}}{{{nickname}}}Opts** | optional parameters | nil if no parameters
2626

2727
### Optional Parameters
2828
Optional parameters are passed through a pointer to a {{{classname}}}{{{nickname}}}Opts struct
29-
{{#allParams}}{{#-last}}
29+
{{#allParams}}{{#@last}}
3030
Name | Type | Description | Notes
31-
------------- | ------------- | ------------- | -------------{{/-last}}{{/allParams}}{{#allParams}}
31+
------------- | ------------- | ------------- | -------------{{/@last}}{{/allParams}}{{#allParams}}
3232
{{^required}} **{{paramName}}** | {{#isFile}}**optional.Interface of {{dataType}}**{{/isFile}}{{#isPrimitiveType}}**optional.{{vendorExtensions.x-optionalDataType}}**{{/isPrimitiveType}}{{^isPrimitiveType}}{{^isFile}}[**optional.Interface of {{dataType}}**]({{baseType}}.md){{/isFile}}{{/isPrimitiveType}}| {{description}} | {{#defaultValue}}[default to {{defaultValue}}]{{/defaultValue}}{{/required}}{{/allParams}}{{/hasOptionalParams}}
3333

3434
### Return type
@@ -37,7 +37,7 @@ Name | Type | Description | Notes
3737

3838
### Authorization
3939

40-
{{^authMethods}}No authorization required{{/authMethods}}{{#authMethods}}[{{{name}}}](../README.md#{{{name}}}){{^-last}}, {{/-last}}{{/authMethods}}
40+
{{^authMethods}}No authorization required{{/authMethods}}{{#authMethods}}[{{{name}}}](../README.md#{{{name}}}){{^@last}}, {{/@last}}{{/authMethods}}
4141

4242
### HTTP request headers
4343

src/main/resources/handlebars/go/model.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ type {{{classname}}} {{^format}}{{dataType}}{{/format}}{{#format}}{{{format}}}{{
1111
const (
1212
{{#allowableValues}}
1313
{{#enumVars}}
14-
{{^-first}}
15-
{{/-first}}
14+
{{^@first}}
15+
{{/@first}}
1616
{{name}}_{{{classname}}} {{{classname}}} = "{{{value}}}"
1717
{{/enumVars}}
1818
{{/allowableValues}}
1919
){{/isEnum}}{{^isEnum}}{{#description}}
2020
// {{{description}}}{{/description}}
2121
type {{classname}} struct {
2222
{{#vars}}
23-
{{^-first}}
24-
{{/-first}}
23+
{{^@first}}
24+
{{/@first}}
2525
{{#description}}
2626
// {{{description}}}
2727
{{/description}}

0 commit comments

Comments
 (0)