Skip to content

Commit

Permalink
Improve deserialization of Model.OpenEnum
Browse files Browse the repository at this point in the history
  • Loading branch information
nomisRev committed Jul 3, 2024
1 parent f415c94 commit d4573cc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ private fun Model.Enum.Open.toTypeSpec(): TypeSpec {
.addCode(
CodeBlock.builder()
.addStatement("val value = decoder.decodeString()")
.addStatement("return attemptDeserialize(value,")
.addStatement("return deserializeOpenEnum(value, { OpenCase(value) },")
.withIndent {
rawToName.forEach { (_, name) ->
val nested = NamingContext.Nested(Named(name), context)
Expand All @@ -389,7 +389,6 @@ private fun Model.Enum.Open.toTypeSpec(): TypeSpec {
toClassName(nested)
)
}
addStatement("Pair(OpenCase::class) { OpenCase(value) }")
}
.addStatement(")")
.build()
Expand Down
17 changes: 11 additions & 6 deletions generation/src/main/kotlin/io/github/nomisrev/openapi/Predef.kt
Original file line number Diff line number Diff line change
Expand Up @@ -243,19 +243,25 @@ fun predef(): FileSpec =
.build()
)
.addFunction(
FunSpec.builder("attemptDeserialize")
FunSpec.builder("deserializeOpenEnum")
.addTypeVariable(TypeVariableName("A"))
.returns(TypeVariableName("A"))
.addParameter("value", String::class)
.addParameter(
"open",
LambdaTypeName.get(
parameters = listOf(ParameterSpec.unnamed(String::class)),
returnType = TypeVariableName("A")
)
)
.addParameter(
"block",
ClassName("kotlin", "Pair")
.parameterizedBy(
ClassName("kotlin.reflect", "KClass").parameterizedBy(TypeVariableName("*")),
LambdaTypeName.get(
receiver = null,
returnType = TypeVariableName("A").nullable(),
parameters = listOf(ParameterSpec.unnamed(String::class))
parameters = listOf(ParameterSpec.unnamed(String::class)),
returnType = TypeVariableName("A").nullable()
)
),
KModifier.VARARG
Expand All @@ -270,8 +276,7 @@ fun predef(): FileSpec =
errors[kclass] = e
}
}
// TODO Improve this error message
throw RuntimeException("BOOM! Improve this error message")
return open(value)
"""
.trimIndent()
)
Expand Down

0 comments on commit d4573cc

Please sign in to comment.