Comments (12)
from kotlin-faker.
Hi @urosjarc , thanks for opening this issue.
To understand it better, do you want to prepend string values with the property name, or was that just an example?
from kotlin-faker.
Look, now that you explained where to make a change I will be more than happy to create PR myself and show my appreciation for the work that you have done for this library.
I'm planning to add config with following options...
instanceProperties: {
prepend: {
active: Boolean = false,
position: <start,end> = start,
style: <snake_case, cammel_case, ...> = camel_case
text: <leave, capitalize, lowercase, ...> = leave
joinWith: String = "_"
maxWidth: Int? = null
forceWidth: Int? = null
align: <left, right> = left
emptySpaceFill: String = "_"
},
}
from kotlin-faker.
I will put some good tests displaying all possible configurations. 👍
from kotlin-faker.
I would like that faker prepends all string type property values with property name automatically if using randomClassInstance
on some random class.
I suggest adding a configuration option to faker prependWithPropertyName: Boolean
and by default it would be false so that breaking changes will not happen in the next version.
I would create PR myself if I would understand the architecture of this library.
from kotlin-faker.
In the CONTRIBUTING it writes only how to add a new provider but I could not see any manuals that could help me to structure PR myself. 😓 I usually don't want to bother people with my wishes but in this case, I could not do things differently.
If you explain which files I have to look for, I will create PR myself no problem.
from kotlin-faker.
PRs are very much welcome, but I can take a look at it myself also if you don't feel like making a PR :) (Not promising a super fast solution though, but I will try to find time this month.)
The implementation for random class instance is here - https://github.com/serpro69/kotlin-faker/blob/master/core/src/main/kotlin/io/github/serpro69/kfaker/provider/misc/RandomClassProvider.kt
It's not mentioned in the "contributing" because it's a bit "specific", but the file has kdocs which hopefully makes it easier to understand what's what.
Anyways, I can see how something like this can be useful, but I would also like it to be more configurable so to speak. Prepending a property name seems like a very specific use-case. What I envision doing here is to add another function similar to typeGenerator
or namedParameterGenerator
(propertyGenerator
, for example), which would give access to the KProperty
, for example? There you could do something like this:
faker.randomProvider.randomClassInstance<T> {
propertyGenerator<String> { prop ->
"${prop.name}_${faker.random.randomString()}"
}
}
I need to think a bit if this makes sense at all and if so - how to implement it.
from kotlin-faker.
Sounds good :)
I rescind my previous proposal on giving access to KProperty
via a new function - I kind of thought it might be useful, but now that I've given it some thought, I don't really see how that would make things better in any way; and I wanted to get rid of reflection also for awhile now.
So yeah, let's go for the configuration of instance properties instead.
Not sure what some of these configs you mention are supposed to do, but I'm happy to look at the PR and we can continue discussing the implementation there :)
from kotlin-faker.
Available in 1.14.0-rc.1
;)
from kotlin-faker.
@serpro69 Here is my example of how to use this feature in the best possible way...
The code so fine you need sunglasses 😎
val fake = Faker()
var str_counter = 0
inline fun <reified T : Any> random(): T = fake.randomProvider.randomClassInstance {
this.typeGenerator<String> { pInfo ->
str_counter++
"${pInfo.name}_${str_counter}"
}
}
data class Person(
val name: String,
val surname: String,
val phone: String,
val email: String,
val password: String,
val username: String,
)
fun main() {
for (i in 0..10) {
val oseba = random<Person>()
println(oseba)
}
}
Person(name=name_1, surname=surname_2, phone=phone_3, email=email_4, password=password_5, username=username_6)
Person(name=name_7, surname=surname_8, phone=phone_9, email=email_10, password=password_11, username=username_12)
Person(name=name_13, surname=surname_14, phone=phone_15, email=email_16, password=password_17, username=username_18)
Person(name=name_19, surname=surname_20, phone=phone_21, email=email_22, password=password_23, username=username_24)
Person(name=name_25, surname=surname_26, phone=phone_27, email=email_28, password=password_29, username=username_30)
Person(name=name_31, surname=surname_32, phone=phone_33, email=email_34, password=password_35, username=username_36)
Person(name=name_37, surname=surname_38, phone=phone_39, email=email_40, password=password_41, username=username_42)
Person(name=name_43, surname=surname_44, phone=phone_45, email=email_46, password=password_47, username=username_48)
Person(name=name_49, surname=surname_50, phone=phone_51, email=email_52, password=password_53, username=username_54)
Person(name=name_55, surname=surname_56, phone=phone_57, email=email_58, password=password_59, username=username_60)
Person(name=name_61, surname=surname_62, phone=phone_63, email=email_64, password=password_65, username=username_66)
from kotlin-faker.
Or event better...
val fake = Faker()
var counters = mutableMapOf<String, Int>()
inline fun <reified T : Any> random(): T = fake.randomProvider.randomClassInstance {
this.typeGenerator<String> { pInfo ->
val value = counters.getOrDefault(pInfo.name, -1) + 1
counters[pInfo.name] = value
"${pInfo.name}_${value}"
}
}
Person(name=name_0, surname=surname_0, phone=phone_0, email=email_0, password=password_0, username=username_0)
Person(name=name_1, surname=surname_1, phone=phone_1, email=email_1, password=password_1, username=username_1)
Person(name=name_2, surname=surname_2, phone=phone_2, email=email_2, password=password_2, username=username_2)
Person(name=name_3, surname=surname_3, phone=phone_3, email=email_3, password=password_3, username=username_3)
Person(name=name_4, surname=surname_4, phone=phone_4, email=email_4, password=password_4, username=username_4)
Person(name=name_5, surname=surname_5, phone=phone_5, email=email_5, password=password_5, username=username_5)
Person(name=name_6, surname=surname_6, phone=phone_6, email=email_6, password=password_6, username=username_6)
Person(name=name_7, surname=surname_7, phone=phone_7, email=email_7, password=password_7, username=username_7)
Person(name=name_8, surname=surname_8, phone=phone_8, email=email_8, password=password_8, username=username_8)
Person(name=name_9, surname=surname_9, phone=phone_9, email=email_9, password=password_9, username=username_9)
Person(name=name_10, surname=surname_10, phone=phone_10, email=email_10, password=password_10, username=username_10)
from kotlin-faker.
Nice :) Glad it's doing exactly what you needed. I'm sure someone else will find this feature useful as well :)
from kotlin-faker.
Related Issues (20)
- 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
- Remove all deprecated functionality in faker 2.0
- Split core Faker into sub-fakers by categories
- Allow creating custom fakers / generators
- kotlin-faker 2.0 - Breaking Changes HOT 1
- Suggestion: Add a Database Provider HOT 6
- Add Kotlin BCV to keep changes to public API under control
- Add BOM to manage dependencies HOT 2
- Figure out versioning for submodules
- Kotlin Multiplatform Support HOT 11
- Fix publishing of snapshots
- Random country code returned for address with locale `en-US` HOT 12
- Add faker extension module for kotest Arbs
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.