Skip to content

Commit

Permalink
Add iOS target on multiplatform module
Browse files Browse the repository at this point in the history
Resolves:
#130
  • Loading branch information
joffrey-bion committed Aug 14, 2021
1 parent 94e32ff commit e811af9
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:

jobs:
publish:
runs-on: ubuntu-latest
runs-on: macos-latest # For iOS publications
steps:
- name: Checkout repository
uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:

jobs:
release:
runs-on: ubuntu-latest
runs-on: macos-latest # For iOS tests
steps:
- name: Checkout repository
uses: actions/checkout@v2
Expand Down
2 changes: 2 additions & 0 deletions krossbow-stomp-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ kotlin {
}
}
}
ios()

sourceSets {
all {
languageSettings.useExperimentalAnnotation("kotlin.RequiresOptIn")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.hildan.krossbow.stomp.charsets

actual abstract class Charset(internal val _name: String) {
actual abstract fun newEncoder(): CharsetEncoder
actual abstract fun newDecoder(): CharsetDecoder
}

internal data class CharsetImpl(val name: String) : Charset(name) {
override fun newEncoder(): CharsetEncoder = CharsetEncoderImpl(this)
override fun newDecoder(): CharsetDecoder = CharsetDecoderImpl(this)
}

internal actual fun String.toCharset() = when (lowercase().replace('_', '-')) {
"utf-8", "utf8" -> Charsets.UTF_8
"iso-8859-1", "latin1" -> Charsets.ISO_8859_1
else -> throw IllegalArgumentException("Charset $this is not supported")
}

actual abstract class CharsetDecoder(internal val _charset: Charset)
actual abstract class CharsetEncoder(internal val _charset: Charset)

internal data class CharsetDecoderImpl(private val charset: Charset) : CharsetDecoder(charset)
internal data class CharsetEncoderImpl(private val charset: Charset) : CharsetEncoder(charset)

actual object Charsets {
actual val UTF_8: Charset = CharsetImpl("UTF-8")
actual val ISO_8859_1: Charset = CharsetImpl("ISO-8859-1")
}

@OptIn(ExperimentalStdlibApi::class)
actual fun CharsetEncoder.encode(input: String): ByteArray = when (_charset) {
Charsets.UTF_8 -> input.encodeToByteArray()
else -> error("Non UTF-8 encodings are not supported on iOS platform")
}

@OptIn(ExperimentalStdlibApi::class)
actual fun CharsetDecoder.decode(bytes: ByteArray): String = when (_charset) {
Charsets.UTF_8 -> bytes.decodeToString()
else -> error("Non UTF-8 encodings are not supported on iOS platform")
}
2 changes: 2 additions & 0 deletions krossbow-stomp-kxserialization/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ kotlin {
nodejs()
browser()
}
ios()

sourceSets {
val commonMain by getting {
dependencies {
Expand Down
2 changes: 2 additions & 0 deletions krossbow-websocket-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ kotlin {
nodejs()
browser()
}
ios()

sourceSets {
all {
languageSettings.useExperimentalAnnotation("kotlin.RequiresOptIn")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.hildan.krossbow.websocket

actual fun defaultWebSocketClient(): WebSocketClient =
throw NotImplementedError("There is no default web socket client on iOS target at the moment, please provide your" +
" own client implementation.")
2 changes: 2 additions & 0 deletions krossbow-websocket-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ kotlin {
nodejs()
browser()
}
ios()

sourceSets {
all {
languageSettings.useExperimentalAnnotation("kotlin.RequiresOptIn")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.hildan.krossbow.websocket.test

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.runBlocking

actual fun runSuspendingTest(block: suspend CoroutineScope.() -> Unit) = runBlocking { block() }

internal actual suspend fun runAlongEchoWSServer(block: suspend (port: Int) -> Unit) {
TODO("Implement test WS echo server on native platform")
}

0 comments on commit e811af9

Please sign in to comment.