Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render sub-providers functions in cli output #197

Merged
merged 5 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
*** `Faker().southPark.episodes()`
** Updates to existing localized dictionaries

* https://github.com/serpro69/kotlin-faker/pull/197[#197] [cli] Render sub-providers functions in cli output

[discrete]
=== Changed

* ...
* https://github.com/serpro69/kotlin-faker/pull/197[#197] [cli] Change cli `lookup` command to return matching providers by name

[discrete]
=== Fixed
Expand Down
35 changes: 24 additions & 11 deletions cli-bot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ Quickly find required kotlin-faker functionality from your terminal
<img src=./img/faker-peek.gif width="768" height="606" alt=""/>

## ToC

* [Installation](#installation)
* [Usage](#usage)
* [Available commands](#available-commands)
* [list](#list)
* [lookup](#lookup)
* [Available options](#available-options)
* [--verbose](#verbose-output)
* [--java-syntax](#switch-to-java-syntax)
* [--locale](#using-non-default-locale)
* [--list-locales](#list-available-locales)
* [Available commands](#available-commands)
* [list](#list)
* [lookup](#lookup)
* [Available options](#available-options)
* [--verbose](#verbose-output)
* [--java-syntax](#switch-to-java-syntax)
* [--locale](#using-non-default-locale)
* [--list-locales](#list-available-locales)
* [Thanks](#thanks)

## Installation

The native images are available on [releases](https://github.com/serpro69/kotlin-faker/releases) page.
Download the image to a desired location and make it executable or use below script (Depends on [jq](https://stedolan.github.io/jq/))

Expand All @@ -28,36 +30,47 @@ chmod a+x ~/faker-bot
```

## Usage

Usage details are also available with the `--help` option:
`./faker-bot --help`
`./faker-bot list --help`
`./faker-bot lookup --help`

### Available commands

#### `list`

* `./faker-bot list` - list all providers and their functions
* `./faker-bot list Address Name` - list functions of `Address` and `Name` providers (Case insensitive)

Since version `1.4.0` partial matching is also supported:

* `./faker-bot list addr` - list functions of `Address` provider

#### `lookup`
* `./faker-bot lookup name` - lookup functions by name (Case insensitive partial matching)
#### `lookup`

* `./faker-bot lookup name` - lookup providers and functions by name (Case insensitive partial matching)

### Available options

#### Verbose output

`./faker-bot list --verbose` - prints sample values for each function
`./faker-bot lookup name --verbose` - prints sample values for each function

#### Switch to java syntax

`./faker-bot list --java-syntax` - prints list of all available providers and their functions using java syntax

#### Using non-default locale
`./faker-bot list --verbose --locale de` - combined with `--verbose`, all localized functions will be printed

`./faker-bot list --verbose --locale de` - combined with `--verbose`, all localized functions will be printed
using `de` locale.

#### List available locales

`./faker-bot list --list-locales` - prints all available locales

## Thanks

Inspired by [faker-ruby/faker-bot](https://github.com/faker-ruby/faker-bot)
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,23 @@ class Introspector(private val faker: Faker) {
&& it.returnType.classifier != StringProvider::class // Ignore this one as it's "special"
}

// Get a list of all publicly visible functions in each provider
val providerFunctions: Map<KProperty<*>, Sequence<KFunction<*>>> = providers.associateBy { provider ->
provider.getter.call(faker)!!::class.declaredMemberFunctions.asSequence().filter {
// Get a list of all publicly visible functions and sub-provider properties in each provider
val providerData: Map<KProperty<*>, Pair<Sequence<KFunction<*>>, Map<KProperty<*>, Sequence<KFunction<*>>>>> = providers.associateBy { provider ->
val providerInstance = provider.getter.call(faker)!!

val functions = providerInstance::class.declaredMemberFunctions.asSequence().filter {
it.visibility == KVisibility.PUBLIC && !it.annotations.any { ann -> ann is Deprecated }
}
val properties = providerInstance::class.declaredMemberProperties.asSequence().filter {
it.visibility == KVisibility.PUBLIC
&& it.returnType.isSubtypeOf(FakeDataProvider::class.starProjectedType)
&& !it.annotations.any { ann -> ann is Deprecated }
&& it.name != "unique"
}.associateBy { sub: KProperty<*> ->
sub.getter.call(providerInstance)!!::class.declaredMemberFunctions.asSequence().filter {
it.visibility == KVisibility.PUBLIC && !it.annotations.any { ann -> ann is Deprecated }
}
}.map { it.value as KProperty<*> to it.key }.toMap()
(functions to properties)
}.map { it.value to it.key }.toMap()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io.github.serpro69.kfaker.app.cli
import io.github.serpro69.kfaker.Faker
import io.github.serpro69.kfaker.app.subcommands.CommandOptions
import io.github.serpro69.kfaker.provider.Money
import io.github.serpro69.kfaker.provider.misc.StringProvider
import kotlin.reflect.KFunction
import kotlin.reflect.KProperty
import kotlin.system.exitProcess
Expand Down Expand Up @@ -61,18 +60,25 @@ fun renderProvider(
options: CommandOptions,
faker: Faker,
provider: KProperty<*>,
functions: Sequence<KFunction<*>>
subProvider: KProperty<*>?,
functions: Sequence<KFunction<*>>,
properties: Map<KProperty<*>, Sequence<KFunction<*>>>,
): Renderer {
val renderedFunctions = if (options.verbose) {
val instance = if (subProvider != null) {
subProvider.getter.call(provider.getter.call(faker))
} else {
provider.getter.call(faker)
}
functions.map {
val value = when (it.parameters.size) {
1 -> it.call(provider.getter.call(faker)).toString()
1 -> it.call(instance).toString()
2 -> {
if (it.parameters[1].isOptional) { // optional params are enum typed (see functions in Dune, Finance or Tron, for example)
it.callBy(mapOf(it.parameters[0] to provider.getter.call(faker))).toString()
} else it.call(provider.getter.call(faker), "").toString()
it.callBy(mapOf(it.parameters[0] to instance)).toString()
} else it.call(instance, "").toString()
}
3 -> it.call(provider.getter.call(faker), "", "").toString()
3 -> it.call(instance, "", "").toString()
else -> {
when (it) {
Money::amount -> it.call(provider.getter.call(faker), 100_000..1_000_000, true, ",", ".")
Expand All @@ -89,11 +95,17 @@ fun renderProvider(
} else {
functions.map { Renderer("${it.name}()", emptyList()) }
}
val subFunctions = properties.map { (sub, funcs) ->
renderProvider(options, faker, provider, sub, funcs, emptyMap())
}

val name = subProvider?.name ?: provider.name
val allFunctions = renderedFunctions.toList().plus(subFunctions)

return if (options.javaSyntax) {
val getterName = "get${provider.name.first().uppercase()}${provider.name.substring(1)}()"
Renderer(getterName, renderedFunctions.toList())
val getterName = "get${name.first().uppercase()}${name.substring(1)}()"
Renderer(getterName, allFunctions)
} else {
Renderer(provider.name, renderedFunctions.toList())
Renderer(name, allFunctions)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ object List : Runnable {
val introspector = Introspector(faker)

val renderedProviders = if (providerNames.isNotEmpty()) {
introspector.providerFunctions.asSequence().filter { (provider, _) ->
introspector.providerData.asSequence().filter { (provider, _) ->
providerNames.any { provider.name.lowercase().contains(it.lowercase() ) }
}.map { (provider, functions) ->
renderProvider(options, faker, provider, functions)
}.map { (provider, fpPair) ->
val (functions, properties) = fpPair
renderProvider(options, faker, provider, null, functions, properties)
}
} else {
introspector.providerFunctions.asSequence().map { (provider, functions) ->
renderProvider(options, faker, provider, functions)
introspector.providerData.asSequence().map { (provider, fpPair) ->
val (functions, properties) = fpPair
renderProvider(options, faker, provider, null, functions, properties)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@ object Lookup : Runnable {

val introspector = Introspector(faker)

val filteredMap: Map<KProperty<*>, Sequence<KFunction<*>>> = introspector.providerFunctions
.mapValuesTo(mutableMapOf()) { (_, v) ->
v.filter { it.name.lowercase().contains(functionName.lowercase()) }
}.filterValues { it.count() > 0 }
val filteredMap = introspector.providerData
.mapValuesTo(mutableMapOf()) { (_, fpPair) ->
val (functions, properties) = fpPair
functions.filter { it.toString().lowercase().contains(functionName.lowercase()) } to
properties.filter { (sub, funcs) ->
sub.toString().lowercase().contains(functionName.lowercase()) ||
funcs.any { f -> f.toString().lowercase().contains(functionName.lowercase()) }
}
}.filterValues { (funcs, subFuncs) ->
funcs.count() > 0 || subFuncs.isNotEmpty()
}

val renderedProviders = filteredMap.map { (provider, functions) ->
renderProvider(options, faker, provider, functions)
val renderedProviders = filteredMap.map { (provider, fpPair) ->
val (functions, properties) = fpPair
renderProvider(options, faker, provider, null, functions, properties)
}

val output = Renderer("Faker()", renderedProviders).toString()
Expand Down
Loading
Loading