Skip to content

Commit 91daca3

Browse files
authored
Better Go code format (#3819)
* better varible naming * better comments * better code format for go experimental client * better comment, update samples
1 parent 12ba817 commit 91daca3

File tree

241 files changed

+4645
-4347
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

241 files changed

+4645
-4347
lines changed

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

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,33 @@ var (
1616
_ _context.Context
1717
)
1818

19+
// {{classname}}Service {{classname}} service
1920
type {{classname}}Service service
2021
{{#operation}}
2122

23+
{{#hasOptionalParams}}
24+
// {{{nickname}}}Opts Optional parameters for the method '{{{nickname}}}'
25+
type {{{nickname}}}Opts struct {
26+
{{#allParams}}
27+
{{^required}}
28+
{{#isPrimitiveType}}
29+
{{^isBinary}}
30+
{{vendorExtensions.x-exportParamName}} optional.{{vendorExtensions.x-optionalDataType}}
31+
{{/isBinary}}
32+
{{#isBinary}}
33+
{{vendorExtensions.x-exportParamName}} optional.Interface
34+
{{/isBinary}}
35+
{{/isPrimitiveType}}
36+
{{^isPrimitiveType}}
37+
{{vendorExtensions.x-exportParamName}} optional.Interface
38+
{{/isPrimitiveType}}
39+
{{/required}}
40+
{{/allParams}}
41+
}
42+
43+
{{/hasOptionalParams}}
2244
/*
23-
{{{classname}}}Service{{#summary}} {{{.}}}{{/summary}}
45+
{{operationId}}{{#summary}} {{{.}}}{{/summary}}{{^summary}} Method for {{operationId}}{{/summary}}
2446
{{#notes}}
2547
{{notes}}
2648
{{/notes}}
@@ -42,30 +64,9 @@ type {{classname}}Service service
4264
@return {{{returnType}}}
4365
{{/returnType}}
4466
*/
45-
{{#hasOptionalParams}}
46-
47-
type {{{nickname}}}Opts struct {
48-
{{#allParams}}
49-
{{^required}}
50-
{{#isPrimitiveType}}
51-
{{^isBinary}}
52-
{{vendorExtensions.x-exportParamName}} optional.{{vendorExtensions.x-optionalDataType}}
53-
{{/isBinary}}
54-
{{#isBinary}}
55-
{{vendorExtensions.x-exportParamName}} optional.Interface
56-
{{/isBinary}}
57-
{{/isPrimitiveType}}
58-
{{^isPrimitiveType}}
59-
{{vendorExtensions.x-exportParamName}} optional.Interface
60-
{{/isPrimitiveType}}
61-
{{/required}}
62-
{{/allParams}}
63-
}
64-
65-
{{/hasOptionalParams}}
6667
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) {
6768
var (
68-
localVarHttpMethod = _nethttp.Method{{httpMethod}}
69+
localVarHTTPMethod = _nethttp.Method{{httpMethod}}
6970
localVarPostBody interface{}
7071
localVarFormFileName string
7172
localVarFileName string
@@ -169,24 +170,24 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx _context.Context{{#hasParams
169170
{{/hasQueryParams}}
170171
// to determine the Content-Type header
171172
{{=<% %>=}}
172-
localVarHttpContentTypes := []string{<%#consumes%>"<%&mediaType%>"<%^-last%>, <%/-last%><%/consumes%>}
173+
localVarHTTPContentTypes := []string{<%#consumes%>"<%&mediaType%>"<%^-last%>, <%/-last%><%/consumes%>}
173174
<%={{ }}=%>
174175

175176
// set Content-Type header
176-
localVarHttpContentType := selectHeaderContentType(localVarHttpContentTypes)
177-
if localVarHttpContentType != "" {
178-
localVarHeaderParams["Content-Type"] = localVarHttpContentType
177+
localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes)
178+
if localVarHTTPContentType != "" {
179+
localVarHeaderParams["Content-Type"] = localVarHTTPContentType
179180
}
180181

181182
// to determine the Accept header
182183
{{=<% %>=}}
183-
localVarHttpHeaderAccepts := []string{<%#produces%>"<%&mediaType%>"<%^-last%>, <%/-last%><%/produces%>}
184+
localVarHTTPHeaderAccepts := []string{<%#produces%>"<%&mediaType%>"<%^-last%>, <%/-last%><%/produces%>}
184185
<%={{ }}=%>
185186

186187
// set Accept header
187-
localVarHttpHeaderAccept := selectHeaderAccept(localVarHttpHeaderAccepts)
188-
if localVarHttpHeaderAccept != "" {
189-
localVarHeaderParams["Accept"] = localVarHttpHeaderAccept
188+
localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts)
189+
if localVarHTTPHeaderAccept != "" {
190+
localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
190191
}
191192
{{#hasHeaderParams}}
192193
{{#headerParams}}
@@ -293,56 +294,56 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx _context.Context{{#hasParams
293294
{{/isKeyInCookie}}
294295
{{/isApiKey}}
295296
{{/authMethods}}
296-
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFormFileName, localVarFileName, localVarFileBytes)
297+
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFormFileName, localVarFileName, localVarFileBytes)
297298
if err != nil {
298299
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, err
299300
}
300301

301-
localVarHttpResponse, err := a.client.callAPI(r)
302-
if err != nil || localVarHttpResponse == nil {
303-
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHttpResponse, err
302+
localVarHTTPResponse, err := a.client.callAPI(r)
303+
if err != nil || localVarHTTPResponse == nil {
304+
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, err
304305
}
305306

306-
localVarBody, err := _ioutil.ReadAll(localVarHttpResponse.Body)
307-
localVarHttpResponse.Body.Close()
307+
localVarBody, err := _ioutil.ReadAll(localVarHTTPResponse.Body)
308+
localVarHTTPResponse.Body.Close()
308309
if err != nil {
309-
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHttpResponse, err
310+
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, err
310311
}
311312

312-
if localVarHttpResponse.StatusCode >= 300 {
313+
if localVarHTTPResponse.StatusCode >= 300 {
313314
newErr := GenericOpenAPIError{
314315
body: localVarBody,
315-
error: localVarHttpResponse.Status,
316+
error: localVarHTTPResponse.Status,
316317
}
317318
{{#responses}}
318319
{{#dataType}}
319-
if localVarHttpResponse.StatusCode == {{{code}}} {
320+
if localVarHTTPResponse.StatusCode == {{{code}}} {
320321
var v {{{dataType}}}
321-
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
322+
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
322323
if err != nil {
323324
newErr.error = err.Error()
324-
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHttpResponse, newErr
325+
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, newErr
325326
}
326327
newErr.model = v
327-
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHttpResponse, newErr
328+
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, newErr
328329
}
329330
{{/dataType}}
330331
{{/responses}}
331-
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHttpResponse, newErr
332+
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, newErr
332333
}
333334

334335
{{#returnType}}
335-
err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
336+
err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
336337
if err != nil {
337338
newErr := GenericOpenAPIError{
338339
body: localVarBody,
339340
error: err.Error(),
340341
}
341-
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHttpResponse, newErr
342+
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, newErr
342343
}
343344

344345
{{/returnType}}
345-
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHttpResponse, nil
346+
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, nil
346347
}
347348
{{/operation}}
348349
{{/operations}}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func (c *APIClient) callAPI(request *http.Request) (*http.Response, error) {
164164
return c.cfg.HTTPClient.Do(request)
165165
}
166166

167-
// Change base path to allow switching to mocks
167+
// ChangeBasePath changes base path to allow switching to mocks
168168
func (c *APIClient) ChangeBasePath(path string) {
169169
c.cfg.BasePath = path
170170
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type APIKey struct {
4141
Prefix string
4242
}
4343

44+
// Configuration stores the configuration of the API client
4445
type Configuration struct {
4546
BasePath string `json:"basePath,omitempty"`
4647
Host string `json:"host,omitempty"`
@@ -50,6 +51,7 @@ type Configuration struct {
5051
HTTPClient *http.Client
5152
}
5253

54+
// NewConfiguration returns a new Configuration object
5355
func NewConfiguration() *Configuration {
5456
cfg := &Configuration{
5557
BasePath: "{{{basePath}}}",
@@ -59,6 +61,7 @@ func NewConfiguration() *Configuration {
5961
return cfg
6062
}
6163

64+
// AddDefaultHeader adds a new HTTP header to the default header in the request
6265
func (c *Configuration) AddDefaultHeader(key string, value string) {
6366
c.DefaultHeader[key] = value
6467
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ import (
1212
{{/imports}}
1313
{{#model}}
1414
{{#isEnum}}
15-
{{#description}}
16-
// {{{classname}}} : {{{description}}}
17-
{{/description}}
15+
// {{{classname}}} {{#description}}{{{.}}}{{/description}}{{^description}}the model '{{{classname}}}'{{/description}}
1816
type {{{classname}}} {{^format}}{{dataType}}{{/format}}{{#format}}{{{format}}}{{/format}}
1917

2018
// List of {{{name}}}
@@ -26,8 +24,10 @@ const (
2624
{{#enumClassPrefix}}{{{classname.toUpperCase}}}_{{/enumClassPrefix}}{{name}} {{{classname}}} = "{{{value}}}"
2725
{{/enumVars}}
2826
{{/allowableValues}}
29-
){{/isEnum}}{{^isEnum}}{{#description}}
30-
// {{{description}}}{{/description}}
27+
)
28+
{{/isEnum}}
29+
{{^isEnum}}
30+
// {{classname}}{{#description}} {{{description}}}{{/description}}{{^description}} struct for {{{classname}}}{{/description}}
3131
type {{classname}} struct {
3232
{{#vars}}
3333
{{^-first}}
@@ -87,6 +87,7 @@ func (o *{{classname}}) Set{{name}}ExplicitNull(b bool) {
8787
{{/isNullable}}
8888
{{/vars}}
8989

90+
// MarshalJSON returns the JSON representation of the model.
9091
func (o {{classname}}) MarshalJSON() ([]byte, error) {
9192
toSerialize := map[string]interface{}{}
9293
{{#vars}}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
)
77

8+
// APIResponse stores the API response returned by the server.
89
type APIResponse struct {
910
*http.Response `json:"-"`
1011
Message string `json:"message,omitempty"`
@@ -22,12 +23,14 @@ type APIResponse struct {
2223
Payload []byte `json:"-"`
2324
}
2425

26+
// NewAPIResponse returns a new APIResonse object.
2527
func NewAPIResponse(r *http.Response) *APIResponse {
2628
2729
response := &APIResponse{Response: r}
2830
return response
2931
}
3032

33+
// NewAPIResponseWithError returns a new APIResponse object with the provided error message.
3134
func NewAPIResponseWithError(errorMessage string) *APIResponse {
3235
3336
response := &APIResponse{Message: errorMessage}

0 commit comments

Comments
 (0)