From 9b51ad234f63f11e9537136ccf854f2098582226 Mon Sep 17 00:00:00 2001 From: Marcel Jacek Date: Tue, 13 May 2025 15:00:42 +0200 Subject: [PATCH 1/2] feat: add interfaces to the go sdk generator --- scripts/generate-sdk/languages/go.sh | 4 +-- templates/go/api.mustache | 52 +++++++++++++++++++--------- templates/go/client.mustache | 2 +- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/scripts/generate-sdk/languages/go.sh b/scripts/generate-sdk/languages/go.sh index b0e2044..5c5c007 100644 --- a/scripts/generate-sdk/languages/go.sh +++ b/scripts/generate-sdk/languages/go.sh @@ -159,8 +159,8 @@ generate_go_sdk() { --git-user-id ${GIT_USER_ID} \ --git-repo-id ${GIT_REPO_ID} \ --global-property apis,models,modelTests=true,modelDocs=false,apiDocs=false,supportingFiles \ - --additional-properties=isGoSubmodule=true,enumClassPrefix=true,$regional_api \ - --http-user-agent stackit-sdk-go/${service} + --additional-properties=isGoSubmodule=true,enumClassPrefix=true,generateInterfaces=true,$regional_api \ + --http-user-agent stackit-sdk-go/${service} # Remove unnecessary files rm ${SERVICES_FOLDER}/${service}/.openapi-generator-ignore diff --git a/templates/go/api.mustache b/templates/go/api.mustache index 3ce8436..ce26903 100644 --- a/templates/go/api.mustache +++ b/templates/go/api.mustache @@ -18,7 +18,6 @@ import ( type {{classname}} interface { {{#operation}} - /* {{operationId}} {{{summary}}}{{^summary}}Method for {{operationId}}{{/summary}} {{#notes}} @@ -34,27 +33,44 @@ type {{classname}} interface { /* {{nickname}}Execute executes the request{{#returnType}} + @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().{{#pathParams}} + @param {{paramName}}{{#description}} {{{.}}}{{/description}}{{/pathParams}} @return {{{.}}}{{/returnType}} {{#isDeprecated}} Deprecated: {{{unescapedNotes}}} {{/isDeprecated}} */ - {{nickname}}Execute(r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request) ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}}*http.Response, error) + {{nickname}}Execute(ctx context.Context{{#pathParams}}, {{paramName}} {{{dataType}}}{{/pathParams}}) ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}} error) {{/operation}} } + +{{#operation}} +type {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request interface { +{{#allParams}} +{{^isPathParam}} +{{#description}} +// {{.}} +{{/description}} +{{#isDeprecated}} +// Deprecated +{{/isDeprecated}} +{{vendorExtensions.x-export-param-name}}({{paramName}} {{{dataType}}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request +{{/isPathParam}} +{{/allParams}} + Execute() ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}} error) +} + +{{/operation}} {{/generateInterfaces}} // {{classname}}Service {{classname}} service type {{classname}}Service service {{#operation}} -type {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request struct { - ctx context.Context{{#generateInterfaces}} - apiService {{classname}} -{{/generateInterfaces}}{{^generateInterfaces}} +type {{#structPrefix}}{{&classname}}{{/structPrefix}}{{operationId}}Request struct { + ctx context.Context apiService *{{classname}}Service -{{/generateInterfaces}} {{#allParams}} {{paramName}} {{^isPathParam}}{{^isFile}}*{{/isFile}}{{/isPathParam}}{{{dataType}}} {{/allParams}} @@ -69,7 +85,7 @@ type {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/stru // Deprecated {{/isDeprecated}} -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 { +func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{operationId}}Request) {{vendorExtensions.x-export-param-name}}({{paramName}} {{{dataType}}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request { r.{{paramName}} = {{^isFile}}&{{/isFile}}{{paramName}} return r } @@ -77,7 +93,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s {{/isPathParam}} {{/allParams}} -func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request) Execute() ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}} error) { +func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{operationId}}Request) Execute() ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}} error) { var ( localVarHTTPMethod = http.Method{{httpMethod}} localVarPostBody interface{} @@ -87,7 +103,11 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s {{/returnType}} ) a := r.apiService - localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "{{{classname}}}Service.{{{nickname}}}") + client, ok := a.client.(*APIClient) + if !ok { + return {{#returnType}}nil, {{/returnType}} fmt.Errorf("could not parse client to type APIClient") + } + localBasePath, err := client.cfg.ServerURLWithContext(r.ctx, "{{{classname}}}Service.{{{nickname}}}") if err != nil { return {{#returnType}}localVarReturnValue, {{/returnType}} &oapierror.GenericOpenAPIError{ErrorMessage: err.Error()} } @@ -301,7 +321,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s {{/isKeyInCookie}} {{/isApiKey}} {{/authMethods}} - req, err := a.client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, formFiles) + req, err := client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, formFiles) if err != nil { return {{#returnType}}localVarReturnValue, {{/returnType}} err } @@ -311,7 +331,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s *contextHTTPRequest = req } - localVarHTTPResponse, err := a.client.callAPI(req) + localVarHTTPResponse, err := client.callAPI(req) contextHTTPResponse, ok := r.ctx.Value(config.ContextHTTPResponse).(**http.Response) if ok { *contextHTTPResponse = localVarHTTPResponse @@ -354,7 +374,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s {{/wildcard}} {{/range}} var v {{{dataType}}} - err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) + err = client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { newErr.ErrorMessage = err.Error() return {{#returnType}}localVarReturnValue, {{/returnType}} newErr @@ -375,7 +395,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s } {{#returnType}} - err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) + err = client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { newErr := &oapierror.GenericOpenAPIError{ StatusCode: localVarHTTPResponse.StatusCode, @@ -401,7 +421,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/s @return {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request */ func (a *APIClient) {{{nickname}}}(ctx context.Context{{#pathParams}}, {{paramName}} {{{dataType}}}{{/pathParams}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request { - return {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request{ + return {{#structPrefix}}{{&classname}}{{/structPrefix}}{{operationId}}Request{ apiService: a.defaultApi, ctx: ctx, {{#pathParams}} @@ -416,7 +436,7 @@ Deprecated: {{{unescapedNotes}}} */ {{/isDeprecated}} func (a *APIClient) {{{nickname}}}Execute(ctx context.Context{{#pathParams}}, {{paramName}} {{{dataType}}}{{/pathParams}}) ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}} error){ - r := {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request{ + r := {{#structPrefix}}{{&classname}}{{/structPrefix}}{{operationId}}Request{ apiService: a.defaultApi, ctx: ctx, {{#pathParams}} diff --git a/templates/go/client.mustache b/templates/go/client.mustache index f70e117..71754fc 100644 --- a/templates/go/client.mustache +++ b/templates/go/client.mustache @@ -45,7 +45,7 @@ type APIClient struct { } type service struct { - client *APIClient + client DefaultApi } // NewAPIClient creates a new API client. From 936e27c8b86adeafca33de31622df472104d3dee Mon Sep 17 00:00:00 2001 From: Marcel Jacek Date: Wed, 14 May 2025 10:28:10 +0200 Subject: [PATCH 2/2] fix descriptions of interfaces --- templates/go/api.mustache | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/templates/go/api.mustache b/templates/go/api.mustache index ce26903..879615c 100644 --- a/templates/go/api.mustache +++ b/templates/go/api.mustache @@ -19,27 +19,26 @@ import ( type {{classname}} interface { {{#operation}} /* - {{operationId}} {{{summary}}}{{^summary}}Method for {{operationId}}{{/summary}} - {{#notes}} - - {{#isDeprecated}}Deprecated: {{/isDeprecated}}{{{unescapedNotes}}} - {{/notes}} +{{operationId}} {{{summary}}}{{^summary}}Method for {{operationId}}{{/summary}} +{{#notes}} +{{#isDeprecated}}Deprecated: {{/isDeprecated}}{{{unescapedNotes}}} +{{/notes}} - @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().{{#pathParams}} - @param {{paramName}}{{#description}} {{{.}}}{{/description}}{{/pathParams}} - @return {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request +@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().{{#pathParams}} +@param {{paramName}}{{#description}} {{{.}}}{{/description}}{{/pathParams}} +@return {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request */ {{{nickname}}}(ctx context.Context{{#pathParams}}, {{paramName}} {{{dataType}}}{{/pathParams}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request - /* - {{nickname}}Execute executes the request{{#returnType}} - @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().{{#pathParams}} - @param {{paramName}}{{#description}} {{{.}}}{{/description}}{{/pathParams}} - @return {{{.}}}{{/returnType}} - - {{#isDeprecated}} - Deprecated: {{{unescapedNotes}}} - {{/isDeprecated}} +{{nickname}}Execute executes the request{{#returnType}} + +@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().{{#pathParams}} +@param {{paramName}}{{#description}} {{{.}}}{{/description}}{{/pathParams}} +@return {{{.}}}{{/returnType}} + +{{#isDeprecated}} +Deprecated: {{{unescapedNotes}}} +{{/isDeprecated}} */ {{nickname}}Execute(ctx context.Context{{#pathParams}}, {{paramName}} {{{dataType}}}{{/pathParams}}) ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}{{^isResponseFile}}*{{/isResponseFile}}{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}} error) {{/operation}}