How does nested covariance works in Kotlin? - kotlin

Here's a code in which I'm having hard time understanding why the first compiles and second doesn't?
class Test11<T : Number> {
lateinit var test: MutableList<out T>.() -> Unit
}
fun main() {
val test: Test11<Int> = Test11<Int>()
val test2: Test11<out Number> = test
test.test.invoke(MutableList(3) { 55 }) // First
test2.test.invoke(MutableList(3) { 55 }) // Second
}
The second says MutableList<Nothing> was expected.
So basically in first case, T => Int so out out T => out Int => out Number maybe. In second case, T => out Number so anything which is subclass of Number, then still out T => out Number right?
I'm not able to understand why doesn't it work by that logic...

The MutableList is a function parameter. You'd have the exact same issue with:
class Test11<T : Number> {
fun test(list: MutableList<out T>) {
}
}
fun main() {
val test: Test11<Number> = Test11<Number>()
val test2: Test11<out Number> = test
test.test(MutableList(3) { 55 }) // First
test2.test(MutableList(3) { 55 }) // Second
}
A covariant type by definition prevents functions where the type is a parameter from being called, but this also logically extends to nested covariance of the same type. If T is covariant (for the class), then it is not any more safe to consume an object that can produce Ts than to consume Ts directly.
Example of how this could create a failure:
class Test11<T : Number> {
var list: MutableList<out T>? = null
fun test(list: MutableList<out T>) {
this.list = list
}
}
fun main() {
val test: Test11<Long> = Test11()
val test2: Test11<out Number> = test
val doubleList: MutableList<out Number> = mutableListOf(1.0)
test2.test(doubleList) // Not allowed
// if it were allowed:
val long: Long? = test.list?.firstOrNull() // ClassCastException casting the Double to a Long
}

Related

Referencing a type or class through an enum value?

I'm new to Kotlin, thank you for your patience!
I'm working on the code below, to try and figure out if it's possible to associate the enum value defined, with the intended class and type for reference elsewhere, and to use that defined relationship to eliminate the "when" clause from 5 to 1.
The issue I run into is the line referencing types with angle brackets, is there a way or a way around it, using the enum value I've defined?
GenericCsvParser<SnowflakeReserveItem>()
Where I want to get to is
fun persistSnowflakeData(
inputStream: InputStream,
dataType: SnowflakeDataType,
){
val sfItems = GenericCsvParser<dataType.value>()
.parse(inputStream, datatype.value)
.toList()
snowflakeItemRepository.saveAll(sfItems)
}
The code right now:
fun persistSnowflakeData(
inputStream: InputStream,
dataType: SnowflakeDataType,
) {
when (dataType) {
SNOWFLAKE_RESERVE_BOOKING -> {
val sfItems = GenericCsvParser<SnowflakeReserveItem>()
.parse(inputStream, SnowflakeReserveItem::class.java)
.toList()
snowflakeItemRepository.saveAll(sfItems)
}
SNOWFLAKE_MEMBERSHIP_TERM -> {
val sfItems = GenericCsvParser<SnowflakeMembershipTerm>()
.parse(inputStream, SnowflakeMembershipTerm::class.java)
.toList()
snowflakeMembershipTermRepository.saveAll(sfItems)
}
SNOWFLAKE_MEMBERSHIP_BOOKING -> {
val sfItems = GenericCsvParser<SnowflakeMembershipOrderBooking>()
.parse(inputStream, SnowflakeMembershipOrderBooking::class.java)
.toList()
snowflakeMembershipItemRepository.saveAll(sfItems)
}
SNOWFLAKE_OFFLINE_SALE_BOOKING -> {
val sfItems = GenericCsvParser<SnowflakeOfflineItem>()
.parse(inputStream, SnowflakeOfflineItem::class.java)
.toList()
snowflakeOfflineItemRepository.saveAll(sfItems)
}
SNOWFLAKE_ONLINE_SALE_BOOKING -> {
val sfItems = GenericCsvParser<SnowflakeOnlineItem>()
.parse(inputStream, SnowflakeOnlineItem::class.java)
.toList()
snowflakeOnlineItemRepository.saveAll(sfItems)
}
}
}
enum class SnowflakeDataType(clazz: Class<*>) {
SNOWFLAKE_RESERVE_BOOKING(SnowflakeReserveItem::class.java),
SNOWFLAKE_MEMBERSHIP_TERM(SnowflakeMembershipTerm::class.java),
SNOWFLAKE_MEMBERSHIP_BOOKING(SnowflakeMembershipOrderBooking::class.java),
SNOWFLAKE_OFFLINE_SALE_BOOKING(SnowflakeOfflineItem::class.java),
SNOWFLAKE_ONLINE_SALE_BOOKING(SnowflakeOnlineItem::class.java)
}
I would suggest using Reified type parameters
interface ItemRepository<T> {
fun saveAll(item: T)
}
//...
inline fun <reified T> persistSnowflakeData(
inputStream: InputStream,
repository: ItemRepository<T>,
) {
val items = GenericCsvParser<T>()
.parse(inputStream, T::class.java)
.toList()
repository.saveAll(items)
}
snowflakeItemRepository, snowflakeMembershipTermRepository and others item repositories should implement ItemRepository.
Then you can call the function as follows:
//...
persistSnowflakeData(inputStream, snowflakeItemRepository)
//...
persistSnowflakeData(inputStream, snowflakeMembershipTermRepository)
//...

How can I check the constructur arguments of a mockk Mock?

I have the following code (in Kotlin):
class X {
fun foo() {
val A(1, true, "three")
val b = B()
b.bar(A)
}
}
What I want to to is find out what A has been instantiated with.
My test code looks like so:
// Needed for something else
every { anyConstructed<A>().go() } returns "testString"
// What I'm using to extract A
val barSlot = slot<A>()
verify { anyConstructed<B>().bar(capture(barSlot)) }
val a = barSlot.captured
How can I check what values A has been instantiated with now I've managed to capture the mock that was created when it was constructed (thanks to the every statement)?
Thanks!
You can do it in two ways:
Using slot to capture the parameter:
#Test
fun shouldCheckValuesAtConstruct() {
val a = A(1, true, "s")
val b = mockk<B>()
val aSlot = slot<A>()
every { b.bar(a = capture(aSlot)) } returns Unit
b.bar(a)
val captured = aSlot.captured
assertEquals(1, captured.a)
assertEquals(true, captured.b)
assertEquals("s", captured.s)
}
Or using withArg function and inline assertions
#Test
fun shouldCheckValuesAtConstructInlineAssertion() {
val a = A(1, true, "s")
val b = mockk<B>()
every { b.bar(a) } returns Unit
b.bar(a)
verify {
b.bar(withArg {
assertEquals(1, it.a)
assertEquals(true, it.b)
assertEquals("s", it.s)
})
}
}

How we can mock a CoroutineDatabase in ktor?

I use the KMongo tool
How we can mock a Coroutine Database?
How can we mock our database in a koin module?
Is there a way to do this?
Thanks for guiding me
Methods I have tried and it has not worked:
The first method:
single<CoroutineDatabase> {
val client = Mockito.mock(CoroutineClient::class.java)
client.getDatabase(CoreConstants.DATABASE_NAME)
}
The second method:
single<CoroutineDatabase> {
val client = declareMock<CoroutineClient> { }
client.getDatabase(CoreConstants.DATABASE_NAME)
}
I've managed to get this working with MockK with the following approach.
TLDR
Just use a mock of MongoDatabase/MongoCollection<T> and make their coroutine extension property return a mocked CoroutineDatabase/CoroutineCollection<T>. Also need to mock the actual MongoDatabase::getCollection to return the respective MongoCollection<T>.
Suppose we have this scenario.
data class User(val id: Int, val name: String)
class Service(private val myDatabase: CoroutineDatabase) {
private val userCollection: CoroutineCollection<User> = myDatabase.getCollection("users")
suspend fun getById(id: Int): User? = userCollection.findOneById(id)
}
Since userCollection is acquired by calling the inline method CoroutineDatabase::getCollection we need to mock all the code inside that inline because inline methods cannot be mocked with MockK (at the time of writing). Looking at the method code
inline fun <reified TDocument : Any> getCollection(
collectionName: String = KMongoUtil.defaultCollectionName(TDocument::class)
): CoroutineCollection<TDocument> =
database.getCollection(collectionName, TDocument::class.java).coroutine
It just calls com.mongodb.reactivestreams.client.MongoDatabase::getCollection and then uses this extension property to map it to a CoroutineCollection. Notice it uses the field database from CoroutineDatabase which is a MongoDatabase (The CoroutineDatabase was previously obtain via a similar extension property for MongoDatabase).
val <T : Any> MongoCollection<T>.coroutine: CoroutineCollection<T> get() = CoroutineCollection(this)
val MongoDatabase.coroutine: CoroutineDatabase get() = CoroutineDatabase(this)
Having all of this we need to mock:
Both coroutine extension properties on MongoDatabase and MongoCollection<T> (see mocking extension properties with MockK)
The actual MongoDatabase::getCollection because CoroutineDatabase::getCollection is an inline function
// Arrange
val mockedMongoDd: MongoDatabase = mockk<MongoDatabase> {
mockkStatic(MongoDatabase::coroutine)
val that = this
every { coroutine } returns mockk {
every { database } returns that
}
}
val mockedMongoCol: MongoCollection<User> = mockk<MongoCollection<User>> {
mockkStatic(MongoCollection<T>::coroutine)
val that = this
every { ofType<MongoCollection<T>>().coroutine } returns mockk {
every { collection } returns that
}
}
every {
mockedMongoDb.getCollection("users", User::class.java)
} returns mockedMongoCol
val mockedCoroutineDb = mockedMongoDb.coroutine
val mockedCoroutineCol = mockedMongoCol.coroutine
val service = Service(mockedCoroutineDb)
val expectedUser = User(2, "Joe")
coEvery {
mockedCoroutineCol.findOneById(2)
} returns expectedUser
// Act
val actualUser = service.getById(2)
// Assert
assertEquals(expectedUser, actualUser)
Finally, one could make some methods like the following to hide this details from the test.
inline fun <reified T : Any> mockkCoroutineCollection(
name: String? = null,
relaxed: Boolean = false,
vararg moreInterfaces: KClass<*>,
relaxUnitFun: Boolean = false,
block: MongoCollection<T>.() -> Unit = {}
): MongoCollection<T> = mockk(name, relaxed, *moreInterfaces, relaxUnitFun = relaxUnitFun) {
mockkStatic(MongoCollection<*>::coroutine)
val that = this
every { coroutine } returns mockk(name, relaxed, *moreInterfaces, relaxUnitFun = relaxUnitFun) {
every { collection } returns that
}
block()
}
inline fun mockkCoroutineDatabase(
name: String? = null,
relaxed: Boolean = false,
vararg moreInterfaces: KClass<*>,
relaxUnitFun: Boolean = false,
block: MongoDatabase.() -> Unit = {}
): MongoDatabase = mockk(name, relaxed, *moreInterfaces, relaxUnitFun = relaxUnitFun) {
mockkStatic(MongoDatabase::coroutine)
val that = this
every { coroutine } returns mockk(name, relaxed, *moreInterfaces, relaxUnitFun = relaxUnitFun) {
every { database } returns that
}
block()
}
This would reduce the first lines to
val mockedMongoDb: MongoDatabase = mockkCoroutineDatabase()
val mockedMongoCol: MongoCollection<User> = mockkCoroutineCollection<User>()
// ...

Writing an addition visitor function in Kotlin

I'm trying to write a visitor function in Kotlin that adds two integers together. I've been working off of some sample code and I can't figure out what these .value or .visit functions are. It doesn't seem to be declared in the sample code, so I'm unsure how to declare it in my code. Whenever I compile the code, I get an error saying that value is an unresolved reference.
Relevant Kotlin code:
package backend
import org.antlr.v4.runtime.*
import grammar.*
abstract class Data
class IntData(val value: Int): Data() {
override fun toString(): String
= "Int($value)"
}
class Context(): HashMap<String, Data>() {
constructor(parent: Context): this() {
this.putAll(parent)
}
}
abstract class Expr {
abstract fun eval(scope: Context): Data
fun run(program: Expr) {
try {
val data = program.eval(Context())
println("=> ${data}")
} catch(e: Exception) {
println("[err] ${e}")
}
}
}
class IntLiteral(val value: Int): Expr() {
override fun eval(scope:Context): Data
= IntData(value)
}
enum class Op {
Add,
Sub,
Mul,
Div
}
class Arithmetic(
val op: Op,
val left: Expr,
val right: Expr): Expr() {
override fun eval(scope: Context): Data {
val x = (left.eval(scope) as IntData).value
val y = (right.eval(scope) as IntData).value
return IntData(
when(op) {
Op.Add -> x + y
Op.Mul -> x * y
Op.Sub -> x - y
Op.Div -> x / y
}
)
}
}
}
class Compiler: PLBaseVisitor<Expr>() {
val scope = mutableMapOf<String, Expr>()
override fun visitAddExpr(ctx: PLParser.AddExprContext): Expr {
val xValue = this.visit(ctx.x)
val yValue = this.visit(ctx.y)
val result = xValue.value + yValue.value
return IntLiteral(result)
}
}
Relevant Antlr Grammar:
expr : x=expr '+' y=expr # addExpr
| x=expr '-' y=expr # subExpr
| x=expr '*' y=expr # mulExpr
| x=expr '/' y=expr # divExpr
;
Code I'm trying to execute:
val test = """
x=1+2
print(x)
"""
fun parse(source: String): PLParser.ProgramContext {
val input = CharStreams.fromString(source)
val lexer = PLLexer(input)
val tokens = CommonTokenStream(lexer)
val parser = PLParser(tokens)
}
val testTree = parse(source1)
val testTree = parse(source1)
fun execute(program: Expr?) {
if(program == null) {
println("Program is null.")
return
}
try {
val data = program.eval(Context())
println("> ${data}")
} catch(e: Exception) {
println("[err] ${e}")
}
}
execute(testProgram)
Code from sample:
data class NodeValue(val value: Int)
val visitor = object: CalcBaseVisitor<NodeValue>() {
override fun visitAddition(ctx: CalcParser.AdditionContext): NodeValue {
val xValue = this.visit(ctx.x)
val yValue = this.visit(ctx.y)
return NodeValue(xValue.value + yValue.value)
}
override fun visitValue(ctx: CalcParser.ValueContext): NodeValue {
val lexeme = ctx.Number().getText()
return NodeValue(lexeme.toInt())
}
}
You don’t show the code for your program.eval() method.
The eval function would need to create an instance of your Visitor. (You’ve done that and called it visitor).
You also have the root expr node in you program variable.
Now you would have your visitor “visit” that node and save the return value:
val nodeVal = visitor.visit(program)
At that point nodeVal.value will have the result of visiting that expression.
note: since you’re doing the evaluation in your visitor, there’s not really any use for your Arithmetic class (unless you refactor your visitor to use it instead of just doing the math, but I don’t see much value in that as the visitor is already pretty easy to read).

Kotlin generates SAM stubs inconsistencely

For example, I've got a functional interface:
public interface SomeInt<R, P> {
R execute(P param);
}
Later I want to pass it as a parameter, so I'm creating inline implementation
//implementation 1
val someInt = SomeInt { id: Int? -> "param $id" }
//implementation 2
val someFun = { id: Int? -> "param $id" }
val someInt2: SomeInt<String, Int> = SomeInt(someFun)
The code above generates two different implementations
I'm not sure where to look at, but that's what caught my eye:
println(someInt.javaClass.name)
println(someInt2.javaClass.name)
println(someInt.javaClass.methods.filter { it.name == "execute" }.map { "${it.name} ${it.returnType} ${it.parameterTypes.map { it.name }} ${it.parameters[0].name}\n" })
println(someInt2.javaClass.methods.filter { it.name == "execute" }.map { "${it.name} ${it.returnType} ${it.parameterTypes.map { it.name }} ${it.parameters[0].name}\n" })
output is:
my.test.TestKt$main$someInt$1
my.test.TestKt$sam$my_test_SomeInt$0
[execute class java.lang.String [java.lang.Integer] arg0
, execute class java.lang.Object [java.lang.Object] arg0
]
[execute class java.lang.Object [java.lang.Object] arg0
]
Why do both implementations generate totally different bytecode? I thought that at least the amount of method overloads should be the same