-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Added Bson-Kotlin Array Codec #1457
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
128 changes: 128 additions & 0 deletions
128
bson-kotlin/src/main/kotlin/org/bson/codecs/kotlin/ArrayCodec.kt
This file contains hidden or 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,128 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.bson.codecs.kotlin | ||
|
||
import java.lang.reflect.ParameterizedType | ||
import java.lang.reflect.Type | ||
import kotlin.reflect.KClass | ||
import org.bson.BsonReader | ||
import org.bson.BsonType | ||
import org.bson.BsonWriter | ||
import org.bson.codecs.Codec | ||
import org.bson.codecs.DecoderContext | ||
import org.bson.codecs.EncoderContext | ||
import org.bson.codecs.configuration.CodecRegistry | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
internal data class ArrayCodec<R : Any, V>(private val kClass: KClass<R>, private val codec: Codec<V?>) : Codec<R> { | ||
|
||
companion object { | ||
internal fun <R : Any> create( | ||
kClass: KClass<R>, | ||
typeArguments: List<Type>, | ||
codecRegistry: CodecRegistry | ||
): Codec<R> { | ||
assert(kClass.javaObjectType.isArray) { "$kClass must be an array type" } | ||
val (valueClass, nestedTypes) = | ||
if (typeArguments.isEmpty()) { | ||
Pair(kClass.java.componentType.kotlin.javaObjectType as Class<Any>, emptyList()) | ||
} else { | ||
// Unroll the actual class and any type arguments | ||
when (val pType = typeArguments[0]) { | ||
is Class<*> -> Pair(pType as Class<Any>, emptyList()) | ||
is ParameterizedType -> Pair(pType.rawType as Class<Any>, pType.actualTypeArguments.toList()) | ||
else -> Pair(Object::class.java as Class<Any>, emptyList()) | ||
} | ||
} | ||
val codec = | ||
if (nestedTypes.isEmpty()) codecRegistry.get(valueClass) else codecRegistry.get(valueClass, nestedTypes) | ||
return ArrayCodec(kClass, codec) | ||
} | ||
} | ||
|
||
private val isPrimitiveArray = kClass.java.componentType != kClass.java.componentType.kotlin.javaObjectType | ||
|
||
override fun encode(writer: BsonWriter, arrayValue: R, encoderContext: EncoderContext) { | ||
writer.writeStartArray() | ||
|
||
boxed(arrayValue).forEach { | ||
if (it == null) writer.writeNull() else encoderContext.encodeWithChildContext(codec, writer, it) | ||
} | ||
|
||
writer.writeEndArray() | ||
} | ||
|
||
override fun getEncoderClass(): Class<R> = kClass.java | ||
|
||
override fun decode(reader: BsonReader, decoderContext: DecoderContext): R { | ||
reader.readStartArray() | ||
val data = ArrayList<V?>() | ||
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) { | ||
if (reader.currentBsonType == BsonType.NULL) { | ||
reader.readNull() | ||
data.add(null) | ||
} else { | ||
data.add(decoderContext.decodeWithChildContext(codec, reader)) | ||
} | ||
} | ||
reader.readEndArray() | ||
return unboxed(data) | ||
} | ||
|
||
fun boxed(arrayValue: R): Iterable<V?> { | ||
val boxedValue = | ||
if (!isPrimitiveArray) { | ||
(arrayValue as Array<V?>).asIterable() | ||
} else if (arrayValue is BooleanArray) { | ||
arrayValue.asIterable() | ||
} else if (arrayValue is ByteArray) { | ||
arrayValue.asIterable() | ||
} else if (arrayValue is CharArray) { | ||
arrayValue.asIterable() | ||
} else if (arrayValue is DoubleArray) { | ||
arrayValue.asIterable() | ||
} else if (arrayValue is FloatArray) { | ||
arrayValue.asIterable() | ||
} else if (arrayValue is IntArray) { | ||
arrayValue.asIterable() | ||
} else if (arrayValue is LongArray) { | ||
arrayValue.asIterable() | ||
} else if (arrayValue is ShortArray) { | ||
arrayValue.asIterable() | ||
} else { | ||
throw IllegalArgumentException("Unsupported array type ${arrayValue.javaClass}") | ||
} | ||
return boxedValue as Iterable<V?> | ||
} | ||
|
||
private fun unboxed(data: ArrayList<V?>): R { | ||
return when (kClass) { | ||
BooleanArray::class -> (data as ArrayList<Boolean>).toBooleanArray() as R | ||
ByteArray::class -> (data as ArrayList<Byte>).toByteArray() as R | ||
CharArray::class -> (data as ArrayList<Char>).toCharArray() as R | ||
DoubleArray::class -> (data as ArrayList<Double>).toDoubleArray() as R | ||
FloatArray::class -> (data as ArrayList<Float>).toFloatArray() as R | ||
IntArray::class -> (data as ArrayList<Int>).toIntArray() as R | ||
LongArray::class -> (data as ArrayList<Long>).toLongArray() as R | ||
ShortArray::class -> (data as ArrayList<Short>).toShortArray() as R | ||
else -> data.toArray(arrayOfNulls(data.size)) as R | ||
} | ||
} | ||
|
||
private fun arrayOfNulls(size: Int): Array<V?> { | ||
return java.lang.reflect.Array.newInstance(codec.encoderClass, size) as Array<V?> | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
bson-kotlin/src/main/kotlin/org/bson/codecs/kotlin/ArrayCodecProvider.kt
This file contains hidden or 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,31 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.bson.codecs.kotlin | ||
|
||
import java.lang.reflect.Type | ||
import org.bson.codecs.Codec | ||
import org.bson.codecs.configuration.CodecProvider | ||
import org.bson.codecs.configuration.CodecRegistry | ||
|
||
/** A Kotlin reflection based Codec Provider for data classes */ | ||
public class ArrayCodecProvider : CodecProvider { | ||
override fun <T : Any> get(clazz: Class<T>, registry: CodecRegistry): Codec<T>? = get(clazz, emptyList(), registry) | ||
|
||
override fun <T : Any> get(clazz: Class<T>, typeArguments: List<Type>, registry: CodecRegistry): Codec<T>? = | ||
if (clazz.isArray) { | ||
ArrayCodec.create(clazz.kotlin, typeArguments, registry) | ||
} else null | ||
} |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a question: Is it our Kotlin code style to use single-line if-else statements without braces?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we follow the main kotlin coding standards.
See: https://kotlinlang.org/docs/control-flow.html#if-expression
It looks unusual as in Java we'd use a tenary operator but in Kotlin there is no such thing.