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

Added Bson-Kotlin Array Codec #1457

Merged
merged 4 commits into from
Aug 1, 2024
Merged

Added Bson-Kotlin Array Codec #1457

merged 4 commits into from
Aug 1, 2024

Conversation

rozza
Copy link
Member

@rozza rozza commented Jul 22, 2024

Adds Kotlin array support to the bson-kotlin library

JAVA-5122

Adds Kotlin array support to the bson-kotlin library

JAVA-5122
@rozza rozza requested review from a team, stIncMale, vbabanin and jyemin and removed request for a team and stIncMale July 22, 2024 13:32
}
}
val codec =
if (nestedTypes.isEmpty()) codecRegistry.get(valueClass) else codecRegistry.get(valueClass, nestedTypes)
Copy link
Member

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?

Copy link
Member Author

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.

Comment on lines 40 to 48
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())
}
}
Copy link
Member

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.

Copy link
Member Author

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()
Copy link
Member

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?

Copy link
Member Author

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.

@rozza
Copy link
Member Author

rozza commented Jul 30, 2024

Thanks @vbabanin the when block looks much better

@rozza rozza requested a review from vbabanin July 30, 2024 08:34
@jyemin jyemin removed their request for review July 30, 2024 18:57
@rozza rozza merged commit 4df1108 into mongodb:master Aug 1, 2024
57 of 59 checks passed
@rozza rozza deleted the JAVA-5122 branch August 1, 2024 07:20
rozza added a commit to rozza/mongo-java-driver that referenced this pull request Aug 7, 2024
Adds Kotlin array support to the bson-kotlin library

JAVA-5122

Co-authored-by: Viacheslav Babanin <[email protected]>
rozza added a commit to rozza/mongo-java-driver that referenced this pull request Aug 7, 2024
Adds Kotlin array support to the bson-kotlin library

JAVA-5122

Co-authored-by: Viacheslav Babanin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants