Skip to content

Commit

Permalink
fix: added fix to handle status codes a bit better
Browse files Browse the repository at this point in the history
  • Loading branch information
demeyerthom committed Nov 14, 2023
1 parent 5fdde84 commit 94fa3e5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 40 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 @@ -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()
)
}
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,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<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)
| 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 +350,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 94fa3e5

Please sign in to comment.