Comments (9)
Of course, I would like to contribute to this revision. I will try to fix this part.
from kotlin-faker.
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.
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.
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.
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.
@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.
from kotlin-faker.
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.
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.
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)
- Add context when newInstance() fails HOT 4
- Crashes in Jetpack Compose previews HOT 1
- Move to java17 as default
- Cannot generate valid en-GB postcode HOT 7
- Remove SLF4J HOT 1
- Postal Code for CA/UK returns regex, not an actual Postal Code HOT 5
- Add blns
- Add rendering for sub-provider functions in cli
- Make it possible to lookup providers by name in cli
- Improve integration tests
- Allow RandomClassProvider::randomClassInstance to use predefinedGenerators from config if present HOT 4
- RandomClassProvider doesn't handle constructorless types correctly in all instances HOT 1
- Allow random to be used with unique HOT 1
- Regexify generates invalid value HOT 2
- Regexify should take Regex as an argument HOT 1
- Regexify fails with StackOverflowError HOT 5
- random.nextLong(bound: Long) also returns negative values HOT 3
- Suggestion: internet.ipv4() and internet.ipv6() HOT 4
- Support inner classes in RandomClassProvider
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kotlin-faker.