Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move core to be mpp #20

Draft
wants to merge 42 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
88559c0
progress
i-walker Oct 24, 2021
b8e1bac
progress
i-walker Nov 1, 2021
b790c85
Merge remote-tracking branch 'origin/main' into mpp-core
i-walker Nov 3, 2021
be040bb
progress
i-walker Nov 3, 2021
6115b5f
progress
i-walker Nov 3, 2021
8484f2b
progress
i-walker Nov 3, 2021
e462f9f
fixed core mpp migration, except Uri
i-walker Nov 3, 2021
f7f7adb
further migrations
i-walker Jan 22, 2022
a121c0b
progress
i-walker Jan 24, 2022
cc72b47
clena up
i-walker Jan 24, 2022
5e8f52a
progress
i-walker Jan 24, 2022
23dedbf
progress
i-walker Jan 24, 2022
8445222
fixed core mpp migration, except Uri
i-walker Jan 24, 2022
e336eec
further migrations
i-walker Jan 24, 2022
9781ec0
Merge remote-tracking branch 'origin/mpp-core' into mpp-core
i-walker Jan 24, 2022
5de3b2a
Merge remote-tracking branch 'origin/main' into mpp-core
i-walker Jan 24, 2022
7d1f60b
remove ByteBuffer and inputstream references
i-walker Jan 24, 2022
7fdc953
clean up
i-walker Jan 24, 2022
f7c9c50
clean up
i-walker Jan 24, 2022
221759f
add Optin
i-walker Jan 24, 2022
e3bee82
add JvmInline
i-walker Jan 24, 2022
64cb6a3
clean up
i-walker Jan 24, 2022
aa20dc2
clean up
i-walker Jan 24, 2022
a81f90e
clean up
i-walker Jan 24, 2022
07bf507
add yarn lock
i-walker Jan 24, 2022
aebce42
small progress in uri parse
i-walker Jan 24, 2022
f43ef59
move to stdlibcommon
i-walker Jan 24, 2022
982a535
move to stdlibcommon
i-walker Jan 24, 2022
5b3bfa2
add mpp decoder
i-walker Jan 25, 2022
a78bd19
reorg and remove companion object of Uri
i-walker Jan 26, 2022
bc2ba9f
implement new parser, still need to authority
i-walker Jan 27, 2022
e343095
fix remaining errors
i-walker Jan 27, 2022
b9a2e0b
Merge remote-tracking branch 'origin/main' into mpp-core
i-walker Jan 27, 2022
7c5c3f4
mask testts
i-walker Jan 27, 2022
c7b7722
lowercase host nd unmask all tests
i-walker Jan 27, 2022
e4fbc9d
rm kotest dependency
i-walker Jan 27, 2022
c5dae11
implement js part
i-walker Jan 28, 2022
dd31440
clean up and add js related git ignore
i-walker Jan 28, 2022
564ec74
resolve missing dependiences, also
i-walker Jan 28, 2022
b618978
resolve Schema tests to be jvm only and compile and run jsTest
i-walker Jan 28, 2022
ca63cf2
fix js tests, besides a few encoding tests and change signature of Ur…
i-walker Jan 28, 2022
0a76fb3
make UrlencodedData mpp and simplify encodeQuery from Uricompatibily
i-walker Jan 30, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,18 @@ subprojects {
}

sourceSets {
val commonMain by getting {
i-walker marked this conversation as resolved.
Show resolved Hide resolved
dependencies {
implementation(rootProject.libs.kotlin.stdlibCommon)
}
}

val jvmMain by getting {
dependsOn(commonMain)
}

val commonTest by getting {
dependsOn(commonMain)
dependencies {
implementation(rootProject.libs.coroutines.core)
implementation(rootProject.libs.kotest.assertionsCore)
Expand All @@ -42,6 +53,7 @@ subprojects {
}

val jvmTest by getting {
dependsOn(commonMain)
dependencies {
implementation(rootProject.libs.kotest.runnerJUnit5)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ public fun <I, E, O> Endpoint<I, E, O>.toRequest(baseUrl: String, i: I): Request

return when (val body = info.body) {
is Body.ByteArray -> r4.body(MemoryBody(body.byteArray))
is Body.ByteBuffer -> r4.body(MemoryBody(body.byteBuffer))
is Body.InputStream -> r4.body(body.inputStream)
is Body.String -> r4.body(body.string)
null -> r4
}
Expand Down Expand Up @@ -150,8 +148,6 @@ private fun EndpointOutput<*>.getOutputParams(
when (val output = this) {
is EndpointOutput.Single<*> -> when (val single = (output as EndpointOutput.Single<Any?>)) {
is EndpointIO.ByteArrayBody -> single.codec.decode(response.body.payload.array())
is EndpointIO.ByteBufferBody -> single.codec.decode(response.body.payload)
is EndpointIO.InputStreamBody -> single.codec.decode(response.body.stream)
is EndpointIO.StringBody -> single.codec.decode(response.body.toString())

is EndpointIO.Empty -> single.codec.decode(Unit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ public fun <I, E, O> Endpoint<I, E, O>.toRequestBuilder(baseUrl: String, input:
}
body = when (val body = info.body) {
is Body.ByteArray -> ByteArrayContent(body.byteArray/* contentType, statusCode*/)
is Body.ByteBuffer -> ByteArrayContent(body.byteBuffer.array())
is Body.InputStream -> ByteArrayContent(body.inputStream.readBytes())

// TODO fix ContentType
is Body.String -> TextContent(body.string, ContentType.Text.Plain)
Expand Down Expand Up @@ -150,8 +148,6 @@ private suspend fun EndpointOutput<*>.outputParams(
is EndpointOutput.Single<*> ->
when (this) {
is EndpointIO.ByteArrayBody -> codec.decode(response.receive())
is EndpointIO.ByteBufferBody -> codec.decode(ByteBuffer.wrap(response.receive<ByteArray>()))
is EndpointIO.InputStreamBody -> codec.decode(response.receive())
is EndpointIO.StringBody -> codec.decode(response.receive())
is EndpointIO.Empty -> codec.decode(Unit)
is EndpointIO.Header -> codec.decode(headers.getAll(name).orEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ private fun EndpointOutput<*>.getOutputParams(
when (val output = this) {
is EndpointOutput.Single<*> -> when (val single = (output as EndpointOutput.Single<Any?>)) {
is EndpointIO.ByteArrayBody -> single.codec.decode(response.body.readBytes())
is EndpointIO.ByteBufferBody -> single.codec.decode(ByteBuffer.wrap(response.body.readBytes()))
is EndpointIO.InputStreamBody -> single.codec.decode(response.body.readBytes().inputStream())
is EndpointIO.StringBody -> single.codec.decode(String(response.body.readBytes()))
is EndpointIO.Empty -> single.codec.decode(Unit)
is EndpointOutput.FixedStatusCode -> single.codec.decode(Unit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,6 @@ private suspend fun EndpointOutput<*>.getOutputParams(
when (val output = this) {
is EndpointOutput.Single<*> -> when (val single = (output as EndpointOutput.Single<Any?>)) {
is EndpointIO.ByteArrayBody -> single.codec.decode(response.awaitBodyOrNull(ByteArray::class) ?: byteArrayOf())
is EndpointIO.ByteBufferBody -> single.codec.decode(
response.awaitBodyOrNull(ByteBuffer::class) ?: ByteBuffer.wrap(byteArrayOf())
)
is EndpointIO.InputStreamBody -> single.codec.decode(
ByteArrayInputStream(
response.awaitBodyOrNull(ByteArray::class) ?: byteArrayOf()
)
)
is EndpointIO.StringBody -> single.codec.decode(response.awaitBodyOrNull(String::class) ?: "")
is EndpointIO.Empty -> single.codec.decode(Unit)
is EndpointOutput.FixedStatusCode -> single.codec.decode(Unit)
Expand Down
13 changes: 4 additions & 9 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
kotlin {
sourceSets {
commonMain {
dependencies {
implementation(kotlin("stdlib", Version.kotlin))
implementation(Libs.kotlinxCoroutines)
implementation(Libs.ktorio)
}
}

jvmMain {
dependencies {
// Needed for Uri MatchNamedGroupCollection, ties us to JDK8
// TODO https://app.clickup.com/t/kt7qd2
implementation(kotlin("stdlib-jdk8", Version.kotlin))
api(libs.arrow.core)
implementation(libs.kotlin.stdlibJDK8)
implementation(libs.coroutines.core)
api(libs.ktor.io)
}
}
}
Expand Down
6 changes: 0 additions & 6 deletions core/src/commonMain/kotlin/arrow/endpoint/ArrowEndpoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,6 @@ public object ArrowEndpoint {
public fun byteArrayBody(): EndpointIO.ByteArrayBody<ByteArray> =
EndpointIO.ByteArrayBody(Codec.byteArray, EndpointIO.Info.empty())

public fun byteBufferBody(): EndpointIO.ByteBufferBody<ByteChannel> =
EndpointIO.ByteBufferBody(Codec.byteBuffer, EndpointIO.Info.empty())

public fun inputStreamBody(): EndpointIO.InputStreamBody<ByteReadChannel> =
EndpointIO.InputStreamBody(Codec.inputStream, EndpointIO.Info.empty())

public fun <A> formBody(codec: Codec<String, A, CodecFormat.XWwwFormUrlencoded>): EndpointIO.StringBody<A> =
anyFromStringBody(codec)

Expand Down
31 changes: 2 additions & 29 deletions core/src/commonMain/kotlin/arrow/endpoint/EndpointIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package arrow.endpoint
import arrow.core.Tuple4
import arrow.core.Tuple5
import arrow.endpoint.model.CodecFormat
import java.io.InputStream
import java.nio.ByteBuffer
import java.nio.charset.Charset
import io.ktor.utils.io.charsets.Charset
import kotlin.jvm.JvmName

// Elements that can occur in both input and output
// Such as body, headers, etc
Expand Down Expand Up @@ -90,32 +89,6 @@ public sealed interface EndpointIO<A> : EndpointInput<A>, EndpointOutput<A> {
}
}

public data class ByteBufferBody<T>(
override val codec: Codec<ByteBuffer, T, CodecFormat>,
override val info: Info<T>
) : BinaryBody<ByteBuffer, T> {
override fun <B> copyWith(c: Codec<ByteBuffer, B, CodecFormat>, i: Info<B>): ByteBufferBody<B> =
ByteBufferBody(c, i)

override fun toString(): String {
val format = codec.format.mediaType
return "{body as $format}"
}
}

public data class InputStreamBody<T>(
override val codec: Codec<InputStream, T, CodecFormat>,
override val info: Info<T>
) : BinaryBody<InputStream, T> {
override fun <B> copyWith(c: Codec<InputStream, B, CodecFormat>, i: Info<B>): InputStreamBody<B> =
InputStreamBody(c, i)

override fun toString(): String {
val format = codec.format.mediaType
return "{body as $format}"
}
}

public data class Info<T>(val description: String?, val examples: List<Example<T>>, val deprecated: Boolean) {
public fun description(d: String): Info<T> = copy(description = d)
public fun example(): T? = examples.firstOrNull()?.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,9 @@ public fun <I> EndpointInput<I>.requestInfo(
is EndpointIO.ByteArrayBody -> {
body = Body.ByteArray(codec.encode(value), codec.format)
}
is EndpointIO.ByteBufferBody -> {
body = Body.ByteBuffer(codec.encode(value), codec.format)
}
is EndpointIO.InputStreamBody -> {
body = Body.InputStream(codec.encode(value), codec.format)
}
is EndpointIO.StringBody -> {
body = Body.String(charset, codec.encode(value), codec.format)
}

is EndpointIO.Empty -> Unit
is EndpointInput.FixedMethod -> {
method = this.m
Expand Down
15 changes: 7 additions & 8 deletions core/src/commonMain/kotlin/arrow/endpoint/model/Rfc3986.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import arrow.core.left
import arrow.core.right
import io.ktor.utils.io.charsets.Charset
import io.ktor.utils.io.charsets.Charsets
import io.ktor.utils.io.charsets.decode
import io.ktor.utils.io.core.toByteArray
import kotlin.experimental.and

internal object Rfc3986 {
private val AlphaNum: Set<Char> = (('a'..'z') + ('A'..'Z') + ('0'..'9')).toSet()
Expand Down Expand Up @@ -99,7 +97,7 @@ internal object Rfc3986 {
// "%x" will cause an exception to be thrown
if ((i < numChars) && (c == '%'))
return UriError.IllegalArgument("URLDecoder: Incomplete trailing escape (%) pattern").left()
sb.append(bytes.joinToString { it.toString(16)}, startIndex = 0, endIndex = pos)
sb.append(bytes.joinToString { it.toString(16) }, startIndex = 0, endIndex = pos)
needToChange = true
}
else -> {
Expand All @@ -111,12 +109,13 @@ internal object Rfc3986 {
return (if (needToChange) sb.toString() else this).right()
}

// private fun Byte.format(): String = "%02X".format(this)
private val hexArray: CharArray
get() = "0123456789ABCDEF".toCharArray()

// TODO: previously Jvm specific with String.format("%02x", this), check if this is cohesive
private fun Byte.format(): String {
val decimal = this.and(0xff.toByte())
val hex = decimal.toUInt().toString(16)
return if(hex.length.mod(2) == 1) "0$hex" else hex
val v = toInt().and(0xFF)
val a = hexArray[v ushr 4]
val b = hexArray[v and 0x0F]
return "$a$b"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,6 @@ internal data class OutputValues(
val mapping = output.codec as Mapping<ByteArray, Any?>
ov.withBody(Body.ByteArray(mapping.encode(value.asAny), output.codec.format), output)
}
is EndpointIO.ByteBufferBody -> {
val mapping = output.codec as Mapping<ByteBuffer, Any?>
ov.withBody(Body.ByteBuffer(mapping.encode(value.asAny), output.codec.format), output)
}
is EndpointIO.InputStreamBody -> {
val mapping = output.codec as Mapping<InputStream, Any?>
ov.withBody(Body.InputStream(mapping.encode(value.asAny), output.codec.format), output)
}
is EndpointIO.StringBody -> {
val mapping = output.codec as Mapping<String, Any?>
ov.withBody(Body.String(output.charset, mapping.encode(value.asAny), output.codec.format), output)
Expand Down
3 changes: 2 additions & 1 deletion core/src/jvmMain/kotlin/arrow/endpoint/Codec.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package arrow.endpoint

import arrow.endpoint.model.CodecFormat
import io.ktor.utils.io.charsets.Charset
import java.io.InputStream
import java.math.BigDecimal
import java.nio.ByteBuffer
import kotlin.text.Charsets
import java.nio.charset.Charset
import java.time.Duration as JavaDuration
import java.time.Instant
import java.time.LocalDate
Expand Down
8 changes: 8 additions & 0 deletions core/src/jvmMain/kotlin/arrow/endpoint/Schema.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,31 @@ public val Schema.Companion.inputStream: Schema<InputStream>

public val Schema.Companion.instant: Schema<Instant>
get() = Schema.DateTime()

public val Schema.Companion.zonedDateTime: Schema<ZonedDateTime>
get() = Schema.DateTime()

public val Schema.Companion.offsetDateTime: Schema<OffsetDateTime>
get() = Schema.DateTime()

public val Schema.Companion.date: Schema<java.util.Date>
get() = Schema.DateTime()

public val Schema.Companion.localDateTime: Schema<LocalDateTime>
get() = Schema.String()

public val Schema.Companion.localDate: Schema<LocalDate>
get() = Schema.String()

public val Schema.Companion.zoneOffset: Schema<ZoneOffset>
get() = Schema.String()

public val Schema.Companion.javaDuration: Schema<Duration>
get() = Schema.String()

public val Schema.Companion.localTime: Schema<LocalTime>
get() = Schema.String()

public val Schema.Companion.offsetTime: Schema<OffsetTime>
get() = Schema.String()

Expand Down
19 changes: 0 additions & 19 deletions core/src/jvmMain/kotlin/arrow/endpoint/model/RequestMetadata.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import io.ktor.http.HttpStatusCode
import io.ktor.http.RequestConnectionPoint
import io.ktor.http.content.ByteArrayContent
import io.ktor.http.content.OutgoingContent
import io.ktor.http.content.OutputStreamContent
import io.ktor.http.content.TextContent
import io.ktor.http.withCharset
import io.ktor.request.host
Expand All @@ -39,8 +38,6 @@ import io.ktor.response.header
import io.ktor.response.respond
import io.ktor.util.flattenEntries
import io.ktor.util.toByteArray
import io.ktor.utils.io.jvm.javaio.toInputStream
import java.nio.ByteBuffer

public fun <I, E, O> Application.install(ses: ServerEndpoint<I, E, O>): Unit =
install(listOf(ses))
Expand Down Expand Up @@ -73,23 +70,11 @@ public fun ServerResponse.outgoingContent(): OutgoingContent? =
body.contentType(),
HttpStatusCode.fromValue(code.code)
)
is Body.ByteBuffer -> ByteArrayContent(
body.toByteArray(),
body.contentType(),
HttpStatusCode.fromValue(code.code)
)
is Body.String -> TextContent(
body.string,
body.contentType(),
HttpStatusCode.fromValue(code.code)
)
is Body.InputStream -> OutputStreamContent(
{
body.inputStream.copyTo(this)
},
body.contentType(),
HttpStatusCode.fromValue(code.code)
)
else -> null
}

Expand Down Expand Up @@ -135,8 +120,6 @@ internal class KtorRequestBody(private val ctx: ApplicationCall) : RequestBody {
val body = ctx.request.receiveChannel()
return when (bodyType) {
is EndpointIO.ByteArrayBody -> body.toByteArray()
is EndpointIO.ByteBufferBody -> ByteBuffer.wrap(body.toByteArray())
is EndpointIO.InputStreamBody -> body.toInputStream()
is EndpointIO.StringBody -> body.toByteArray().toString(bodyType.charset)
} as R
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ private class SpringRequestBody(
val body: DataBuffer? = request.awaitBodyOrNull()
return when (bodyType) {
is EndpointIO.ByteArrayBody -> toByteArray(body)
is EndpointIO.ByteBufferBody -> body?.asByteBuffer()
is EndpointIO.InputStreamBody -> body?.asInputStream()
is EndpointIO.StringBody -> body?.toString(Charsets.UTF_8)
is EndpointIO.StringBody -> body?.toString(Charsets.UTF_8)
} as R
}

Expand Down
4 changes: 0 additions & 4 deletions test/src/jvmMain/kotlin/arrow/endpoint/test/MockWebServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ internal class RequestBody(val ctx: RecordedRequest) : RequestBody {
override suspend fun <R> toRaw(bodyType: EndpointIO.Body<R, *>): R {
return when (bodyType) {
is EndpointIO.ByteArrayBody -> withContext(Dispatchers.IO) { ctx.body.readByteArray() }
is EndpointIO.ByteBufferBody -> withContext(Dispatchers.IO) { ByteBuffer.wrap(ctx.body.readByteArray()) }
is EndpointIO.InputStreamBody -> ctx.body.inputStream()
is EndpointIO.StringBody -> withContext(Dispatchers.IO) { ctx.body.readByteArray().toString(bodyType.charset) }
} as R
}
Expand All @@ -78,8 +76,6 @@ public fun RecordedRequest.toServerRequest(): ServerRequest =
public fun MockResponse.setBody(response: ServerResponse): MockResponse =
when (val r = response.body) {
is Body.ByteArray -> setBody(Buffer().apply { write(r.byteArray) })
is Body.ByteBuffer -> setBody(Buffer().apply { write(r.byteBuffer) })
is Body.InputStream -> setBody(Buffer().apply { readFrom(r.inputStream) })
is Body.String -> setBody(r.string)
else -> this
}