From 94fa3e5f869c94eb991f8f67f7536446848bc2d6 Mon Sep 17 00:00:00 2001 From: Thomas De Meyer Date: Tue, 14 Nov 2023 16:10:09 +0100 Subject: [PATCH 1/5] fix: added fix to handle status codes a bit better --- .../languages/go/client/ClientFileProducer.kt | 9 ++-- .../languages/go/client/GoClientModule.kt | 10 ++-- .../languages/go/client/MethodRenderer.kt | 49 +++++++------------ 3 files changed, 28 insertions(+), 40 deletions(-) diff --git a/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/ClientFileProducer.kt b/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/ClientFileProducer.kt index d216dd582..462549c55 100644 --- a/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/ClientFileProducer.kt +++ b/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/ClientFileProducer.kt @@ -7,10 +7,9 @@ import io.vrap.rmf.codegen.rendering.FileProducer import io.vrap.rmf.codegen.rendering.utils.keepIndentation import io.vrap.rmf.raml.model.modules.Api -class ClientFileProducer constructor( - val clientConstants: ClientConstants, - val api: Api, - @BasePackageName val basePackageName: String +class ClientFileProducer( + val api: Api, + @BasePackageName val basePackageName: String ) : FileProducer { override fun produceFiles(): List { @@ -201,6 +200,8 @@ class ClientFileProducer constructor( |func (e GenericRequestError) Error() string { | return fmt.Sprintf("Request returned status code %d", e.StatusCode) |} + | + |var ErrNotFound = errors.New("resource not found") """.trimMargin().keepIndentation() ) } diff --git a/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/GoClientModule.kt b/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/GoClientModule.kt index 16841ac77..ce4c54af1 100644 --- a/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/GoClientModule.kt +++ b/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/GoClientModule.kt @@ -21,9 +21,8 @@ object GoClientModule : Module { MethodGenerator( setOf( GoMethodRenderer( - generatorModule.clientConstants(), - generatorModule.vrapTypeProvider(), - generatorModule.providePackageName() + generatorModule.vrapTypeProvider(), + generatorModule.providePackageName() ) ), generatorModule.allResourceMethods() @@ -31,9 +30,8 @@ object GoClientModule : Module { FileGenerator( setOf( ClientFileProducer( - generatorModule.clientConstants(), - generatorModule.provideRamlModel(), - generatorModule.providePackageName() + generatorModule.provideRamlModel(), + generatorModule.providePackageName() ) ) ) diff --git a/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/MethodRenderer.kt b/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/MethodRenderer.kt index 3b4d76d07..6f407b412 100644 --- a/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/MethodRenderer.kt +++ b/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/MethodRenderer.kt @@ -19,10 +19,9 @@ import io.vrap.rmf.raml.model.resources.Method import io.vrap.rmf.raml.model.types.ArrayType import io.vrap.rmf.raml.model.types.FileType -class GoMethodRenderer constructor( - private val clientConstants: ClientConstants, - override val vrapTypeProvider: VrapTypeProvider, - @BasePackageName val basePackageName: String +class GoMethodRenderer( + override val vrapTypeProvider: VrapTypeProvider, + @BasePackageName val basePackageName: String ) : MethodRenderer, GoObjectTypeExtensions { override fun render(type: Method): TemplateFile { @@ -183,7 +182,7 @@ class GoMethodRenderer constructor( |} """.trimMargin() } else if (it.required) { - "${addStatement(it.name, "input.$name", vrapType)}" + addStatement(it.name, "input.$name", vrapType) } else { """ |if (input.$name != nil) { @@ -234,7 +233,7 @@ class GoMethodRenderer constructor( } private fun Method.renderFuncExecute(): String { - var methodReturn = if (this.returnType().toVrapType().goTypeName() != "nil") + val methodReturn = if (this.returnType().toVrapType().goTypeName() != "nil") "(result *${this.returnType().toVrapType().goTypeName()}, err error)" else "error" @@ -292,8 +291,6 @@ class GoMethodRenderer constructor( } fun Method.responseHandler(): String { - data class Key(val className: String, val success: Boolean) - val returnValue = if (this.hasReturnValue()) "nil, " else "" val switchStatements = this.responses .map { @@ -305,42 +302,34 @@ class GoMethodRenderer constructor( "nil" to statusCode } } - .groupBy { - Key(it.first, (it.second.toInt() in (200..299))) - } - .mapValues { - entry -> - entry.value.map { it.second.toInt() } - } .map { + val isSuccess = it.second.toInt() in (200..399) - val statusCodes = mutableListOf() - statusCodes.addAll(it.value) - - // Hack to work around incorrect importapi raml vs implementation - // if (statusCodes.contains(201) && !statusCodes.contains(200)) { - // statusCodes.add(200) - // } - if (it.key.className == "nil") { - if (it.key.success) { + if (it.first == "nil") { + if (isSuccess) { """ - |case ${statusCodes.joinToString(", ")}: + |case ${it.second.toInt()}: | return ${returnValue}nil """.trimMargin() } else { "" } } else { - if (it.key.success) { + if (isSuccess) { """ - |case ${statusCodes.joinToString(", ")}: + |case ${it.second.toInt()}: | err = json.Unmarshal(content, &result) | return result, nil """.trimMargin() + } else if (it.second.toInt() == 404) { + """ + |case ${it.second.toInt()}: + | return nil, ErrNotFound + """.trimMargin() } else { """ - |case ${statusCodes.joinToString(", ")}: - | errorObj := ${it.key.className}{} + |case ${it.second.toInt()}: + | errorObj := ${it.first}{} | err = json.Unmarshal(content, &errorObj) | if (err != nil) { | return ${returnValue}err @@ -361,7 +350,7 @@ class GoMethodRenderer constructor( |} |defer resp.Body.Close() |switch resp.StatusCode { - | <$switchStatements> + | <${switchStatements.trimEnd()}> | default: | result := GenericRequestError{ | StatusCode: resp.StatusCode, From 4126562657a6c6e6939be542cfe588bec7980449 Mon Sep 17 00:00:00 2001 From: Thomas De Meyer Date: Tue, 14 Nov 2023 16:30:57 +0100 Subject: [PATCH 2/5] fix: added return type for when an error occurs during unmarshalling --- .../io/vrap/codegen/languages/go/client/MethodRenderer.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/MethodRenderer.kt b/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/MethodRenderer.kt index 6f407b412..2fa53ccd3 100644 --- a/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/MethodRenderer.kt +++ b/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/MethodRenderer.kt @@ -319,6 +319,9 @@ class GoMethodRenderer( """ |case ${it.second.toInt()}: | err = json.Unmarshal(content, &result) + | if (err != nil) { + | return nil, err + | } | return result, nil """.trimMargin() } else if (it.second.toInt() == 404) { From a076cf53688ccbc7b46c73742d3fc63e07967129 Mon Sep 17 00:00:00 2001 From: Thomas De Meyer Date: Fri, 15 Dec 2023 16:05:34 +0100 Subject: [PATCH 3/5] fix: added default transport when no roundtripper is set --- .../vrap/codegen/languages/go/client/ClientFileProducer.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/ClientFileProducer.kt b/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/ClientFileProducer.kt index 462549c55..c564f16b0 100644 --- a/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/ClientFileProducer.kt +++ b/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/ClientFileProducer.kt @@ -65,7 +65,10 @@ class ClientFileProducer( | |func (sat *SetUserAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) { | req.Header.Set("User-Agent", sat.userAgent) - | return sat.T.RoundTrip(req) + | if sat.T != nil { + | return sat.T.RoundTrip(req) + | } + | return http.DefaultTransport.RoundTrip(req) |} | |// NewClient creates a new client based on the provided ClientConfig From fc8bd9faea9483919958bbe9f4261c1573463fff Mon Sep 17 00:00:00 2001 From: Thomas De Meyer Date: Tue, 20 Feb 2024 13:24:45 +0100 Subject: [PATCH 4/5] Update languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/ClientFileProducer.kt Co-authored-by: Jens Schulze --- .../vrap/codegen/languages/go/client/ClientFileProducer.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/ClientFileProducer.kt b/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/ClientFileProducer.kt index c564f16b0..6330cb082 100644 --- a/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/ClientFileProducer.kt +++ b/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/ClientFileProducer.kt @@ -66,9 +66,9 @@ class ClientFileProducer( |func (sat *SetUserAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) { | req.Header.Set("User-Agent", sat.userAgent) | if sat.T != nil { - | return sat.T.RoundTrip(req) - | } - | return http.DefaultTransport.RoundTrip(req) + | return sat.T.RoundTrip(req) + | } + | return http.DefaultTransport.RoundTrip(req) |} | |// NewClient creates a new client based on the provided ClientConfig From 7b7ddce5d848487e3b07f4de58e26e4d0351d349 Mon Sep 17 00:00:00 2001 From: Thomas De Meyer Date: Tue, 20 Feb 2024 13:24:50 +0100 Subject: [PATCH 5/5] Update languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/MethodRenderer.kt Co-authored-by: Jens Schulze --- .../io/vrap/codegen/languages/go/client/MethodRenderer.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/MethodRenderer.kt b/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/MethodRenderer.kt index 2fa53ccd3..5d6736e6b 100644 --- a/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/MethodRenderer.kt +++ b/languages/go/src/main/kotlin/io/vrap/codegen/languages/go/client/MethodRenderer.kt @@ -303,7 +303,7 @@ class GoMethodRenderer( } } .map { - val isSuccess = it.second.toInt() in (200..399) + val isSuccess = it.second.toInt() in (200..299) if (it.first == "nil") { if (isSuccess) {