Skip to content

Commit 7facca0

Browse files
authored
feat: add interfaces for the go sdk (#157)
* feat: add interfaces to the go sdk generator
1 parent 862b65c commit 7facca0

File tree

3 files changed

+53
-34
lines changed

3 files changed

+53
-34
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: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,58 @@ import (
1818

1919
type {{classname}} interface {
2020
{{#operation}}
21-
2221
/*
23-
{{operationId}} {{{summary}}}{{^summary}}Method for {{operationId}}{{/summary}}
24-
{{#notes}}
25-
26-
{{#isDeprecated}}Deprecated: {{/isDeprecated}}{{{unescapedNotes}}}
27-
{{/notes}}
22+
{{operationId}} {{{summary}}}{{^summary}}Method for {{operationId}}{{/summary}}
23+
{{#notes}}
24+
{{#isDeprecated}}Deprecated: {{/isDeprecated}}{{{unescapedNotes}}}
25+
{{/notes}}
2826

29-
@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().{{#pathParams}}
30-
@param {{paramName}}{{#description}} {{{.}}}{{/description}}{{/pathParams}}
31-
@return {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request
27+
@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().{{#pathParams}}
28+
@param {{paramName}}{{#description}} {{{.}}}{{/description}}{{/pathParams}}
29+
@return {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request
3230
*/
3331
{{{nickname}}}(ctx context.Context{{#pathParams}}, {{paramName}} {{{dataType}}}{{/pathParams}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request
34-
3532
/*
36-
{{nickname}}Execute executes the request{{#returnType}}
37-
@return {{{.}}}{{/returnType}}
38-
39-
{{#isDeprecated}}
40-
Deprecated: {{{unescapedNotes}}}
41-
{{/isDeprecated}}
33+
{{nickname}}Execute executes the request{{#returnType}}
34+
35+
@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().{{#pathParams}}
36+
@param {{paramName}}{{#description}} {{{.}}}{{/description}}{{/pathParams}}
37+
@return {{{.}}}{{/returnType}}
38+
39+
{{#isDeprecated}}
40+
Deprecated: {{{unescapedNotes}}}
41+
{{/isDeprecated}}
4242
*/
43-
{{nickname}}Execute(r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request) ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}}*http.Response, error)
43+
{{nickname}}Execute(ctx context.Context{{#pathParams}}, {{paramName}} {{{dataType}}}{{/pathParams}}) ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}} error)
4444
{{/operation}}
4545
}
46+
47+
{{#operation}}
48+
type {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request interface {
49+
{{#allParams}}
50+
{{^isPathParam}}
51+
{{#description}}
52+
// {{.}}
53+
{{/description}}
54+
{{#isDeprecated}}
55+
// Deprecated
56+
{{/isDeprecated}}
57+
{{vendorExtensions.x-export-param-name}}({{paramName}} {{{dataType}}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request
58+
{{/isPathParam}}
59+
{{/allParams}}
60+
Execute() ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}} error)
61+
}
62+
63+
{{/operation}}
4664
{{/generateInterfaces}}
4765

4866
// {{classname}}Service {{classname}} service
4967
type {{classname}}Service service
5068
{{#operation}}
5169

52-
type {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request struct {
53-
ctx context.Context{{#generateInterfaces}}
54-
apiService {{classname}}
55-
{{/generateInterfaces}}{{^generateInterfaces}}
70+
type {{#structPrefix}}{{&classname}}{{/structPrefix}}{{operationId}}Request struct {
71+
ctx context.Context
5672
apiService *{{classname}}Service
57-
{{/generateInterfaces}}
5873
{{#allParams}}
5974
{{paramName}} {{^isPathParam}}{{^isFile}}*{{/isFile}}{{/isPathParam}}{{{dataType}}}
6075
{{/allParams}}
@@ -69,15 +84,15 @@ type {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/stru
6984
// Deprecated
7085
{{/isDeprecated}}
7186

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 {
87+
func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{operationId}}Request) {{vendorExtensions.x-export-param-name}}({{paramName}} {{{dataType}}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request {
7388
r.{{paramName}} = {{^isFile}}&{{/isFile}}{{paramName}}
7489
return r
7590
}
7691

7792
{{/isPathParam}}
7893
{{/allParams}}
7994

80-
func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request) Execute() ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}} error) {
95+
func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{operationId}}Request) Execute() ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}} error) {
8196
var (
8297
localVarHTTPMethod = http.Method{{httpMethod}}
8398
localVarPostBody interface{}
@@ -87,7 +102,11 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s
87102
{{/returnType}}
88103
)
89104
a := r.apiService
90-
localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "{{{classname}}}Service.{{{nickname}}}")
105+
client, ok := a.client.(*APIClient)
106+
if !ok {
107+
return {{#returnType}}nil, {{/returnType}} fmt.Errorf("could not parse client to type APIClient")
108+
}
109+
localBasePath, err := client.cfg.ServerURLWithContext(r.ctx, "{{{classname}}}Service.{{{nickname}}}")
91110
if err != nil {
92111
return {{#returnType}}localVarReturnValue, {{/returnType}} &oapierror.GenericOpenAPIError{ErrorMessage: err.Error()}
93112
}
@@ -301,7 +320,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s
301320
{{/isKeyInCookie}}
302321
{{/isApiKey}}
303322
{{/authMethods}}
304-
req, err := a.client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, formFiles)
323+
req, err := client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, formFiles)
305324
if err != nil {
306325
return {{#returnType}}localVarReturnValue, {{/returnType}} err
307326
}
@@ -311,7 +330,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s
311330
*contextHTTPRequest = req
312331
}
313332

314-
localVarHTTPResponse, err := a.client.callAPI(req)
333+
localVarHTTPResponse, err := client.callAPI(req)
315334
contextHTTPResponse, ok := r.ctx.Value(config.ContextHTTPResponse).(**http.Response)
316335
if ok {
317336
*contextHTTPResponse = localVarHTTPResponse
@@ -354,7 +373,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s
354373
{{/wildcard}}
355374
{{/range}}
356375
var v {{{dataType}}}
357-
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
376+
err = client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
358377
if err != nil {
359378
newErr.ErrorMessage = err.Error()
360379
return {{#returnType}}localVarReturnValue, {{/returnType}} newErr
@@ -375,7 +394,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s
375394
}
376395

377396
{{#returnType}}
378-
err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
397+
err = client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
379398
if err != nil {
380399
newErr := &oapierror.GenericOpenAPIError{
381400
StatusCode: localVarHTTPResponse.StatusCode,
@@ -401,7 +420,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s
401420
@return {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request
402421
*/
403422
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{
423+
return {{#structPrefix}}{{&classname}}{{/structPrefix}}{{operationId}}Request{
405424
apiService: a.defaultApi,
406425
ctx: ctx,
407426
{{#pathParams}}
@@ -416,7 +435,7 @@ Deprecated: {{{unescapedNotes}}}
416435
*/
417436
{{/isDeprecated}}
418437
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{
438+
r := {{#structPrefix}}{{&classname}}{{/structPrefix}}{{operationId}}Request{
420439
apiService: a.defaultApi,
421440
ctx: ctx,
422441
{{#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)