Giter Club home page Giter Club logo

Comments (9)

mandanai avatar mandanai commented on May 28, 2024 1

Of course, I would like to contribute to this revision. I will try to fix this part.

from kotlin-faker.

serpro69 avatar serpro69 commented on May 28, 2024 1

Of course, I would like to contribute to this revision. I will try to fix this part.

Great! Let me know if you need any help then.

from kotlin-faker.

mandanai avatar mandanai commented on May 28, 2024 1

Hi @serpro69
Right, I have a workaround for this issue
Example in below; it's work for me:

data class BaseResponse<T>(
    val code: ResponseCode = ResponseCode.OK,
    val message: String = "",
    val error: String = "",
    val timestamp: ZonedDateTime = ZonedDateTime.now(),
    val data: T? = null,
    val status: String = ""
)

data class Car(
    val type: String = "",
    val color: String = "",
    val availableDate: ZonedDateTime = ZonedDateTime.now()
)

object CarMock {

    fun carSuccess(faker: Faker): BaseResponse<Car> {
        return BaseResponse(
            code = ResponseCode.OK,
            message = faker.lorem.punctuation(),
            status = "SUCCESS",
            data = getCar(faker)
        )
    }

   private fun getCar(faker: Faker): Car {
        return faker.randomProvider.randomClassInstance {
            typeGenerator<ZonedDateTime> {
                ZonedDateTime.now()
            }
        }
    }
}

from kotlin-faker.

serpro69 avatar serpro69 commented on May 28, 2024 1

Yeah, I would do something similar myself :) I guess it's currently the only way to go about it due to how JVM deals with generics. I'm adding this to docs and closing the issue for now, but feel free to re-open if you find out how this can be solved in a better fashion.
Thanks!

from kotlin-faker.

serpro69 avatar serpro69 commented on May 28, 2024

Hi @mandanai , thanks for opening the issue.
Yeah, I don't think I've ever tested generics with randomClassInstance generation.
I will try to reproduce this and will look into how this can be fixed (I'm not sure if I have much time this month, but I will have some free time in July for sure). Unless you'd like to contribute a fix yourself?

from kotlin-faker.

mandanai avatar mandanai commented on May 28, 2024

@serpro69 At RandomClassProvider.kt from this it.type.classifier, I got T (KTypeParemeterImpl). How do I know the class type from the generic type, Do you have any ideas?

And for this line cast as KClass<*>, an exception event will occur.

image

from kotlin-faker.

serpro69 avatar serpro69 commented on May 28, 2024

Hi @mandanai ,

I do not think it's possible to get the class type from generic type; you have no type information for T because JVM erases the type information. Though kotlin might have some tricks up it's sleeve that could help solve this. What first comes to mind is reified type parameters, but that of course only works with inline functions (at least for now)

I'm on vacation currently, with limited access to internet. But I'll try to search around once I'm back if you don't find an answer to this yourself by then.
I do suspect it might not be possible to fix this with current limitations of generic types.

from kotlin-faker.

mandanai avatar mandanai commented on May 28, 2024

Hi @serpro69
Many thanks, I tried to find many ways but still haven't found the solution to this problem and if applied I can not use randomClassInstance directly with that Class; I use a separate randcomClassInstance and assign values ​​to a generic type instead.

from kotlin-faker.

serpro69 avatar serpro69 commented on May 28, 2024

I use a separate randcomClassInstance and assign values ​​to a generic type instead.

Does this mean you have a "workaround" for this issue, @mandanai ?
If so, we should at least document how to work around this issue until generic properties in classes are properly supported with randomClassInstance

from kotlin-faker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.