-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate types and methods for subscriptions.
Subscription calls differ from query and procedure calls in that they are actually subscriptions to a websocket, not a standard HTTP call. For this reason, they actually respond with a `Flow<*>`, not just one value. Part of the actual CBOR implementation in kotlinx-serialization is adapted here to support byte arrays as byte strings for parameterized types, like List<ByteArray>. Kotlin/kotlinx.serialization#2383
- Loading branch information
1 parent
3db1b5c
commit 7246af5
Showing
19 changed files
with
1,084 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
api-gen-runtime-internal/src/commonMain/kotlin/sh/christian/ozone/api/cbor/ByteArrayInput.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
* | ||
* Copied until https://github.com/Kotlin/kotlinx.serialization/pull/2383 is merged and productionized. | ||
*/ | ||
package sh.christian.ozone.api.cbor | ||
|
||
internal class ByteArrayInput(private var array: ByteArray) { | ||
private var position: Int = 0 | ||
val availableBytes: Int get() = array.size - position | ||
|
||
fun read(): Int { | ||
return if (position < array.size) array[position++].toInt() and 0xFF else -1 | ||
} | ||
|
||
fun read(b: ByteArray, offset: Int, length: Int): Int { | ||
// avoid int overflow | ||
if (offset < 0 || offset > b.size || length < 0 | ||
|| length > b.size - offset | ||
) { | ||
throw IndexOutOfBoundsException() | ||
} | ||
// Are there any bytes available? | ||
if (this.position >= array.size) { | ||
return -1 | ||
} | ||
if (length == 0) { | ||
return 0 | ||
} | ||
|
||
val copied = if (this.array.size - position < length) this.array.size - position else length | ||
array.copyInto(destination = b, destinationOffset = offset, startIndex = position, endIndex = position + copied) | ||
position += copied | ||
return copied | ||
} | ||
|
||
fun skip(length: Int) { | ||
position += length | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...gen-runtime-internal/src/commonMain/kotlin/sh/christian/ozone/api/cbor/ByteArrayOutput.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
* | ||
* Copied until https://github.com/Kotlin/kotlinx.serialization/pull/2383 is merged and productionized. | ||
*/ | ||
package sh.christian.ozone.api.cbor | ||
|
||
internal class ByteArrayOutput { | ||
private var array: ByteArray = ByteArray(32) | ||
private var position: Int = 0 | ||
|
||
private fun ensureCapacity(elementsToAppend: Int) { | ||
if (position + elementsToAppend <= array.size) { | ||
return | ||
} | ||
val newArray = ByteArray((position + elementsToAppend).takeHighestOneBit() shl 1) | ||
array.copyInto(newArray) | ||
array = newArray | ||
} | ||
|
||
fun toByteArray(): ByteArray { | ||
val newArray = ByteArray(position) | ||
array.copyInto(newArray, startIndex = 0, endIndex = this.position) | ||
return newArray | ||
} | ||
|
||
fun write(buffer: ByteArray, offset: Int = 0, count: Int = buffer.size) { | ||
// avoid int overflow | ||
if (offset < 0 || offset > buffer.size || count < 0 | ||
|| count > buffer.size - offset | ||
) { | ||
throw IndexOutOfBoundsException() | ||
} | ||
if (count == 0) { | ||
return | ||
} | ||
|
||
ensureCapacity(count) | ||
buffer.copyInto( | ||
destination = array, | ||
destinationOffset = this.position, | ||
startIndex = offset, | ||
endIndex = offset + count | ||
) | ||
this.position += count | ||
} | ||
|
||
fun write(byteValue: Int) { | ||
ensureCapacity(1) | ||
array[position++] = byteValue.toByte() | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...ntime-internal/src/commonMain/kotlin/sh/christian/ozone/api/cbor/CborDecodingException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
* | ||
* Copied until https://github.com/Kotlin/kotlinx.serialization/pull/2383 is merged and productionized. | ||
*/ | ||
package sh.christian.ozone.api.cbor | ||
|
||
import kotlinx.serialization.SerializationException | ||
|
||
internal class CborDecodingException(message: String) : SerializationException(message) | ||
|
||
internal fun CborDecodingException(expected: String, foundByte: Int) = | ||
CborDecodingException("Expected $expected, but found ${printByte(foundByte)}") | ||
|
||
internal fun printByte(b: Int): String { | ||
val hexCode = "0123456789ABCDEF" | ||
return buildString { | ||
append(hexCode[b shr 4 and 0xF]) | ||
append(hexCode[b and 0xF]) | ||
} | ||
} |
Oops, something went wrong.