-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Added Bson-Kotlin Array Codec #1457
Conversation
Adds Kotlin array support to the bson-kotlin library JAVA-5122
} | ||
} | ||
val codec = | ||
if (nestedTypes.isEmpty()) codecRegistry.get(valueClass) else codecRegistry.get(valueClass, nestedTypes) |
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.
bson-kotlin/src/main/kotlin/org/bson/codecs/kotlin/ArrayCodec.kt
Outdated
Show resolved
Hide resolved
if (typeArguments.isEmpty()) { | ||
Pair(kClass.java.componentType.kotlin.javaObjectType as Class<Any>, emptyList()) | ||
} else { | ||
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()) | ||
} | ||
} |
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.
[Optional] We could consider extracting this code block into a named method to enhance clarity.
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.
As its a single block, I opted to add a comment instead.
if (!isPrimitiveArray) { | ||
(arrayValue as Array<V?>).iterator() | ||
} else if (arrayValue is BooleanArray) { | ||
arrayValue.toList().iterator() |
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.
Using toList() seems redundant since we can directly use an iterator on the array itself. Is this additional conversion to a List necessary?
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.
It was used to stop a spotbugs error, however, using asIterable
works without triggering the error.
Thanks @vbabanin the when block looks much better |
Adds Kotlin array support to the bson-kotlin library JAVA-5122 Co-authored-by: Viacheslav Babanin <[email protected]>
Adds Kotlin array support to the bson-kotlin library JAVA-5122 Co-authored-by: Viacheslav Babanin <[email protected]>
Adds Kotlin array support to the bson-kotlin library
JAVA-5122