Skip to content

Commit 9b51ad2

Browse files
committed
feat: add interfaces to the go sdk generator
1 parent 862b65c commit 9b51ad2

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

scripts/generate-sdk/languages/go.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ generate_go_sdk() {
159159
--git-user-id ${GIT_USER_ID} \
160160
--git-repo-id ${GIT_REPO_ID} \
161161
--global-property apis,models,modelTests=true,modelDocs=false,apiDocs=false,supportingFiles \
162-
--additional-properties=isGoSubmodule=true,enumClassPrefix=true,$regional_api \
163-
--http-user-agent stackit-sdk-go/${service}
162+
--additional-properties=isGoSubmodule=true,enumClassPrefix=true,generateInterfaces=true,$regional_api \
163+
--http-user-agent stackit-sdk-go/${service}
164164

165165
# Remove unnecessary files
166166
rm ${SERVICES_FOLDER}/${service}/.openapi-generator-ignore

templates/go/api.mustache

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818

1919
type {{classname}} interface {
2020
{{#operation}}
21-
2221
/*
2322
{{operationId}} {{{summary}}}{{^summary}}Method for {{operationId}}{{/summary}}
2423
{{#notes}}
@@ -34,27 +33,44 @@ type {{classname}} interface {
3433

3534
/*
3635
{{nickname}}Execute executes the request{{#returnType}}
36+
@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().{{#pathParams}}
37+
@param {{paramName}}{{#description}} {{{.}}}{{/description}}{{/pathParams}}
3738
@return {{{.}}}{{/returnType}}
3839

3940
{{#isDeprecated}}
4041
Deprecated: {{{unescapedNotes}}}
4142
{{/isDeprecated}}
4243
*/
43-
{{nickname}}Execute(r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request) ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}}*http.Response, error)
44+
{{nickname}}Execute(ctx context.Context{{#pathParams}}, {{paramName}} {{{dataType}}}{{/pathParams}}) ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}} error)
4445
{{/operation}}
4546
}
47+
48+
{{#operation}}
49+
type {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request interface {
50+
{{#allParams}}
51+
{{^isPathParam}}
52+
{{#description}}
53+
// {{.}}
54+
{{/description}}
55+
{{#isDeprecated}}
56+
// Deprecated
57+
{{/isDeprecated}}
58+
{{vendorExtensions.x-export-param-name}}({{paramName}} {{{dataType}}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request
59+
{{/isPathParam}}
60+
{{/allParams}}
61+
Execute() ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}} error)
62+
}
63+
64+
{{/operation}}
4665
{{/generateInterfaces}}
4766

4867
// {{classname}}Service {{classname}} service
4968
type {{classname}}Service service
5069
{{#operation}}
5170

52-
type {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request struct {
53-
ctx context.Context{{#generateInterfaces}}
54-
apiService {{classname}}
55-
{{/generateInterfaces}}{{^generateInterfaces}}
71+
type {{#structPrefix}}{{&classname}}{{/structPrefix}}{{operationId}}Request struct {
72+
ctx context.Context
5673
apiService *{{classname}}Service
57-
{{/generateInterfaces}}
5874
{{#allParams}}
5975
{{paramName}} {{^isPathParam}}{{^isFile}}*{{/isFile}}{{/isPathParam}}{{{dataType}}}
6076
{{/allParams}}
@@ -69,15 +85,15 @@ type {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/stru
6985
// Deprecated
7086
{{/isDeprecated}}
7187

72-
func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request) {{vendorExtensions.x-export-param-name}}({{paramName}} {{{dataType}}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request {
88+
func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{operationId}}Request) {{vendorExtensions.x-export-param-name}}({{paramName}} {{{dataType}}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request {
7389
r.{{paramName}} = {{^isFile}}&{{/isFile}}{{paramName}}
7490
return r
7591
}
7692

7793
{{/isPathParam}}
7894
{{/allParams}}
7995

80-
func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request) Execute() ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}} error) {
96+
func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{operationId}}Request) Execute() ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}} error) {
8197
var (
8298
localVarHTTPMethod = http.Method{{httpMethod}}
8399
localVarPostBody interface{}
@@ -87,7 +103,11 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s
87103
{{/returnType}}
88104
)
89105
a := r.apiService
90-
localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "{{{classname}}}Service.{{{nickname}}}")
106+
client, ok := a.client.(*APIClient)
107+
if !ok {
108+
return {{#returnType}}nil, {{/returnType}} fmt.Errorf("could not parse client to type APIClient")
109+
}
110+
localBasePath, err := client.cfg.ServerURLWithContext(r.ctx, "{{{classname}}}Service.{{{nickname}}}")
91111
if err != nil {
92112
return {{#returnType}}localVarReturnValue, {{/returnType}} &oapierror.GenericOpenAPIError{ErrorMessage: err.Error()}
93113
}
@@ -301,7 +321,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s
301321
{{/isKeyInCookie}}
302322
{{/isApiKey}}
303323
{{/authMethods}}
304-
req, err := a.client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, formFiles)
324+
req, err := client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, formFiles)
305325
if err != nil {
306326
return {{#returnType}}localVarReturnValue, {{/returnType}} err
307327
}
@@ -311,7 +331,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s
311331
*contextHTTPRequest = req
312332
}
313333

314-
localVarHTTPResponse, err := a.client.callAPI(req)
334+
localVarHTTPResponse, err := client.callAPI(req)
315335
contextHTTPResponse, ok := r.ctx.Value(config.ContextHTTPResponse).(**http.Response)
316336
if ok {
317337
*contextHTTPResponse = localVarHTTPResponse
@@ -354,7 +374,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s
354374
{{/wildcard}}
355375
{{/range}}
356376
var v {{{dataType}}}
357-
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
377+
err = client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
358378
if err != nil {
359379
newErr.ErrorMessage = err.Error()
360380
return {{#returnType}}localVarReturnValue, {{/returnType}} newErr
@@ -375,7 +395,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s
375395
}
376396

377397
{{#returnType}}
378-
err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
398+
err = client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
379399
if err != nil {
380400
newErr := &oapierror.GenericOpenAPIError{
381401
StatusCode: localVarHTTPResponse.StatusCode,
@@ -401,7 +421,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s
401421
@return {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request
402422
*/
403423
func (a *APIClient) {{{nickname}}}(ctx context.Context{{#pathParams}}, {{paramName}} {{{dataType}}}{{/pathParams}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request {
404-
return {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request{
424+
return {{#structPrefix}}{{&classname}}{{/structPrefix}}{{operationId}}Request{
405425
apiService: a.defaultApi,
406426
ctx: ctx,
407427
{{#pathParams}}
@@ -416,7 +436,7 @@ Deprecated: {{{unescapedNotes}}}
416436
*/
417437
{{/isDeprecated}}
418438
func (a *APIClient) {{{nickname}}}Execute(ctx context.Context{{#pathParams}}, {{paramName}} {{{dataType}}}{{/pathParams}}) ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}} error){
419-
r := {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request{
439+
r := {{#structPrefix}}{{&classname}}{{/structPrefix}}{{operationId}}Request{
420440
apiService: a.defaultApi,
421441
ctx: ctx,
422442
{{#pathParams}}

templates/go/client.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type APIClient struct {
4545
}
4646

4747
type service struct {
48-
client *APIClient
48+
client DefaultApi
4949
}
5050

5151
// NewAPIClient creates a new API client.

0 commit comments

Comments
 (0)