Skip to content

Commit

Permalink
Use KClass instead of Class
Browse files Browse the repository at this point in the history
  • Loading branch information
fluidsonic committed Oct 20, 2019
1 parent 39c1cc4 commit b97aea0
Show file tree
Hide file tree
Showing 16 changed files with 158 additions and 150 deletions.
13 changes: 7 additions & 6 deletions sources/MongoClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import kotlinx.coroutines.flow.*
import org.bson.*
import org.bson.conversions.*
import java.io.*
import kotlin.reflect.*

/**
* A client-side representation of a MongoDB cluster. Instances can represent either a standalone MongoDB instance, a replica set,
Expand Down Expand Up @@ -95,7 +96,7 @@ interface MongoClient : Closeable {
* @param <TResult> the type of the class to use instead of `Document`.
* @return the list databases iterable interface
*/
fun <TResult : Any> listDatabases(resultClass: Class<TResult>): ListDatabasesFlow<TResult>
fun <TResult : Any> listDatabases(resultClass: KClass<out TResult>): ListDatabasesFlow<TResult>

/**
* Gets the list of databases
Expand All @@ -108,7 +109,7 @@ interface MongoClient : Closeable {
* @since 3.6
* @mongodb.server.release 3.6
*/
fun <TResult : Any> listDatabases(clientSession: ClientSession, resultClass: Class<TResult>): ListDatabasesFlow<TResult>
fun <TResult : Any> listDatabases(clientSession: ClientSession, resultClass: KClass<out TResult>): ListDatabasesFlow<TResult>

/**
* Creates a change stream for this client.
Expand All @@ -130,7 +131,7 @@ interface MongoClient : Closeable {
* @since 3.8
* @mongodb.server.release 4.0
*/
fun <TResult : Any> watch(resultClass: Class<TResult>): ChangeStreamFlow<TResult>
fun <TResult : Any> watch(resultClass: KClass<out TResult>): ChangeStreamFlow<TResult>

/**
* Creates a change stream for this client.
Expand All @@ -154,7 +155,7 @@ interface MongoClient : Closeable {
* @since 3.8
* @mongodb.server.release 4.0
*/
fun <TResult : Any> watch(pipeline: List<Bson>, resultClass: Class<TResult>): ChangeStreamFlow<TResult>
fun <TResult : Any> watch(pipeline: List<Bson>, resultClass: KClass<out TResult>): ChangeStreamFlow<TResult>

/**
* Creates a change stream for this client.
Expand All @@ -178,7 +179,7 @@ interface MongoClient : Closeable {
* @mongodb.server.release 4.0
* @mongodb.driver.dochub core/changestreams Change Streams
*/
fun <TResult : Any> watch(clientSession: ClientSession, resultClass: Class<TResult>): ChangeStreamFlow<TResult>
fun <TResult : Any> watch(clientSession: ClientSession, resultClass: KClass<out TResult>): ChangeStreamFlow<TResult>

/**
* Creates a change stream for this client.
Expand All @@ -204,7 +205,7 @@ interface MongoClient : Closeable {
* @mongodb.server.release 4.0
* @mongodb.driver.dochub core/changestreams Change Streams
*/
fun <TResult : Any> watch(clientSession: ClientSession, pipeline: List<Bson>, resultClass: Class<TResult>): ChangeStreamFlow<TResult>
fun <TResult : Any> watch(clientSession: ClientSession, pipeline: List<Bson>, resultClass: KClass<out TResult>): ChangeStreamFlow<TResult>

/**
* Creates a client session with default options.
Expand Down
41 changes: 21 additions & 20 deletions sources/MongoCollection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import kotlinx.coroutines.flow.*
import org.bson.*
import org.bson.codecs.configuration.*
import org.bson.conversions.*
import kotlin.reflect.*

/**
* The MongoCollection interface.
Expand All @@ -49,7 +50,7 @@ interface MongoCollection<TDocument : Any> {
*
* @return the class
*/
val documentClass: Class<TDocument>
val documentClass: KClass<TDocument>

/**
* Get the codec registry for the MongoCollection.
Expand Down Expand Up @@ -89,7 +90,7 @@ interface MongoCollection<TDocument : Any> {
* @param <NewTDocument> the type that the new collection will encode documents from and decode documents to
* @return a new MongoCollection instance with the different default class
*/
fun <NewTDocument : Any> withDocumentClass(newDocumentClass: Class<NewTDocument>): MongoCollection<NewTDocument>
fun <NewTDocument : Any> withDocumentClass(newDocumentClass: KClass<NewTDocument>): MongoCollection<NewTDocument>

/**
* Create a new MongoCollection instance with a different codec registry.
Expand Down Expand Up @@ -288,7 +289,7 @@ interface MongoCollection<TDocument : Any> {
* @return an iterable of distinct values
* @mongodb.driver.manual reference/command/distinct/ Distinct
*/
fun <TResult : Any> distinct(fieldName: String, resultClass: Class<TResult>): DistinctFlow<TResult>
fun <TResult : Any> distinct(fieldName: String, resultClass: KClass<out TResult>): DistinctFlow<TResult>

/**
* Gets the distinct values of the specified field name.
Expand All @@ -300,7 +301,7 @@ interface MongoCollection<TDocument : Any> {
* @return an iterable of distinct values
* @mongodb.driver.manual reference/command/distinct/ Distinct
*/
fun <TResult : Any> distinct(fieldName: String, filter: Bson, resultClass: Class<TResult>): DistinctFlow<TResult>
fun <TResult : Any> distinct(fieldName: String, filter: Bson, resultClass: KClass<out TResult>): DistinctFlow<TResult>

/**
* Gets the distinct values of the specified field name.
Expand All @@ -314,7 +315,7 @@ interface MongoCollection<TDocument : Any> {
* @since 3.6
* @mongodb.server.release 3.6
*/
fun <TResult : Any> distinct(clientSession: ClientSession, fieldName: String, resultClass: Class<TResult>): DistinctFlow<TResult>
fun <TResult : Any> distinct(clientSession: ClientSession, fieldName: String, resultClass: KClass<out TResult>): DistinctFlow<TResult>

/**
* Gets the distinct values of the specified field name.
Expand All @@ -329,7 +330,7 @@ interface MongoCollection<TDocument : Any> {
* @since 3.6
* @mongodb.server.release 3.6
*/
fun <TResult : Any> distinct(clientSession: ClientSession, fieldName: String, filter: Bson, resultClass: Class<TResult>): DistinctFlow<TResult>
fun <TResult : Any> distinct(clientSession: ClientSession, fieldName: String, filter: Bson, resultClass: KClass<out TResult>): DistinctFlow<TResult>

/**
* Finds all documents in the collection.
Expand All @@ -347,7 +348,7 @@ interface MongoCollection<TDocument : Any> {
* @return the find iterable interface
* @mongodb.driver.manual tutorial/query-documents/ Find
*/
fun <TResult : Any> find(resultClass: Class<TResult>): FindFlow<TResult>
fun <TResult : Any> find(resultClass: KClass<out TResult>): FindFlow<TResult>

/**
* Finds all documents in the collection.
Expand All @@ -367,7 +368,7 @@ interface MongoCollection<TDocument : Any> {
* @return the find iterable interface
* @mongodb.driver.manual tutorial/query-documents/ Find
*/
fun <TResult : Any> find(filter: Bson, resultClass: Class<TResult>): FindFlow<TResult>
fun <TResult : Any> find(filter: Bson, resultClass: KClass<out TResult>): FindFlow<TResult>

/**
* Finds all documents in the collection.
Expand All @@ -391,7 +392,7 @@ interface MongoCollection<TDocument : Any> {
* @since 3.6
* @mongodb.server.release 3.6
*/
fun <TResult : Any> find(clientSession: ClientSession, resultClass: Class<TResult>): FindFlow<TResult>
fun <TResult : Any> find(clientSession: ClientSession, resultClass: KClass<out TResult>): FindFlow<TResult>

/**
* Finds all documents in the collection.
Expand All @@ -417,7 +418,7 @@ interface MongoCollection<TDocument : Any> {
* @since 3.6
* @mongodb.server.release 3.6
*/
fun <TResult : Any> find(clientSession: ClientSession, filter: Bson, resultClass: Class<TResult>): FindFlow<TResult>
fun <TResult : Any> find(clientSession: ClientSession, filter: Bson, resultClass: KClass<out TResult>): FindFlow<TResult>

/**
* Aggregates documents according to the specified aggregation pipeline. If the pipeline ends with a $out stage, the returned
Expand All @@ -441,7 +442,7 @@ interface MongoCollection<TDocument : Any> {
* @return an iterable containing the result of the aggregation operation
* @mongodb.driver.manual aggregation/ Aggregation
*/
fun <TResult : Any> aggregate(pipeline: List<Bson>, resultClass: Class<TResult>): AggregateFlow<TResult>
fun <TResult : Any> aggregate(pipeline: List<Bson>, resultClass: KClass<out TResult>): AggregateFlow<TResult>

/**
* Aggregates documents according to the specified aggregation pipeline. If the pipeline ends with a $out stage, the returned
Expand Down Expand Up @@ -471,7 +472,7 @@ interface MongoCollection<TDocument : Any> {
* @since 3.6
* @mongodb.server.release 3.6
*/
fun <TResult : Any> aggregate(clientSession: ClientSession, pipeline: List<Bson>, resultClass: Class<TResult>): AggregateFlow<TResult>
fun <TResult : Any> aggregate(clientSession: ClientSession, pipeline: List<Bson>, resultClass: KClass<out TResult>): AggregateFlow<TResult>

/**
* Creates a change stream for this collection.
Expand All @@ -493,7 +494,7 @@ interface MongoCollection<TDocument : Any> {
* @since 3.6
* @mongodb.server.release 3.6
*/
fun <TResult : Any> watch(resultClass: Class<TResult>): ChangeStreamFlow<TResult>
fun <TResult : Any> watch(resultClass: KClass<out TResult>): ChangeStreamFlow<TResult>

/**
* Creates a change stream for this collection.
Expand All @@ -517,7 +518,7 @@ interface MongoCollection<TDocument : Any> {
* @since 3.6
* @mongodb.server.release 3.6
*/
fun <TResult : Any> watch(pipeline: List<Bson>, resultClass: Class<TResult>): ChangeStreamFlow<TResult>
fun <TResult : Any> watch(pipeline: List<Bson>, resultClass: KClass<out TResult>): ChangeStreamFlow<TResult>

/**
* Creates a change stream for this collection.
Expand All @@ -541,7 +542,7 @@ interface MongoCollection<TDocument : Any> {
* @since 3.6
* @mongodb.server.release 3.6
*/
fun <TResult : Any> watch(clientSession: ClientSession, resultClass: Class<TResult>): ChangeStreamFlow<TResult>
fun <TResult : Any> watch(clientSession: ClientSession, resultClass: KClass<out TResult>): ChangeStreamFlow<TResult>

/**
* Creates a change stream for this collection.
Expand All @@ -567,7 +568,7 @@ interface MongoCollection<TDocument : Any> {
* @since 3.6
* @mongodb.server.release 3.6
*/
fun <TResult : Any> watch(clientSession: ClientSession, pipeline: List<Bson>, resultClass: Class<TResult>): ChangeStreamFlow<TResult>
fun <TResult : Any> watch(clientSession: ClientSession, pipeline: List<Bson>, resultClass: KClass<out TResult>): ChangeStreamFlow<TResult>

/**
* Aggregates documents according to the specified map-reduce function.
Expand All @@ -589,7 +590,7 @@ interface MongoCollection<TDocument : Any> {
* @return an iterable containing the result of the map-reduce operation
* @mongodb.driver.manual reference/command/mapReduce/ map-reduce
*/
fun <TResult : Any> mapReduce(mapFunction: String, reduceFunction: String, resultClass: Class<TResult>): MapReduceFlow<TResult>
fun <TResult : Any> mapReduce(mapFunction: String, reduceFunction: String, resultClass: KClass<out TResult>): MapReduceFlow<TResult>

/**
* Aggregates documents according to the specified map-reduce function.
Expand Down Expand Up @@ -618,7 +619,7 @@ interface MongoCollection<TDocument : Any> {
* @mongodb.server.release 3.6
*/
fun <TResult : Any> mapReduce(clientSession: ClientSession, mapFunction: String, reduceFunction: String,
resultClass: Class<TResult>): MapReduceFlow<TResult>
resultClass: KClass<out TResult>): MapReduceFlow<TResult>

/**
* Executes a mix of inserts, updates, replaces, and deletes.
Expand Down Expand Up @@ -1335,7 +1336,7 @@ interface MongoCollection<TDocument : Any> {
* @return the list indexes iterable interface
* @mongodb.driver.manual reference/command/listIndexes/ List indexes
*/
fun <TResult : Any> listIndexes(resultClass: Class<TResult>): ListIndexesFlow<TResult>
fun <TResult : Any> listIndexes(resultClass: KClass<out TResult>): ListIndexesFlow<TResult>

/**
* Get all the indexes in this collection.
Expand All @@ -1359,7 +1360,7 @@ interface MongoCollection<TDocument : Any> {
* @since 3.6
* @mongodb.server.release 3.6
*/
fun <TResult : Any> listIndexes(clientSession: ClientSession, resultClass: Class<TResult>): ListIndexesFlow<TResult>
fun <TResult : Any> listIndexes(clientSession: ClientSession, resultClass: KClass<out TResult>): ListIndexesFlow<TResult>

/**
* Drops the index given its name.
Expand Down
27 changes: 14 additions & 13 deletions sources/MongoDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import kotlinx.coroutines.flow.*
import org.bson.*
import org.bson.codecs.configuration.*
import org.bson.conversions.*
import kotlin.reflect.*

/**
* The MongoDatabase interface.
Expand Down Expand Up @@ -125,7 +126,7 @@ interface MongoDatabase {
* @param <TDocument> the type of the class to use instead of `Document`.
* @return the collection
*/
fun <TDocument : Any> getCollection(collectionName: String, documentClass: Class<TDocument>): MongoCollection<TDocument>
fun <TDocument : Any> getCollection(collectionName: String, documentClass: KClass<TDocument>): MongoCollection<TDocument>

/**
* Executes the given command in the context of the current database with a read preference of [ReadPreference.primary].
Expand All @@ -149,7 +150,7 @@ interface MongoDatabase {
* @param resultClass the default class to cast any documents returned from the database into.
* @param <TResult> the type of the class to use instead of `Document`.
*/
suspend fun <TResult : Any> runCommand(command: Bson, resultClass: Class<TResult>): TResult
suspend fun <TResult : Any> runCommand(command: Bson, resultClass: KClass<out TResult>): TResult

/**
* Executes the given command in the context of the current database with the given read preference.
Expand All @@ -159,7 +160,7 @@ interface MongoDatabase {
* @param resultClass the default class to cast any documents returned from the database into.
* @param <TResult> the type of the class to use instead of `Document`.
*/
suspend fun <TResult : Any> runCommand(command: Bson, readPreference: ReadPreference, resultClass: Class<TResult>): TResult
suspend fun <TResult : Any> runCommand(command: Bson, readPreference: ReadPreference, resultClass: KClass<out TResult>): TResult

/**
* Executes the given command in the context of the current database with a read preference of [ReadPreference.primary].
Expand Down Expand Up @@ -192,7 +193,7 @@ interface MongoDatabase {
* @since 3.6
* @mongodb.server.release 3.6
*/
suspend fun <TResult : Any> runCommand(clientSession: ClientSession, command: Bson, resultClass: Class<TResult>): TResult
suspend fun <TResult : Any> runCommand(clientSession: ClientSession, command: Bson, resultClass: KClass<out TResult>): TResult

/**
* Executes the given command in the context of the current database with the given read preference.
Expand All @@ -205,7 +206,7 @@ interface MongoDatabase {
* @since 3.6
* @mongodb.server.release 3.6
*/
suspend fun <TResult : Any> runCommand(clientSession: ClientSession, command: Bson, readPreference: ReadPreference, resultClass: Class<TResult>): TResult
suspend fun <TResult : Any> runCommand(clientSession: ClientSession, command: Bson, readPreference: ReadPreference, resultClass: KClass<out TResult>): TResult

/**
* Drops this database.
Expand Down Expand Up @@ -257,7 +258,7 @@ interface MongoDatabase {
* @return the list collections iterable interface
* @mongodb.driver.manual reference/command/listCollections listCollections
*/
fun <TResult : Any> listCollections(resultClass: Class<TResult>): ListCollectionsFlow<TResult>
fun <TResult : Any> listCollections(resultClass: KClass<out TResult>): ListCollectionsFlow<TResult>

/**
* Finds all the collections in this database.
Expand All @@ -281,7 +282,7 @@ interface MongoDatabase {
* @since 3.6
* @mongodb.server.release 3.6
*/
fun <TResult : Any> listCollections(clientSession: ClientSession, resultClass: Class<TResult>): ListCollectionsFlow<TResult>
fun <TResult : Any> listCollections(clientSession: ClientSession, resultClass: KClass<out TResult>): ListCollectionsFlow<TResult>

/**
* Create a new collection with the given name.
Expand Down Expand Up @@ -396,7 +397,7 @@ interface MongoDatabase {
* @since 3.8
* @mongodb.server.release 4.0
*/
fun <TResult : Any> watch(resultClass: Class<TResult>): ChangeStreamFlow<TResult>
fun <TResult : Any> watch(resultClass: KClass<out TResult>): ChangeStreamFlow<TResult>

/**
* Creates a change stream for this database.
Expand All @@ -420,7 +421,7 @@ interface MongoDatabase {
* @since 3.8
* @mongodb.server.release 4.0
*/
fun <TResult : Any> watch(pipeline: List<Bson>, resultClass: Class<TResult>): ChangeStreamFlow<TResult>
fun <TResult : Any> watch(pipeline: List<Bson>, resultClass: KClass<out TResult>): ChangeStreamFlow<TResult>

/**
* Creates a change stream for this database.
Expand All @@ -444,7 +445,7 @@ interface MongoDatabase {
* @mongodb.server.release 4.0
* @mongodb.driver.dochub core/changestreams Change Streams
*/
fun <TResult : Any> watch(clientSession: ClientSession, resultClass: Class<TResult>): ChangeStreamFlow<TResult>
fun <TResult : Any> watch(clientSession: ClientSession, resultClass: KClass<out TResult>): ChangeStreamFlow<TResult>

/**
* Creates a change stream for this database.
Expand All @@ -470,7 +471,7 @@ interface MongoDatabase {
* @mongodb.server.release 4.0
* @mongodb.driver.dochub core/changestreams Change Streams
*/
fun <TResult : Any> watch(clientSession: ClientSession, pipeline: List<Bson>, resultClass: Class<TResult>): ChangeStreamFlow<TResult>
fun <TResult : Any> watch(clientSession: ClientSession, pipeline: List<Bson>, resultClass: KClass<out TResult>): ChangeStreamFlow<TResult>

/**
* Runs an aggregation framework pipeline on the database for pipeline stages
Expand All @@ -496,7 +497,7 @@ interface MongoDatabase {
* @mongodb.driver.manual reference/command/aggregate/#dbcmd.aggregate Aggregate Command
* @mongodb.server.release 3.6
</TResult> */
fun <TResult : Any> aggregate(pipeline: List<Bson>, resultClass: Class<TResult>): AggregateFlow<TResult>
fun <TResult : Any> aggregate(pipeline: List<Bson>, resultClass: KClass<out TResult>): AggregateFlow<TResult>

/**
* Runs an aggregation framework pipeline on the database for pipeline stages
Expand Down Expand Up @@ -524,5 +525,5 @@ interface MongoDatabase {
* @mongodb.driver.manual reference/command/aggregate/#dbcmd.aggregate Aggregate Command
* @mongodb.server.release 3.6
</TResult> */
fun <TResult : Any> aggregate(clientSession: ClientSession, pipeline: List<Bson>, resultClass: Class<TResult>): AggregateFlow<TResult>
fun <TResult : Any> aggregate(clientSession: ClientSession, pipeline: List<Bson>, resultClass: KClass<out TResult>): AggregateFlow<TResult>
}
Loading

0 comments on commit b97aea0

Please sign in to comment.