Skip to content

Commit

Permalink
resolves #104 kotlinx.serialization support
Browse files Browse the repository at this point in the history
  • Loading branch information
zigzago committed Oct 13, 2019
1 parent ffec0d1 commit e06b3dd
Show file tree
Hide file tree
Showing 76 changed files with 1,700 additions and 99 deletions.
110 changes: 110 additions & 0 deletions kmongo-async-serialization/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<!--
~ Copyright (C) 2017/2019 Litote
~
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.litote.kmongo</groupId>
<artifactId>kmongo-root</artifactId>
<version>3.11.1-SNAPSHOT</version>
</parent>

<artifactId>kmongo-async-serialization</artifactId>
<name>KMongo async serialization</name>
<description>KMongo asynchronous client with kotlinx serialization object mapping</description>

<dependencies>
<dependency>
<groupId>org.litote.kmongo</groupId>
<artifactId>kmongo-async-core</artifactId>
</dependency>
<dependency>
<groupId>org.litote.kmongo</groupId>
<artifactId>kmongo-serialization-mapping</artifactId>
</dependency>

<dependency>
<groupId>org.litote.kmongo</groupId>
<artifactId>kmongo-async-core-tests</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<groups>org.litote.kmongo.SerializationMappingCategory</groups>
<dependenciesToScan>
<dependency>org.litote.kmongo:kmongo-async-core-tests</dependency>
</dependenciesToScan>
<includes>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*Tests.java</include>
<include>**/*TestCase.java</include>
<include>**/*Issue*.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.jetbrains.dokka</groupId>
<artifactId>dokka-maven-plugin</artifactId>
<version>${plugin.dokka.version}</version>
<executions>
<execution>
<goals>
<goal>dokka</goal>
<goal>javadoc</goal>
<goal>javadocJar</goal>
</goals>
</execution>
</executions>
<configuration>
<jdkVersion>8</jdkVersion>
<reportNotDocumented>false</reportNotDocumented>
<sourceLinks>
<link>
<dir>${project.basedir}/../kmongo-async-core/src/main/kotlin</dir>
<url>https://github.com/Litote/kmongo/blob/master/kmongo-async-core/src/main/kotlin
</url>
<urlSuffix>#L</urlSuffix>
</link>
</sourceLinks>
<sourceDirectories>
<dir>${project.basedir}/../kmongo-async-core/src/main/kotlin</dir>
</sourceDirectories>
<externalDocumentationLinks>
<link>
<url>http://mongodb.github.io/mongo-java-driver/${mongo.base.version}/javadoc/</url>
</link>
</externalDocumentationLinks>
<perPackageOptions>
<packageOptions>
<prefix>kotlin</prefix>
<skipDeprecated>false</skipDeprecated>
<reportUndocumented>true</reportUndocumented>
<includeNonPublic>false</includeNonPublic>
</packageOptions>
</perPackageOptions>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package org.litote.kmongo

import com.mongodb.MongoCommandException
import com.mongodb.client.MongoCollection
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import org.bson.codecs.pojo.annotations.BsonId
import org.junit.After
import org.junit.Before
Expand All @@ -37,12 +39,15 @@ import kotlin.test.fail

class AggregateTest : AllCategoriesKMongoBaseTest<Article>() {

@Serializable
data class Article(val title: String, val author: String, val tags: List<String>) {

constructor(title: String, author: String, vararg tags: String) : this(title, author, tags.asList())
}

@Serializable
private data class Result(
@SerialName("_id")
@BsonId val title: String,
val averageYear: Double = 0.0,
val count: Int = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package org.litote.kmongo

import com.mongodb.client.MongoCollection
import kotlinx.serialization.ContextualSerialization
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import org.bson.codecs.pojo.annotations.BsonId
import org.junit.After
import org.junit.Before
Expand All @@ -33,10 +36,12 @@ import kotlin.test.assertTrue
*/
class AggregateTypedTest : AllCategoriesKMongoBaseTest<Article>() {

@Serializable
data class Article(
val title: String,
val author: String,
val tags: List<String>,
@ContextualSerialization
val date: Instant = Instant.now(),
val count: Int = 1,
val ok: Boolean = true
Expand All @@ -45,7 +50,9 @@ class AggregateTypedTest : AllCategoriesKMongoBaseTest<Article>() {
constructor(title: String, author: String, vararg tags: String) : this(title, author, tags.asList())
}

@Serializable
private data class Result(
@SerialName("_id")
@BsonId val title: String,
val averageYear: Double = 0.0,
val count: Int = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.junit.experimental.categories.Category
/**
*
*/
@Category(JacksonMappingCategory::class, NativeMappingCategory::class)
@Category(JacksonMappingCategory::class, NativeMappingCategory::class, SerializationMappingCategory::class)
open class AllCategoriesKMongoBaseTest<T : Any> : KMongoBaseTest<T>() {

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.litote.kmongo

import kotlinx.serialization.ContextualSerialization
import kotlinx.serialization.Serializable
import org.bson.Document
import org.bson.types.Decimal128
import org.junit.Test
Expand All @@ -30,8 +32,8 @@ import kotlin.test.assertTrue
*/
class BigDecimalTest : AllCategoriesKMongoBaseTest<Article>() {

data class Article(val title: String, val big: BigDecimal) {
}
@Serializable
data class Article(val title: String, @ContextualSerialization val big: BigDecimal)

@Test
fun bigDecimalShouldBePersistedAsDecimal128() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.litote.kmongo

import kotlinx.serialization.ContextualSerialization
import kotlinx.serialization.Serializable
import org.bson.types.Binary
import org.junit.Assert.assertArrayEquals
import org.junit.Before
Expand All @@ -28,7 +30,11 @@ import kotlin.test.assertNull

class BinaryTest : AllCategoriesKMongoBaseTest<BinaryFriend>() {

data class BinaryFriend(val _id: Binary, var name: String = "none")
@Serializable
data class BinaryFriend(
@ContextualSerialization
val _id: Binary,
var name: String = "none")

lateinit var friendId: Binary

Expand Down
15 changes: 12 additions & 3 deletions kmongo-core-tests/src/main/kotlin/org/litote/kmongo/BsonIdTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.litote.kmongo

import kotlinx.serialization.ContextualSerialization
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import org.bson.Document
import org.bson.codecs.pojo.annotations.BsonId
import org.bson.types.ObjectId
Expand All @@ -32,16 +35,22 @@ import kotlin.test.assertTrue
*/
class BsonIdTest : AllCategoriesKMongoBaseTest<Friend>() {

@Serializable
class StringId(val _id: String? = null)

class WithMongoId(@BsonId val key: ObjectId? = null)
@Serializable
class WithMongoId(@SerialName("_id") @BsonId @ContextualSerialization val key: ObjectId? = null)

class WithMongoStringId(@BsonId val key: String? = null)
@Serializable
class WithMongoStringId(@SerialName("_id") @BsonId val key: String? = null)

@Serializable
class CompositeId(val _id: Key?)

class CompositeKey(@BsonId val key: Key?)
@Serializable
class CompositeKey(@SerialName("_id") @BsonId val key: Key?)

@Serializable
data class Key(val category: String, val index: Int)

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.litote.kmongo

import kotlinx.serialization.ContextualSerialization
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import org.bson.codecs.pojo.annotations.BsonId
import org.bson.types.ObjectId
import org.junit.Test
Expand All @@ -27,16 +30,22 @@ import kotlin.test.assertEquals
*/
class BsonIdTypedTest : AllCategoriesKMongoBaseTest<Friend>() {

@Serializable
class StringId(val _id: String? = null)

class WithMongoId(@BsonId val key: ObjectId? = null)
@Serializable
class WithMongoId(@SerialName("_id") @ContextualSerialization @BsonId val key: ObjectId? = null)

class WithMongoStringId(@BsonId val key: String? = null)
@Serializable
class WithMongoStringId(@SerialName("_id") @BsonId val key: String? = null)

@Serializable
class CompositeId(val _id: Key?)

class CompositeKey(@BsonId val key: Key?)
@Serializable
class CompositeKey(@SerialName("_id") @BsonId val key: Key?)

@Serializable
data class Key(val category: String, val index: Int)

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ChangeStreamTest : AllCategoriesKMongoBaseTest<Friend>() {
col.watch().fullDocument(FullDocument.UPDATE_LOOKUP).listen {
println(it.fullDocument)
if(it.fullDocument != null) {
friends.add(it.fullDocument)
friends.add(it.fullDocument!!)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.litote.kmongo

import kotlinx.serialization.Serializable
import org.bson.Document
import org.junit.Test
import org.litote.kmongo.model.Friend
Expand All @@ -27,14 +28,17 @@ import kotlin.test.assertTrue
*/
class CommandTest : AllCategoriesKMongoBaseTest<Friend>() {

@Serializable
class LocationResult(val results: List<Location>)

@Serializable
class Location(var dis: Double = 0.toDouble(), var obj: NestedLocation? = null) {

val name: String
get() = obj?.name ?: ""
}

@Serializable
class NestedLocation(var name: String? = null)

@Test
Expand Down
12 changes: 12 additions & 0 deletions kmongo-core-tests/src/main/kotlin/org/litote/kmongo/DateTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.litote.kmongo

import kotlinx.serialization.ContextualSerialization
import kotlinx.serialization.Serializable
import org.bson.Document
import org.junit.Test
import org.litote.kmongo.DateTest.DateValue
Expand All @@ -42,15 +44,25 @@ import kotlin.test.assertTrue
*/
class DateTest : AllCategoriesKMongoBaseTest<DateValue>() {

@Serializable
data class DateValue(
@ContextualSerialization
val date: Date?,
@ContextualSerialization
val calendar: Calendar?,
@ContextualSerialization
val localDateTime: LocalDateTime?,
@ContextualSerialization
val localDate: LocalDate?,
@ContextualSerialization
val localTime: LocalTime?,
@ContextualSerialization
var zonedDateTime: ZonedDateTime?,
@ContextualSerialization
var offsetDateTime: OffsetDateTime?,
@ContextualSerialization
var offsetTime: OffsetTime?,
@ContextualSerialization
val instant: Instant?
) {

Expand Down
Loading

0 comments on commit e06b3dd

Please sign in to comment.