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.
This commit is contained in:
Subhashni Balakrishnan
2018-03-01 14:10:08 -08:00
parent a46f8160a9
commit 53e3285098
5 changed files with 216 additions and 0 deletions

14
pom.xml
View File

@@ -165,6 +165,20 @@
<scope>test</scope>
</dependency>
<!-- Kotlin extension -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin}</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>

View File

@@ -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<Entity>(entityId)
assertEquals(entity.id, stored.id)
}
class Entity constructor(id: String) {
@Id
var id = id
}
}

View File

@@ -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<Entity>(entityId).toBlocking().single()
assertEquals(entity.id, stored.id)
}
class Entity constructor(id: String) {
@Id
var id = id
}
}

View File

@@ -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 <reified T: Any> 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 <reified T: Any> CouchbaseOperations.findByView(query: ViewQuery) : List<T> =
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 <reified T: Any> CouchbaseOperations.findBySpatialView(query: SpatialViewQuery) : List<T> =
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 <reified T: Any> CouchbaseOperations.findByN1QL(query: N1qlQuery) : List<T> =
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 <reified T: Any> CouchbaseOperations.findByN1QLProjection(query: N1qlQuery) : List<T> =
findByN1QLProjection(query, T::class.java)

View File

@@ -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 <reified T: Any> RxJavaCouchbaseOperations.findById(id: String) : Observable<T> =
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 <reified T: Any> RxJavaCouchbaseOperations.findByView(query: ViewQuery) : Observable<T> =
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 <reified T: Any> RxJavaCouchbaseOperations.findBySpatialView(query: SpatialViewQuery) : Observable<T> =
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 <reified T: Any> RxJavaCouchbaseOperations.findByN1QL(query: N1qlQuery) : Observable<T> =
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 <reified T: Any> RxJavaCouchbaseOperations.findByN1QLProjection(query: N1qlQuery) : Observable<T> =
findByN1QLProjection(query, T::class.java)