From ac583c3464ca0c7ca6fdfaa934cc0c1c1cefbe61 Mon Sep 17 00:00:00 2001 From: Willem Veelenturf Date: Tue, 12 Nov 2024 10:06:35 +0100 Subject: [PATCH] Emit java toResponse headers (#286) --- .../flock/wirespec/compiler/core/emit/JavaEmitter.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/JavaEmitter.kt b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/JavaEmitter.kt index 52a0ee61..99aea8dd 100644 --- a/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/JavaEmitter.kt +++ b/src/compiler/core/src/commonMain/kotlin/community/flock/wirespec/compiler/core/emit/JavaEmitter.kt @@ -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()});""" @@ -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"