How can I infer generic type in kotlin? (covariance problem) - spring-boot

I have two class which extends Data, interface.
A: Data
B: Data
Then I have two repositories. TestRepository is interface which get generic class.
TestRepository<T: Data> {
fun save(data: T): T
}
#Repository
ARepository: TestRepository<A> {
override fun save(data: A): A
}
#Repository
BRepository: TestRepository<B> {
override fun save(data: B): B
}
it all have save method which gets data from generic type, and returns generic type.
ARepo and BRepo gets data from A: Data, B:Data and returns corresponding type.
Then we have new Service,
#Service
CService(
private aRepository: ARepository,
private bRepository: BRepository
) {
fun test(t: String): TestRepository<out Data> =
when (t) {
'1' -> aRepository
'2' -> bRepository
else -> throw Error("error")
}
}
it returns aRepository or bRepository, so return type of test function is TestRepository<out Data>. But when I try to use that class with DI,
#Service
class TestClass(
private val cService: CService
) {
fun cServiceTest() {
...
val saveObject = Data('')
val repo = cService.test("1") // or "2"
repo.save(saveObject) <-- error
}
}
repo.save emits error,
Type mismatch.
Required:
Nothing
Found:
Data
How can I solve this error?

How about this?
import kotlin.reflect.KClass
interface Data {
val name: String
}
data class A(
override val name: String = "A"
) : Data
data class B(
override val name: String = "B"
) : Data
interface DataRepository<T : Data> {
fun save(data: T): T
}
class ARepository : DataRepository<A> {
override fun save(data: A): A {
println("Saved - (A)")
return data.copy()
}
}
class BRepository : DataRepository<B> {
override fun save(data: B): B {
println("Saved - (B)")
return data.copy()
}
}
class DataService(
private val aClassRepository: ARepository = ARepository(),
private val bClassRepository: BRepository = BRepository(),
) {
#Suppress("UNCHECKED_CAST")
fun <T : Data> getRepository(_class: KClass<T>): DataRepository<T> =
when (_class) {
A::class -> aClassRepository
B::class -> bClassRepository
else -> throw RuntimeException()
} as? DataRepository<T> ?: throw RuntimeException()
}
fun main() {
val service = DataService()
val repository = service.getRepository(A::class)
val saved = repository.save(A())
println(saved)
}
I don't know what kind of problem you are trying to solve.
So I think the solution I came up with is not good.

Related

Kotlin: How to solve the type casting problem?

I am trying to save data to a database. To do this, I choose a service using enum.
interface MyService<K: Serializable, V: Serializable> {
fun gerResource(): JpaRepository<K, V>
fun convert(list: List<String>) : List<K>
fun saveData(data: List<K>) = gerResource().saveAll(data) // signature saveAll: <S extends T> List<S> saveAll(Iterable<S> var1)
}
class DbData : Serializable
class KafkaData : Serializable
#Service
class FirstService( val repository : JpaRepository<DbData, Long>) : MyService<DbData, Long> {
override fun gerResource() = repository
override fun convert(list: List<String>) : List<DbData>{
return listOf(DbData())
}
}
#Service
class SecondService(val repository : JpaRepository<KafkaData, Long>) : MyService<KafkaData, Long> {
override fun gerResource() = repository
override fun convert(list: List<String>) : List<KafkaData>{
return listOf(KafkaData())
}
}
enum class ServiceType {
FIRST,
SECOND
}
#Service
class Cast(
private val firstService: MyService<DbData, Long>,
private val secondService: MyService<KafkaData, Long>
){
private fun saveMessage(type: ServiceType, list: List<String>) {
val myService = when (type) {
ServiceType.FIRST -> firstService
ServiceType.SECOND -> secondService
}
val convertList = myService.convert(list)
myService.saveData(convertList)
}
}
But I have a problem with cast in the code
val convertList = myService.convert(list)
myService.saveData(convertList)
Kotlin thinks that convert returns List<Serialazable> and I get an error in saveData:
Type mismatch.
Required: List<Nothing>
Found: List<Serializable>
if I change fun saveData(data: List<K>) to fun saveData(data: List<Serializable>), I get an error in saveAll(data)
Type mismatch.
Required:K!
Found: Serializable!
Since saveAll is a Spring Framework method, I can't change it.
How can I fix the error?

Using ConnectableFlux for hot stream REST endpoint

I'm trying to create a REST endpoint to subscribe to a hot stream using Reactor.
My test provider for the stream looks like this:
#Service
class TestProvider {
fun getStream(resourceId: String): ConnectableFlux<QData> {
return Flux.create<QData> {
for (i in 1..10) {
it.next(QData(LocalDateTime.now().toString(), "next"))
Thread.sleep(500L)
}
it.complete()
}.publish()
}
}
My service for the REST endpoint looks like this:
#Service
class DataService #Autowired constructor(
private val prv: TestProvider
) {
private val streams = mutableMapOf<String, ConnectableFlux<QData>>()
fun subscribe(resourceId: String): Flux<QData> {
val stream = getStream(resourceId)
return Flux.push { flux ->
stream.subscribe{
flux.next(it)
}
flux.complete()
}
}
private fun getStream(resourceId: String): ConnectableFlux<QData> {
if(streams.containsKey(resourceId).not()) {
streams.put(resourceId, createStream(resourceId))
}
return streams.get(resourceId)!!
}
private fun createStream(resourceId: String): ConnectableFlux<QData> {
val stream = prv.getStream(resourceId)
stream.connect()
return stream
}
}
The controller looks like this:
#RestController
class DataController #Autowired constructor(
private val dataService: DataService
): DataApi {
override fun subscribe(resourceId: String): Flux<QData> {
return dataService.subscribe(resourceId)
}
}
The API interface looks like this:
interface DataApi {
#ApiResponses(value = [
ApiResponse(responseCode = "202", description = "Client is subscribed", content = [
Content(mediaType = "application/json", array = ArraySchema(schema = Schema(implementation = QData::class)))
])
])
#GetMapping(path = ["/subscription/{resourceId}"], produces = [MediaType.APPLICATION_JSON_VALUE])
fun subscribe(
#Parameter(description = "The resource id for which quality data is subscribed for", required = true, example = "example",allowEmptyValue = false)
#PathVariable("resourceId", required = true) #NotEmpty resourceId: String
): Flux<QData>
}
Unfortunately, my curl delivers an empty array.
Does anyone has an idea what the issue is? Thanks in advance!
I had to run connect() async in DataService:
CompletableFuture.runAsync {
stream.connect()
}

createFromFile( ) doesn't populate database - Room

EDIT: 09/30
Thanks to #shb, I've determined that createFromFile() is not properly populating the database as intended.
If anyone can point out what I'm doing wrong/missing, that would be great! Please refer to the below picture for reference on the database structure. Thanks in advance!
MainActivity
class MainActivity: AppCompatActivity() {
lateinit var mDatabase: AppDatabase
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mDatabase = AppDatabase.getInstance(this)
}
fun getFacilityNbr() {
val planInfo = mDatabase.getPlanInfoDao().getPlanInfo()
Log.d("MainActivity", "Size = ${planInfo.size}") // 0
}
}
AppDatabase
#Database(entities = [PlanInfo::class], version = 1)
abstract class AppDatabase: RoomDatabase() {
companion object {
#Volatile private var instance: AppDatabase? = null
val file = File("${Environment.getExternalStorageDirectory()}/appdata/pla/assignment-file/test.db")
Log.d("App Database", "Is valid file? - ${file.isFile}") // true
fun getInstance(context Context) = instance ?: synchronized(this) {
instance ?: Room.databaseBuilder(context.applicationContext, AppDatabase::class.java, "MyDb")
.createFromFile(File("path/to/my/test.db"))
.allowMainThreadQueries()
.build().also { instance = it }
}
abstract fun getPlanInfoDao(): PlanInfoDao
}
PlanInfoDao
#Dao
interface PlanInfoDao {
#Query("SELECT * FROM PlanInfo")
fun getPlanInfo(): List<PlanInfo>
}
PlanInfo
#Entity
data class PlanInfo (
#PrimaryKey
#ColumnInfo(name = "Facility_Nbr")
val facilityNbr: String
)
#Query("SELECT * FROM PlanInfo")
It returns a list of PlanInfo
Change return type to List<PlanInfo>
#Query("SELECT * FROM PlanInfo")
fun getPlanInfo(): List<PlanInfo>
Or you might wanna rewrite your query like below.
#Query("SELECT * FROM PlanInfo where Lifecycle_ID= :lifeCycleId")
fun getPlanInfo(lifeCycleId: String): PlanInfo
EDIT
In doubt, you make sure that there is data in that table, try inserting a row successfully before fetching in your code. Make sure its there.

Blocking problem of graphql-kotlin with DataLoader&BatchLoader

I used the framework "https://github.com/ExpediaGroup/graphql-kotlin" to learn graphql programming of kotlin under springframework.
I used DataLoader&BatchLoader to resolve the 'N+1' loading problem.
When the scope of DataLoader objects is singleton, it works, but it's not my goal because temporary cache mechanism should not be bridging over different requests.
Then I changed the scope of DataLoader objects to be prototype, in all probability, the graphql query may be blocking and associated objects will not be loaded, client waits the response forever.
What's the reason and how can I resolve it?
I did it like this:
Create a simple springboot application, add maven dependency of graph-kotlin
<dependency>
<groupId>com.expediagroup</groupId>
<artifactId>graphql-kotlin-spring-server</artifactId>
<version>2.0.0.RC3</version>
</dependency>
Create two model classes(Note: Their code will be changed in the final step)
data class Department(
val id: Long,
val name: String
)
data class Employee(
val id: Long,
val name: String,
#GraphQLIgnore val departmentId: Long
)
Create two mocked repository objects
val DEPARTMENTS = listOf(
Department(1L, "Develop"),
Department(2L, "Test")
)
val EMPLOYEES = listOf(
Employee(1L, "Jim", 1L),
Employee(2L, "Kate", 1L),
Employee(3L, "Tom", 2L),
Employee(4L, "Mary", 2L)
)
#Repository
open class DepartmentRepository {
companion object {
private val LOGGER = LoggerFactory.getLogger(DepartmentRepository::class.java)
}
open fun findByName(namePattern: String?): List<Department> = //For root query
namePattern
?.takeIf { it.isNotEmpty() }
?.let { pattern ->
DEPARTMENTS.filter { it.name.contains(pattern) }
}
?: DEPARTMENTS
open fun findByIds(ids: Collection<Long>): List<Department> { // For assciation
LOGGER.info("BatchLoad departments by ids: [${ids.joinToString(", ")}]")
return DEPARTMENTS.filter { ids.contains(it.id) }
}
}
#Repository
open class EmployeeRepository {
companion object {
private val LOGGER = LoggerFactory.getLogger(EmployeeRepository::class.java)
}
open fun findByName(namePattern: String?): List<Employee> = //For root query
namePattern
?.takeIf { it.isNotEmpty() }
?.let { pattern ->
EMPLOYEES.filter { it.name.contains(pattern) }
}
?: EMPLOYEES
open fun findByDepartmentIds(departmentIds: Collection<Long>): List<Employee> { // For association
LOGGER.info("BatchLoad employees by departmentIds: [${departmentIds.joinToString(", ")}]")
return EMPLOYEES.filter { departmentIds.contains(it.departmentId) }
}
}
Create a graphql query object to export root query operations
#Service
open class OrgService(
private val departmentRepository: DepartmentRepository,
private val employeeRepository: EmployeeRepository
) : Query {
fun departments(namePattern: String?): List<Department> =
departmentRepository.findByName(namePattern)
fun employees(namePattern: String?): List<Employee> =
employeeRepository.findByName(namePattern)
}
Create an abstract class for many-to-one associated object loading
abstract class AbstractReferenceLoader<K, R: Any> (
batchLoader: (Collection<K>) -> Collection<R>,
keyGetter: (R) ->K,
optionsInInitializer: (DataLoaderOptions.() -> Unit) ? = null
): DataLoader<K, R?>(
{ keys ->
CompletableFuture.supplyAsync {
batchLoader(keys)
.associateBy(keyGetter)
.let { map ->
keys.map { map[it] }
}
}
},
optionsInInitializer?.let {
DataLoaderOptions().apply {
this.it()
}
}
)
Create an abstract class for one-to-many associated collection loading
abstract class AbstractListLoader<K, E>(
batchLoader: (Collection<K>) -> Collection<E>,
keyGetter: (E) ->K,
optionsInInitializer: (DataLoaderOptions.() -> Unit) ? = null
): DataLoader<K, List<E>>(
{ keys ->
CompletableFuture.supplyAsync {
batchLoader(keys)
.groupBy(keyGetter)
.let { map ->
keys.map { map[it] ?: emptyList() }
}
}
},
optionsInInitializer?.let {
DataLoaderOptions().apply {
this.it()
}
}
)
Create annotation to let spring manager DataLoader beans by prototype scope
#Retention(RetentionPolicy.RUNTIME)
#Target(AnnotationTarget.CLASS)
#Component
#Scope(
ConfigurableBeanFactory.SCOPE_PROTOTYPE,
proxyMode = ScopedProxyMode.NO
)
annotation class DataLoaderComponent
Create loader bean to load the parent object reference of Employee object
#DataLoaderComponent
open class DepartmentLoader(
private val departmentRepository: DepartmentRepository
): AbstractReferenceLoader<Long, Department>(
{ departmentRepository.findByIds(it) },
{ it.id },
{ setMaxBatchSize(256) }
)
Create loader bean to load the child object collection of Department object
#DataLoaderComponent
open class EmployeeListByDepartmentIdLoader(
private val employeeRepository: EmployeeRepository
): AbstractListLoader<Long, Employee>(
{ employeeRepository.findByDepartmentIds(it) },
{ it.departmentId },
{ setMaxBatchSize(16) }
)
Create an GraphQL configuration to let 'graphql-kotlin' know all the DataLoader beans
#Configuration
internal abstract class GraphQLConfig {
#Bean
open fun dataLoaderRegistryFactory(): DataLoaderRegistryFactory =
object: DataLoaderRegistryFactory {
override fun generate(): DataLoaderRegistry = dataLoaderRegistry()
}
#Bean
#Scope(
ConfigurableBeanFactory.SCOPE_PROTOTYPE,
proxyMode = ScopedProxyMode.NO
)
protected open fun dataLoaderRegistry(
loaders: List<DataLoader<*, *>>
): DataLoaderRegistry =
DataLoaderRegistry().apply {
loaders.forEach { loader ->
register(
loader::class.qualifiedName,
loader
)
}
}
#Lookup
protected abstract fun dataLoaderRegistry(): DataLoaderRegistry
}
Add tow methods into DataFetchingEnvironment to get DataLoader objects
inline fun <reified L: AbstractReferenceLoader<K, R>, K, R> DataFetchingEnvironment.getReferenceLoader(
): DataLoader<K, R?> =
this.getDataLoader<K, R?>(L::class.qualifiedName)
inline fun <reified L: AbstractListLoader<K, E>, K, E> DataFetchingEnvironment.getListLoader(
): DataLoader<K, List<E>> =
this.getDataLoader<K, List<E>>(L::class.qualifiedName)
Change the code of Department and Employee, let them support association
data class Department(
val id: Long,
val name: String
) {
suspend fun employees(env: DataFetchingEnvironment): List<Employee> =
env
.getListLoader<EmployeeListByDepartmentIdLoader, Long, Employee>()
.load(id)
.await()
}
data class Employee(
val id: Long,
val name: String,
#GraphQLIgnore val departmentId: Long
) {
suspend fun department(env: DataFetchingEnvironment): Department? =
env
.getReferenceLoader<DepartmentLoader, Long, Department>()
.load(departmentId)
.await()
}
Build & Run
Start the SpringBoot applications, open http://locathost:8080/playground.
Execute the query, the result may be success or failed!
{
employees {
id
name
department {
id
name
employees {
id
name
}
}
}
}
If it is success, the client can get the response, and the server log is
2020-03-15 22:47:26.366 INFO 35616 --- [onPool-worker-5] org.frchen.dal.DepartmentRepository : BatchLoad departments by ids: [1, 2]
2020-03-15 22:47:26.367 INFO 35616 --- [onPool-worker-5] org.frchen.dal.EmployeeRepository : BatchLoad employees by departmentIds: [1, 2]
If it is failed, the client is blocking and waits for the response forever, and the server log is
2020-03-15 22:53:43.159 INFO 35616 --- [onPool-worker-6] org.frchen.dal.DepartmentRepository : BatchLoad departments by ids: [1, 2]

lateinit property S has not been initialized

I want inject a singleton in another class by kotlin in spring boot.
S.kt
#Singleton
#Component
class S(
private val userService: UserService,
val companyRepo: CompanyRepo
)
WorkingGroup.kt
class WorkingGroup(
override val name: String = "",
override val desc: String = ""
) : Csv() {
fun isCompatible(ct2: WorkingGroup): Boolean = this == ct2
companion object : ICsvEnumCompanion<WorkingGroup> {
#Inject
private lateinit var s: S
override val VALUES: List<WorkingGroup>
by lazy {
val details = s.user().company.details ?: CompanyDetails()
details.workingGroups.map { WorkingGroup(it.name, it.desc) }
}
}
}
By this code, I get below error:
Caused by: org.zalando.problem.DefaultProblem: Internal Server Error: lateinit property s has not been initialized
I search for this error and found some result like this, but the problem not solved.
How can I inject service in companion object in kotlin?
In order for Spring to inject into a companion object you will need to create a setter for the field outside of the companion object. WorkingGroup will need to be a Spring managed bean in order for Spring to autowire it (inject dependencies).
#Component
class WorkingGroup(
override val name: String = "",
override val desc: String = ""
) : Csv() {
fun isCompatible(ct2: WorkingGroup): Boolean = this == ct2
companion object : ICsvEnumCompanion<WorkingGroup> {
private lateinit var s: S
override val VALUES: List<WorkingGroup>
by lazy {
val details = s.user().company.details ?: CompanyDetails()
details.workingGroups.map { WorkingGroup(it.name, it.desc) }
}
}
#Autowired
fun setS(value: S) {
s = value;
}
}

Resources