From 53e32850989daa614fe6b72f206fa4161903e286 Mon Sep 17 00:00:00 2001 From: Subhashni Balakrishnan Date: Thu, 1 Mar 2018 14:10:08 -0800 Subject: [PATCH] DATACOUCH-341 - Add kotlin extension functions Motivation ---------- Add kotin extension methods to couchbase and reactive couchbase operations as required. Changes ------- Extensions leveraging reified type has been added. Results ------- It is now possible to use reified types rather than passing the java class reference to some operation functions. Original pull request: #160. --- pom.xml | 14 ++++ .../core/CouchbaseOperationsExtensionsTest.kt | 32 +++++++++ ...RxJavaCouchbaseOperationsExtensionsTest.kt | 35 ++++++++++ .../core/CouchbaseOperationsExtensions.kt | 67 ++++++++++++++++++ .../RxJavaCouchbaseOperationsExtensions.kt | 68 +++++++++++++++++++ 5 files changed, 216 insertions(+) create mode 100644 src/integration/kotlin/org/springframework/data/couchbase/core/CouchbaseOperationsExtensionsTest.kt create mode 100644 src/integration/kotlin/org/springframework/data/couchbase/core/RxJavaCouchbaseOperationsExtensionsTest.kt create mode 100644 src/main/kotlin/org/springframework/data/couchbase/core/CouchbaseOperationsExtensions.kt create mode 100644 src/main/kotlin/org/springframework/data/couchbase/core/RxJavaCouchbaseOperationsExtensions.kt diff --git a/pom.xml b/pom.xml index cdabe02b..ca31247d 100644 --- a/pom.xml +++ b/pom.xml @@ -165,6 +165,20 @@ test + + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin} + true + + + org.jetbrains.kotlin + kotlin-test + ${kotlin} + test + + diff --git a/src/integration/kotlin/org/springframework/data/couchbase/core/CouchbaseOperationsExtensionsTest.kt b/src/integration/kotlin/org/springframework/data/couchbase/core/CouchbaseOperationsExtensionsTest.kt new file mode 100644 index 00000000..efc6fd56 --- /dev/null +++ b/src/integration/kotlin/org/springframework/data/couchbase/core/CouchbaseOperationsExtensionsTest.kt @@ -0,0 +1,32 @@ +package org.springframework.data.couchbase.core + +import org.junit.Test +import org.junit.runner.RunWith +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.data.annotation.Id +import org.springframework.data.couchbase.ContainerResourceRunner +import org.springframework.data.couchbase.IntegrationTestApplicationConfig +import org.springframework.test.context.ContextConfiguration +import kotlin.test.assertEquals + +@RunWith(ContainerResourceRunner::class) +@ContextConfiguration(classes = [IntegrationTestApplicationConfig::class]) +class CouchbaseOperationsExtensionsTest { + + @Autowired + lateinit var template: CouchbaseTemplate + + @Test + fun `findById should call the reified extension`() { + val entityId = "CouchbaseOperationsExtensionsTestFindById" + val entity = Entity(entityId) + template.save(entity) + val stored = template.findById(entityId) + assertEquals(entity.id, stored.id) + } + + class Entity constructor(id: String) { + @Id + var id = id + } +} \ No newline at end of file diff --git a/src/integration/kotlin/org/springframework/data/couchbase/core/RxJavaCouchbaseOperationsExtensionsTest.kt b/src/integration/kotlin/org/springframework/data/couchbase/core/RxJavaCouchbaseOperationsExtensionsTest.kt new file mode 100644 index 00000000..cf72563f --- /dev/null +++ b/src/integration/kotlin/org/springframework/data/couchbase/core/RxJavaCouchbaseOperationsExtensionsTest.kt @@ -0,0 +1,35 @@ +package org.springframework.data.couchbase.core + +import org.junit.Test +import org.junit.runner.RunWith +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.data.annotation.Id +import org.springframework.data.couchbase.ContainerResourceRunner +import org.springframework.data.couchbase.ReactiveIntegrationTestApplicationConfig +import org.springframework.data.couchbase.core.query.N1qlSecondaryIndexed +import org.springframework.test.context.ContextConfiguration +import kotlin.test.assertEquals + + +@RunWith(ContainerResourceRunner::class) +@ContextConfiguration(classes = [ReactiveIntegrationTestApplicationConfig::class]) +class RxJavaCouchbaseOperationsExtensionsTest { + + @Autowired + lateinit var template: RxJavaCouchbaseTemplate + + @Test + fun `findById should call the reified extension`() { + val entityId = "RxJavaCouchbaseOperationsExtensionsTestFindById" + val entity = Entity(entityId) + template.save(entity).toBlocking().single() + val stored = template.findById(entityId).toBlocking().single() + assertEquals(entity.id, stored.id) + } + + class Entity constructor(id: String) { + @Id + var id = id + } +} + diff --git a/src/main/kotlin/org/springframework/data/couchbase/core/CouchbaseOperationsExtensions.kt b/src/main/kotlin/org/springframework/data/couchbase/core/CouchbaseOperationsExtensions.kt new file mode 100644 index 00000000..47868be2 --- /dev/null +++ b/src/main/kotlin/org/springframework/data/couchbase/core/CouchbaseOperationsExtensions.kt @@ -0,0 +1,67 @@ +/* + * Copyright 2018 the original author or authors + * + * 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.springframework.data.couchbase.core + +import com.couchbase.client.java.query.N1qlQuery +import com.couchbase.client.java.view.SpatialViewQuery +import com.couchbase.client.java.view.ViewQuery + +/** + * Kotlin extensions for [CouchbaseOperations] + * + * @author Subhashni Balakrishnan + */ + +/** + * Extension for [CouchbaseOperations.findById] leveraging reified type + * @param id the identifier of the document. + * @return the document fetched from couchbase. + */ +inline fun CouchbaseOperations.findById(id: String) : T = + findById(id, T::class.java) + +/** + * Extension for [CouchbaseOperations.findByView] leveraging reified type. + * @param query a [ViewQuery] instance that defines the query. + * @return list of entities satisfying the view query. + */ +inline fun CouchbaseOperations.findByView(query: ViewQuery) : List = + findByView(query, T::class.java) + +/** + * Extension for [CouchbaseOperations.findBySpatialView] leveraging reified type. + * @param query a [SpatialViewQuery] instance that defines the query. + * @return list of entities satisfying the spatial view query. + */ +inline fun CouchbaseOperations.findBySpatialView(query: SpatialViewQuery) : List = + findBySpatialView(query, T::class.java) + +/** + * Extension for [CouchbaseOperations.findByN1QL] leveraging reified type. + * @param query a [N1qlQuery] instance that defines the query. + * @return list of entities satisfying the N1ql query. + */ +inline fun CouchbaseOperations.findByN1QL(query: N1qlQuery) : List = + findByN1QL(query, T::class.java) + +/** + * Extension for [CouchbaseOperations.findByN1QLProjection] leveraging reified type. + * @param query a [N1qlQuery] instance that defines the query. + * @return list of entities satisfying the N1ql query projection. + */ +inline fun CouchbaseOperations.findByN1QLProjection(query: N1qlQuery) : List = + findByN1QLProjection(query, T::class.java) \ No newline at end of file diff --git a/src/main/kotlin/org/springframework/data/couchbase/core/RxJavaCouchbaseOperationsExtensions.kt b/src/main/kotlin/org/springframework/data/couchbase/core/RxJavaCouchbaseOperationsExtensions.kt new file mode 100644 index 00000000..0acb2959 --- /dev/null +++ b/src/main/kotlin/org/springframework/data/couchbase/core/RxJavaCouchbaseOperationsExtensions.kt @@ -0,0 +1,68 @@ +/* + * Copyright 2018 the original author or authors + * + * 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.springframework.data.couchbase.core + +import com.couchbase.client.java.query.N1qlQuery +import com.couchbase.client.java.view.SpatialViewQuery +import com.couchbase.client.java.view.ViewQuery +import rx.Observable + +/** + * Kotlin extensions for [RxJavaCouchbaseOperations] + * + * @author Subhashni Balakrishnan + */ + +/** + * Extension for [RxJavaCouchbaseOperations.findById] leveraging reified type + * @param id the identifier of the document. + * @return the document fetched from couchbase. + */ +inline fun RxJavaCouchbaseOperations.findById(id: String) : Observable = + findById(id, T::class.java) + +/** + * Extension for [RxJavaCouchbaseOperations.findByView] leveraging reified type. + * @param query a [ViewQuery] instance that defines the query. + * @return list of entities satisfying the view query. + */ +inline fun RxJavaCouchbaseOperations.findByView(query: ViewQuery) : Observable = + findByView(query, T::class.java) + +/** + * Extension for [RxJavaCouchbaseOperations.findBySpatialView] leveraging reified type. + * @param query a [SpatialViewQuery] instance that defines the query. + * @return list of entities satisfying the spatial view query. + */ +inline fun RxJavaCouchbaseOperations.findBySpatialView(query: SpatialViewQuery) : Observable = + findBySpatialView(query, T::class.java) + +/** + * Extension for [RxJavaCouchbaseOperations.findByN1QL] leveraging reified type. + * @param query a [N1qlQuery] instance that defines the query. + * @return list of entities satisfying the n1ql query. + */ +inline fun RxJavaCouchbaseOperations.findByN1QL(query: N1qlQuery) : Observable = + findByN1QL(query, T::class.java) + +/** + * Extension for [RxJavaCouchbaseOperations.findByN1QLProjection] leveraging reified type. + * @param query a [N1qlQuery] instance that defines the query. + * @return list of entities satisfying the n1ql query projection. + */ +inline fun RxJavaCouchbaseOperations.findByN1QLProjection(query: N1qlQuery) : Observable = + findByN1QLProjection(query, T::class.java) \ No newline at end of file