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

Unique generation config #52

Merged
merged 16 commits into from
Dec 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Extract classes from UniqueDataProvider.kt
  • Loading branch information
serpro69 committed Dec 12, 2020
commit 8b43d21c5c35776f3d2b4954aff7038a47ebbfc2
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package io.github.serpro69.kfaker.provider.unique

import io.github.serpro69.kfaker.provider.FakeDataProvider
import io.github.serpro69.kfaker.Faker
import kotlin.reflect.KClass
import kotlin.reflect.KProperty0

/**
* Global provider for unique values.
*
* This provider is used in [Faker] class to control global unique generation configuration of faker providers.
*
* Example usage:
* ```
* val faker = Faker()
* faker.unique.enable(faker::address) // enables unique generation for all functions of Address provider
* ```
*/
@Suppress("UNCHECKED_CAST", "unused")
class GlobalUniqueDataDataProvider internal constructor() : UniqueDataProvider() {

@JvmSynthetic
@PublishedApi
override val config = UniqueProviderConfiguration()

// /**
// * A Set of [FakeDataProvider]s' [KClass]es that are configured to return unique values.
// */
// @PublishedApi
// @JvmSynthetic
// override val markedUnique = mutableSetOf<KClass<out FakeDataProvider>>()
//
// /**
// * A HashMap where the key is a [KClass] of [FakeDataProvider],
// * and values are Maps of provider's functionName to a set of already returned (used) values.
// */
// @PublishedApi
// @JvmSynthetic
// override val usedValues = hashMapOf<KClass<out FakeDataProvider>, MutableMap<String, MutableSet<String>>>()
//
// /**
// * A map of key=value pairs, where
// * A set of patterns which are matched against resolved values (before they are returned to the client)
// * to determine if the value should be returned or not.
// */
// @PublishedApi
// @JvmSynthetic
// internal val exclusionPatterns = hashMapOf<KClass<out FakeDataProvider>, MutableMap<String, MutableSet<Regex>>>()

/**
* Disables "unique generation" for all providers that were configured to return unique values,
* and clears out any already returned values, so they can possibly be returned again.
*/
override fun disableAll() {
config.markedUnique.clear()
config.usedValues.clear()
config.exclusionPatterns.clear()
}

/**
* Clears the already returned (used) unique values and exclusion patterns so that values can again be returned.
*/
override fun clearAll() {
config.usedValues.keys.forEach { k -> config.usedValues[k] = hashMapOf() }
config.exclusionPatterns.keys.forEach { k -> config.exclusionPatterns[k] = hashMapOf() }
}

@Deprecated(
level = DeprecationLevel.WARNING,
message = "This functionality is deprecated and will be removed in release 1.7.0",
replaceWith = ReplaceWith("faker.unique.configuration { this.exclude<T>(funcName, values) }")
)
inline fun <reified T : FakeDataProvider> exclude(funcName: String, values: List<String>) {
exclude<T>(funcName, *values.toTypedArray())
}

@Deprecated(
level = DeprecationLevel.WARNING,
message = "This functionality is deprecated and will be removed in release 1.7.0",
replaceWith = ReplaceWith("faker.unique.configuration { this.exclude<T>(funcName, values) }")
)
inline fun <reified T : FakeDataProvider> exclude(funcName: String, vararg values: String) {
if (config.markedUnique.contains(T::class)) {
config.usedValues[T::class]?.merge(funcName, values.toMutableSet()) { oldSet, newSet ->
oldSet.apply { addAll(newSet) }
}
}
}

// TODO remove - unused
// inline fun <reified T : FakeDataProvider> exclude(funcName: String, vararg patterns: Regex) {
// if (config.markedUnique.contains(T::class)) {
// config.exclusionPatterns[T::class]?.merge(funcName, patterns.toMutableSet()) { oldSet, newSet ->
// oldSet.apply { addAll(newSet) }
// }
// }
// }

@Deprecated(
level = DeprecationLevel.WARNING,
message = "This functionality is deprecated and will be removed in release 1.7.0",
replaceWith = ReplaceWith("faker.unique.configuration { this.enable(providerProperty) }")
)
fun <T : FakeDataProvider> enable(providerProperty: KProperty0<T>) {
config.enable(providerProperty.returnType.classifier as KClass<T>)
}

@Deprecated(
level = DeprecationLevel.WARNING,
message = "This functionality is deprecated and will be removed in release 1.7.0",
replaceWith = ReplaceWith("faker.unique.configuration { this.disable(providerProperty) }")
)
fun <T : FakeDataProvider> disable(providerProperty: KProperty0<T>) {
config.disable(providerProperty.returnType.classifier as KClass<T>)
}

fun <T : FakeDataProvider> clear(providerProperty: KProperty0<T>) {
config.clear(providerProperty.returnType.classifier as KClass<T>)
}

// TODO remove - unused
// @PublishedApi
// @JvmSynthetic
// internal fun <T : FakeDataProvider> enable(provider: KClass<out T>) {
// if (!markedUnique.contains(provider)) {
// markedUnique.add(provider).also {
// usedValues[provider] = hashMapOf()
// exclusionPatterns[provider] = hashMapOf()
// }
// }
// }

// TODO remove - unused
// private fun <T : FakeDataProvider> disable(provider: KClass<out T>) {
// if (markedUnique.contains(provider)) {
// markedUnique.remove(provider).also {
// usedValues.remove(provider)
// exclusionPatterns.remove(provider)
// }
// }
// }
//
private fun <T : FakeDataProvider> clear(provider: KClass<out T>) {
if (config.markedUnique.contains(provider)) {
config.usedValues[provider] = hashMapOf()
config.exclusionPatterns[provider] = hashMapOf()
}
}

/**
* Configures `this` Unique provider.
*/
fun configuration(function: UniqueProviderConfiguration.() -> Unit) {
config.apply(function)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package io.github.serpro69.kfaker.provider.unique

import io.github.serpro69.kfaker.provider.AbstractFakeDataProvider
import io.github.serpro69.kfaker.provider.FakeDataProvider
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KClass
import kotlin.reflect.KProperty
import kotlin.reflect.full.memberProperties
import kotlin.reflect.full.primaryConstructor
import kotlin.reflect.jvm.javaField

/**
* Local provider for unique values.
*
* This provider is used in [T] implementation of [FakeDataProvider] class,
* and controls unique generation configuration of [T]'s functions.
*
* Example usage:
* ```
* Faker().address.unique().country()
* ```
*/
@Suppress("UNCHECKED_CAST", "unused")
class LocalUniqueDataProvider<T : FakeDataProvider> internal constructor() : UniqueDataProvider() {
override val config: UniqueProviderConfiguration
get() = TODO("Not yet implemented")
// override val markedUnique: MutableSet<FakeDataProvider> = mutableSetOf()
// override val usedValues = hashMapOf<String, MutableSet<String>>()

val markedUnique: MutableSet<FakeDataProvider> = mutableSetOf()
val usedValues = hashMapOf<String, MutableSet<String>>()

/**
* In `this` class the function works the same as [clearAll] implementation.
*/
override fun disableAll() {
clearAll()
}

override fun clearAll() {
usedValues.keys.forEach { k ->
usedValues[k] = mutableSetOf()
}
}

/**
* Clears the already returned (used) unique values for the function with provided [name].
*
* Example usage:
* ```
* address.unique.clear("country") // clear (reset) unique values for 'country' function
* ```
*/
fun clear(name: String) {
usedValues[name] = mutableSetOf()
}
}

/**
* Delegate class for [LocalUniqueDataProvider] used to return local providers that generate unique values.
*
* @param T an implementation of [AbstractFakeDataProvider]
*
* @property uniqueDataProvider [LocalUniqueDataProvider] of [T] type.
*/
@Suppress("UNCHECKED_CAST")
class UniqueProviderDelegate<T : AbstractFakeDataProvider<*>>(
private val uniqueDataProvider: LocalUniqueDataProvider<T>
) : ReadOnlyProperty<T, T> {

override fun getValue(thisRef: T, property: KProperty<*>): T {
return if (uniqueDataProvider.markedUnique.any { it::class == thisRef::class }) {
uniqueDataProvider.markedUnique.first { it::class == thisRef::class } as T
} else {
val cls = property.returnType.classifier as KClass<T>
val prop = cls.memberProperties.first { it.name == "localUniqueDataProvider" }
val newRef = requireNotNull(cls.primaryConstructor?.call(thisRef.fakerService))
prop.javaField?.let {
it.isAccessible = true
it.set(newRef, uniqueDataProvider)
uniqueDataProvider.markedUnique.add(newRef)
newRef
} ?: throw NoSuchElementException("Unable to get java field for property $prop")
}
}
}
Loading