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..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 @@ -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 { @@ -66,7 +65,10 @@ class ClientFileProducer constructor( | |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 @@ -201,6 +203,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..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 @@ -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,37 @@ 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..299) - 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) + | if (err != nil) { + | return nil, err + | } | 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 +353,7 @@ class GoMethodRenderer constructor( |} |defer resp.Body.Close() |switch resp.StatusCode { - | <$switchStatements> + | <${switchStatements.trimEnd()}> | default: | result := GenericRequestError{ | StatusCode: resp.StatusCode,