Skip to content

Commit

Permalink
Emit java toResponse headers (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilmveel authored Nov 12, 2024
1 parent 835573c commit ac583c3
Showing 1 changed file with 4 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ open class JavaEmitter(
).joinToString(",\n").let { if (it.isBlank()) "" else "\n$it\n${Spacer(3)}" }

private fun Endpoint.Response.emitSerialized() =
"""${Spacer(3)}if (response instanceof Response${status.firstToUpper()} r) { return new Wirespec.RawResponse(r.getStatus(), java.util.Collections.emptyMap(), ${if (content != null) "serialization.serialize(r.body, Wirespec.getType(${content.reference.emitType("Void")}.class, ${content.reference.isIterable}))" else "null"}); }"""
"""${Spacer(3)}if (response instanceof Response${status.firstToUpper()} r) { return new Wirespec.RawResponse(r.getStatus(), ${if(headers.isNotEmpty())"java.util.Map.ofEntries(${headers.joinToString { it.emitSerializedMap("headers") }})" else "java.util.Collections.emptyMap()"}, ${if (content != null) "serialization.serialize(r.body, Wirespec.getType(${content.reference.emitType("Void")}.class, ${content.reference.isIterable}))" else "null"}); }"""

private fun Endpoint.Response.emitDeserialized() =
"""${Spacer(4)}case $status -> new Response${status.firstToUpper()}(${this.emitDeserializedParams()});"""
Expand All @@ -326,6 +326,9 @@ open class JavaEmitter(
private fun Field.emitDeserialized(fields: String) =
"""${Spacer(4)}java.util.Optional.ofNullable(request.$fields().get("${identifier.value}")).map(it -> serialization.<${reference.emit()}>deserialize(it, Wirespec.getType(${reference.emitType()}.class, ${reference.isIterable})))${if(!isNullable) ".get()" else ""}"""

private fun Field.emitSerializedMap(fields: String) =
"""java.util.Map.entry("${identifier.value}", serialization.serialize(response.$fields.${identifier.emit(Field)}, Wirespec.getType(${reference.emitType()}.class, ${reference.isIterable})))"""

private fun Endpoint.Segment.Param.emitIdentifier() = "serialization.serialize(request.path.${identifier.emit(Field).firstToLower()}, Wirespec.getType(${reference.emitType()}.class, ${reference.isIterable}))"

private fun Endpoint.Content?.emitType() = this?.reference?.emitType() ?: "Void"
Expand Down

0 comments on commit ac583c3

Please sign in to comment.