serpro69 / kotlin-faker

Port of a popular ruby faker gem written in kotlin. Generate realistically looking fake data such as names, addresses, banking details, and many more, that can be used for testing and data anonymization purposes.
https://serpro69.github.io/kotlin-faker/
MIT License
472 stars 44 forks source link

Add parameter info context to user defined generators #179

Closed urosjarc closed 1 year ago

urosjarc commented 1 year ago

Changes for issue: #177

Breaking changes?

There are not breaking changes in this PR. :heart: :heart_eyes:

Code snipets

class TestClass(
    val bar: String
)
val t0: TestClass = randomProvider.randomClassInstance {
    typeGenerator<String> { pInfo -> pInfo.toString() }
}
val t1: TestClass = randomProvider.randomClassInstance {
    namedParameterGenerator("bar") { pInfo -> pInfo.toString() }
}
assert t0.bar == "ParameterInfo(index=0, name=bar, isOptional=false, isVararg=false)"
assert t1.bar == "ParameterInfo(index=0, name=bar, isOptional=false, isVararg=false)"
urosjarc commented 1 year ago

@serpro69 I could not test this PR because kotlin/yaml-to-json.gradle.kts is giving me a problems :disappointed: Please test this PR for me :heart:

FAILURE: Build completed with 2 failures.
1: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':core:yaml2json'.
> Unable to process file /home/urosjarc/.gradle/daemon/7.5.1/core/src/main/resources/locales
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
==============================================================================
2: Task failed with an exception.
-----------
* What went wrong:
java.lang.StackOverflowError (no error message)
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
==============================================================================
* Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
See https://docs.gradle.org/7.5.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD FAILED in 13s
14 actionable tasks: 10 executed, 4 up-to-date
serpro69 commented 1 year ago

Thanks a lot @urosjarc ! I will review this over the weekend :)

Not sure why you're getting that error with yaml2json plugin. Can you open a separate issue for it and provide some more output?

urosjarc commented 1 year ago

@serpro69 This is all the output that I got, I didn't got the time to investigate, in the evening I will at least finish pull request so that all github tests will pass...

urosjarc commented 1 year ago

Can you please explain how are docs generated, as far as I looked in the project it looks like docs are generated from tests. I tried to add docs explaining how to use this new feature but could not found how to add additional informations.

urosjarc commented 1 year ago

I have checked more thoroughly what was a problem and I saw that this problem is not worth a separate issue so I have joust fixed it directly in the last commit. And for the tests...

serpro69 commented 1 year ago

@serpro69 This is all the output that I got, I didn't got the time to investigate, in the evening I will at least finish pull request so that all github tests will pass...

You can get more details with --info or --debug for example, as gradle suggests in your output.

Can you please explain how are docs generated, as far as I looked in the project it looks like docs are generated from tests. I tried to add docs explaining how to use this new feature but could not found how to add additional informations.

Docs-from-tests are located under https://github.com/serpro69/kotlin-faker/tree/master/core/src/integration/kotlin/io/github/serpro69/kfaker/docs. You can take a look at https://github.com/serpro69/kotlin-faker/blob/31594082b5728ec70db43c588c093a8e20f1ef5c/core/src/integration/kotlin/io/github/serpro69/kfaker/docs/Extras.kt#L16-L24 for example (note the //START and //END comments which are delimiters for what's included in documentation)

Then the markdown part for the above is located here - https://github.com/serpro69/kotlin-faker/blob/31594082b5728ec70db43c588c093a8e20f1ef5c/docs/src/orchid/resources/wiki/extras.md?plain=1#L32-L42

That's pretty much all you need to add documentation for this change.

I have checked more thoroughly what was a problem and I saw that this problem is not worth a separate issue so I have joust fixed it directly in the last commit. And for the tests...

Nice, thanks! Weird that a relative path was causing issues for you and throwing a stackoverflow error. But so long as it works now, I suppose using an absolute path is also fine :)

urosjarc commented 1 year ago

I have added docs for Extras and Homepage... In the homepage I have added some funky comments in the code to highligh the importance... When I first came to your website I have never even looked on the text that is located on the left side... The code speaks for it self...

val baz: Baz = faker.randomProvider.randomClassInstance {
    // ヽ(^o^)丿 ᕕ(ᐛ)ᕗ Prepend string type parameter values with parameter name!
    typeGenerator<String> { parameterInfo -> "${parameterInfo.name}_${randomString()}" }
    typeGenerator<UUID> { UUID.fromString("00000000-0000-0000-0000-000000000000") }
    typeGenerator<Int> { faker.random.nextInt(min = 0, max = 9) }
}
urosjarc commented 1 year ago

Before I started to code I had a long 1 min life contemplating realization that having configuration would be to limiting for the developer and eventualy it will not cover all usecases that you can think of. Its better to provide developers with with all the informations so that they can structure random data, the fuck they want... I love you babe and this library and I dont want to see you getting stupid issues from developers complaining the configuration is to limiting... It would break my heart... Thank you for fast response and willingness to explain my smartass where to make a change... You can be my waifu anytime 😘... no homo...