Skip to content

Commit

Permalink
Remove unnecessary check for key presense
Browse files Browse the repository at this point in the history
Relates to #243
  • Loading branch information
serpro69 committed Jun 30, 2024
1 parent baa41bb commit 890991b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
[discrete]
=== Added

* https://github.com/serpro69/kotlin-faker/pull/243[#243] (:core) Add collection element and map k/v type gen for random class instance
* https://github.com/serpro69/kotlin-faker/pull/234[#234] (:extension) Add extension module for kotest property testing
* https://github.com/serpro69/kotlin-faker/pull/232[#232] (:core) Add support for alternative primary key when resolving values
* https://github.com/serpro69/kotlin-faker/pull/227[#227] Add BOM to manage faker versions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,44 +235,28 @@ class RandomClassProvider {
): Any? {
return when (this) {
List::class, Set::class -> {
val instance: (el: KClass<*>) -> Any? = {
when {
config.collectionElementTypeGenerators.containsKey(it) -> {
config.collectionElementTypeGenerators[it]?.invoke(pInfo)
}
else -> it.randomClassInstance(config)
}
}
val elementType = kType.arguments[0].type?.classifier as KClass<*>
val r = List(config.collectionsSize) { instance(elementType) }
val r = List(config.collectionsSize) {
config.collectionElementTypeGenerators[elementType]?.invoke(pInfo)
?: elementType.randomClassInstance(config)
}
when (this) {
List::class -> r
Set::class -> r.toSet()
else -> throw UnsupportedOperationException("Collection type $this is not supported")
else -> throw UnsupportedOperationException("$this collection type is not supported")
}
}
Map::class -> {
val keyElementType = kType.arguments[0].type?.classifier as KClass<*>
val valElementType = kType.arguments[1].type?.classifier as KClass<*>
val keys = List(config.collectionsSize) {
when {
// TODO this should be optimized to not look up key presence on each invocation
config.mapEntriesTypeGenerators.first.containsKey(keyElementType) -> {
config.mapEntriesTypeGenerators.first[keyElementType]?.invoke(pInfo)
}
else -> keyElementType.randomClassInstance(config)
}
config.mapEntriesTypeGenerators.first[keyElementType]?.invoke(pInfo)
?: keyElementType.randomClassInstance(config)
}
val values = List(config.collectionsSize) {
when {
// TODO this should be optimized to not look up key presence on each invocation
config.mapEntriesTypeGenerators.second.containsKey(valElementType) -> {
config.mapEntriesTypeGenerators.second[valElementType]?.invoke(pInfo)
}
else -> valElementType.randomClassInstance(config)
}
keys.associateWith {
config.mapEntriesTypeGenerators.second[valElementType]?.invoke(pInfo)
?: valElementType.randomClassInstance(config)
}
keys.zip(values).associate { (k, v) -> k to v }
}
else -> null
}
Expand Down

0 comments on commit 890991b

Please sign in to comment.