Skip to content

Commit

Permalink
Merge pull request commercetools#305 from labd/feat/go-add-404
Browse files Browse the repository at this point in the history
Added fix to handle status codes in go generator a bit better
  • Loading branch information
jenschude authored Feb 21, 2024
2 parents 81fce2e + 7b7ddce commit f93710a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<TemplateFile> {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@ object GoClientModule : Module {
MethodGenerator(
setOf(
GoMethodRenderer(
generatorModule.clientConstants(),
generatorModule.vrapTypeProvider(),
generatorModule.providePackageName()
generatorModule.vrapTypeProvider(),
generatorModule.providePackageName()
)
),
generatorModule.allResourceMethods()
),
FileGenerator(
setOf(
ClientFileProducer(
generatorModule.clientConstants(),
generatorModule.provideRamlModel(),
generatorModule.providePackageName()
generatorModule.provideRamlModel(),
generatorModule.providePackageName()
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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<Int>()
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
Expand All @@ -361,7 +353,7 @@ class GoMethodRenderer constructor(
|}
|defer resp.Body.Close()
|switch resp.StatusCode {
| <$switchStatements>
| <${switchStatements.trimEnd()}>
| default:
| result := GenericRequestError{
| StatusCode: resp.StatusCode,
Expand Down

0 comments on commit f93710a

Please sign in to comment.