Reorg complete, jvm tests pass
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
package solutions.bitbadger.documents.kotlin
|
||||
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
object Configuration {
|
||||
|
||||
/**
|
||||
* JSON serializer; replace to configure with non-default options
|
||||
*
|
||||
* The default sets `encodeDefaults` to `true` and `explicitNulls` to `false`; see
|
||||
* https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/json.md for all configuration options
|
||||
*/
|
||||
@JvmField
|
||||
var json = Json {
|
||||
encodeDefaults = true
|
||||
explicitNulls = false
|
||||
coerceInputValues = true
|
||||
}
|
||||
|
||||
/** The JSON serializer to use for documents */
|
||||
@JvmStatic
|
||||
var serializer: DocumentSerializer = DocumentSerializerKotlin()
|
||||
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import solutions.bitbadger.documents.Field
|
||||
import solutions.bitbadger.documents.FieldMatch
|
||||
import solutions.bitbadger.documents.Parameter
|
||||
import solutions.bitbadger.documents.ParameterType
|
||||
import solutions.bitbadger.documents.query.Count
|
||||
|
||||
import java.sql.Connection
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package solutions.bitbadger.documents.kotlin
|
||||
|
||||
import solutions.bitbadger.documents.*
|
||||
import solutions.bitbadger.documents.Configuration
|
||||
import solutions.bitbadger.documents.Parameter
|
||||
import solutions.bitbadger.documents.jvm.Parameters
|
||||
import solutions.bitbadger.documents.jvm.Custom as JvmCustom
|
||||
import java.sql.Connection
|
||||
import java.sql.ResultSet
|
||||
|
||||
@@ -15,17 +17,16 @@ object Custom {
|
||||
*
|
||||
* @param query The query to retrieve the results
|
||||
* @param parameters Parameters to use for the query
|
||||
* @param clazz The class of the document to be returned
|
||||
* @param conn The connection over which the query should be executed
|
||||
* @param mapFunc The mapping function between the document and the domain item
|
||||
* @return A list of results for the given query
|
||||
*/
|
||||
inline fun <reified TDoc> list(
|
||||
inline fun <reified TDoc : Any> list(
|
||||
query: String,
|
||||
parameters: Collection<Parameter<*>> = listOf(),
|
||||
conn: Connection,
|
||||
noinline mapFunc: (ResultSet, Class<TDoc>) -> TDoc
|
||||
) = Custom.list(query, parameters, TDoc::class.java, conn, mapFunc)
|
||||
mapFunc: (ResultSet) -> TDoc
|
||||
) = Parameters.apply(conn, query, parameters).use { Results.toCustomList(it, mapFunc) }
|
||||
|
||||
/**
|
||||
* Execute a query that returns a list of results (creates connection)
|
||||
@@ -35,10 +36,10 @@ object Custom {
|
||||
* @param mapFunc The mapping function between the document and the domain item
|
||||
* @return A list of results for the given query
|
||||
*/
|
||||
inline fun <reified TDoc> list(
|
||||
inline fun <reified TDoc : Any> list(
|
||||
query: String,
|
||||
parameters: Collection<Parameter<*>> = listOf(),
|
||||
noinline mapFunc: (ResultSet, Class<TDoc>) -> TDoc
|
||||
mapFunc: (ResultSet) -> TDoc
|
||||
) = Configuration.dbConn().use { list(query, parameters, it, mapFunc) }
|
||||
|
||||
/**
|
||||
@@ -50,12 +51,12 @@ object Custom {
|
||||
* @param mapFunc The mapping function between the document and the domain item
|
||||
* @return The document if one matches the query, `null` otherwise
|
||||
*/
|
||||
inline fun <reified TDoc> single(
|
||||
inline fun <reified TDoc : Any> single(
|
||||
query: String,
|
||||
parameters: Collection<Parameter<*>> = listOf(),
|
||||
conn: Connection,
|
||||
noinline mapFunc: (ResultSet, Class<TDoc>) -> TDoc
|
||||
) = Custom.single(query, parameters, TDoc::class.java, conn, mapFunc)
|
||||
mapFunc: (ResultSet) -> TDoc
|
||||
) = list("$query LIMIT 1", parameters, conn, mapFunc).singleOrNull()
|
||||
|
||||
/**
|
||||
* Execute a query that returns one or no results
|
||||
@@ -65,10 +66,10 @@ object Custom {
|
||||
* @param mapFunc The mapping function between the document and the domain item
|
||||
* @return The document if one matches the query, `null` otherwise
|
||||
*/
|
||||
inline fun <reified TDoc> single(
|
||||
inline fun <reified TDoc : Any> single(
|
||||
query: String,
|
||||
parameters: Collection<Parameter<*>> = listOf(),
|
||||
noinline mapFunc: (ResultSet, Class<TDoc>) -> TDoc
|
||||
noinline mapFunc: (ResultSet) -> TDoc
|
||||
) = Configuration.dbConn().use { single(query, parameters, it, mapFunc) }
|
||||
|
||||
/**
|
||||
@@ -79,7 +80,7 @@ object Custom {
|
||||
* @param parameters Parameters to use for the query
|
||||
*/
|
||||
fun nonQuery(query: String, parameters: Collection<Parameter<*>> = listOf(), conn: Connection) =
|
||||
Custom.nonQuery(query, parameters, conn)
|
||||
JvmCustom.nonQuery(query, parameters, conn)
|
||||
|
||||
/**
|
||||
* Execute a query that returns no results
|
||||
@@ -103,8 +104,13 @@ object Custom {
|
||||
query: String,
|
||||
parameters: Collection<Parameter<*>> = listOf(),
|
||||
conn: Connection,
|
||||
noinline mapFunc: (ResultSet, Class<T>) -> T
|
||||
) = Custom.scalar(query, parameters, T::class.java, conn, mapFunc)
|
||||
mapFunc: (ResultSet) -> T
|
||||
) = Parameters.apply(conn, query, parameters).use { stmt ->
|
||||
stmt.executeQuery().use { rs ->
|
||||
rs.next()
|
||||
mapFunc(rs)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a query that returns a scalar result
|
||||
@@ -115,6 +121,6 @@ object Custom {
|
||||
* @return The scalar value from the query
|
||||
*/
|
||||
inline fun <reified T : Any> scalar(
|
||||
query: String, parameters: Collection<Parameter<*>> = listOf(), noinline mapFunc: (ResultSet, Class<T>) -> T
|
||||
query: String, parameters: Collection<Parameter<*>> = listOf(), mapFunc: (ResultSet) -> T
|
||||
) = Configuration.dbConn().use { scalar(query, parameters, it, mapFunc) }
|
||||
}
|
||||
|
||||
33
src/kotlin/src/main/kotlin/DocumentConfig.kt
Normal file
33
src/kotlin/src/main/kotlin/DocumentConfig.kt
Normal file
@@ -0,0 +1,33 @@
|
||||
package solutions.bitbadger.documents.kotlin
|
||||
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
/**
|
||||
* Configuration for document serialization
|
||||
*/
|
||||
object DocumentConfig {
|
||||
|
||||
val options = Json {
|
||||
coerceInputValues = true
|
||||
encodeDefaults = true
|
||||
explicitNulls = false
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize a document to JSON
|
||||
*
|
||||
* @param document The document to be serialized
|
||||
* @return The JSON string with the serialized document
|
||||
*/
|
||||
inline fun <reified TDoc> serialize(document: TDoc) =
|
||||
options.encodeToString(document)
|
||||
|
||||
/**
|
||||
* Deserialize a document from JSON
|
||||
*
|
||||
* @param json The JSON string with the serialized document
|
||||
* @return The document created from the given JSON
|
||||
*/
|
||||
inline fun <reified TDoc> deserialize(json: String) =
|
||||
options.decodeFromString<TDoc>(json)
|
||||
}
|
||||
21
src/kotlin/src/test/kotlin/DocumentConfigTest.kt
Normal file
21
src/kotlin/src/test/kotlin/DocumentConfigTest.kt
Normal file
@@ -0,0 +1,21 @@
|
||||
package solutions.bitbadger.documents.kotlin
|
||||
|
||||
import org.junit.jupiter.api.DisplayName
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
/**
|
||||
* Unit tests for the `Configuration` object
|
||||
*/
|
||||
@DisplayName("Kotlin | DocumentConfig")
|
||||
class DocumentConfigTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("Default JSON options are as expected")
|
||||
fun defaultJsonOptions() {
|
||||
assertTrue(DocumentConfig.options.configuration.encodeDefaults, "Encode Defaults should have been set")
|
||||
assertFalse(DocumentConfig.options.configuration.explicitNulls, "Explicit Nulls should not have been set")
|
||||
assertTrue(DocumentConfig.options.configuration.coerceInputValues, "Coerce Input Values should have been set")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user