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
Remove unused code from provider/unique/* classes
  • Loading branch information
serpro69 committed Dec 12, 2020
commit 53297128bce9e25bfaf8ec59699540deec42c01f
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,24 @@ import kotlin.reflect.KProperty0
* Example usage:
* ```
* val faker = Faker()
* faker.unique.enable(faker::address) // enables unique generation for all functions of Address provider
* faker.unique.configuration {
* enable(faker::address) // enables unique generation for all functions of Address provider
* enable(faker::name) // enables unique generation for all functions of Name provider
* exclude(listOfValues) // exclude values from `listOfValue` collection from being generated (for all providers that are enabled for unique generation)
* enable(faker::internet) { enables unique generation for all functions of Internet provider
* exclude<Internet>(listOfValues) // exclude values from `listOfValue` collection from being generated with Internet provider
* exclude(faker::internet, listOfPatterns)
* }
* }
* ```
*/
@Suppress("UNCHECKED_CAST", "unused")
@Suppress("UNCHECKED_CAST")
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.
Expand All @@ -65,6 +49,10 @@ class GlobalUniqueDataDataProvider internal constructor() : UniqueDataProvider()
config.exclusionPatterns.keys.forEach { k -> config.exclusionPatterns[k] = hashMapOf() }
}

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

@Deprecated(
level = DeprecationLevel.WARNING,
message = "This functionality is deprecated and will be removed in release 1.7.0",
Expand All @@ -87,15 +75,6 @@ class GlobalUniqueDataDataProvider internal constructor() : UniqueDataProvider()
}
}

// 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",
Expand All @@ -114,39 +93,6 @@ class GlobalUniqueDataDataProvider internal constructor() : UniqueDataProvider()
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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@ package io.github.serpro69.kfaker.provider.unique
abstract class UniqueDataProvider {
internal abstract val config: UniqueProviderConfiguration

// /**
// * A set of providers that are configured to return unique values.
// */
// internal abstract val markedUnique: Set<*>
//
// /**
// * A map of key=value pairs representing already returned (used) values
// * which will not be returned again.
// */
// internal abstract val usedValues: Map<*, *>

/**
* Disables "unique generation" for all providers that were configured to return unique values.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.github.serpro69.kfaker.provider.unique

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

/**
* Provides configuration for Unique Generation of values.
Expand Down Expand Up @@ -175,6 +175,18 @@ class UniqueProviderConfiguration @PublishedApi internal constructor() {
}
}

/**
* Exclude a values from being generated by [Regex] patterns in provider [T].
*
* @param T an implementation class of [FakeDataProvider], for example [Address]
* @param func a function that does not take any arguments and returns a [List] of [Regex]es
*/
@JvmName("excludePatternsFor")
inline fun <reified T : FakeDataProvider> excludePatterns(func: () -> List<Regex>) {
TODO("Not implemented")
patterns.addAll(func.invoke())
}

/**
* Exclude a values from being generated by [Regex] patterns
* when a [providerFunction] in provider [T] is called
Expand All @@ -188,24 +200,28 @@ class UniqueProviderConfiguration @PublishedApi internal constructor() {
providerFunction: KFunction1<T, String>,
func: () -> List<Regex>
) {
TODO("Not implemented")
patterns.addAll(func.invoke())
}

/**
* Exclude [values] from being generated.
* This applies to all providers that are enabled for unique value generation.
*
* This applies to ALL providers that are enabled for unique value generation.
*/
fun exclude(values: List<String>) {
TODO("Not implemented")
}

/**
* Exclude a values from being generated by [Regex] patterns.
* This applies to all providers that are enabled for unique value generation.
*
* This applies to ALL providers that are enabled for unique value generation.
*
* @param func a function that does not take any arguments and returns a [List] of [Regex]es
*/
fun excludePatterns(func: () -> List<Regex>) {
TODO("Not implemented")
patterns.addAll(func.invoke())
}
}