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
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
Add kdocs for UniqueDataProvider
  • Loading branch information
serpro69 committed Dec 12, 2020
commit 398bce8856870d9ab43830dd0d530b6fd3d6e044
Original file line number Diff line number Diff line change
@@ -1,34 +1,77 @@
package io.github.serpro69.kfaker.provider

import kotlin.properties.*
import kotlin.reflect.*
import kotlin.reflect.full.*
import kotlin.reflect.jvm.*
import io.github.serpro69.kfaker.Faker
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KClass
import kotlin.reflect.KProperty
import kotlin.reflect.KProperty0
import kotlin.reflect.full.memberProperties
import kotlin.reflect.full.primaryConstructor
import kotlin.reflect.jvm.javaField

abstract class UniqueDataProvider {
/**
* 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.
*/
abstract fun disableAll()

/**
* Clears the already returned (used) unique values so that they can again be returned.
*/
abstract fun clearAll()
}

/**
* 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 : UniqueDataProvider() {
/**
* 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>>>()

/**
* 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() {
markedUnique.clear()
usedValues.clear()
}

/**
* Clears the already returned (used) unique values so that they can again be returned.
*/
override fun clearAll() {
usedValues.keys.forEach { k ->
usedValues[k] = hashMapOf()
Expand Down Expand Up @@ -84,11 +127,25 @@ class GlobalUniqueDataDataProvider : UniqueDataProvider() {
}
}

/**
* 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> : UniqueDataProvider() {
override val markedUnique: MutableSet<FakeDataProvider> = mutableSetOf()
override val usedValues = hashMapOf<String, MutableSet<String>>()

/**
* In `this` class the function works the same as [clearAll] implementation.
*/
override fun disableAll() {
clearAll()
}
Expand All @@ -99,11 +156,26 @@ class LocalUniqueDataProvider<T : FakeDataProvider> : UniqueDataProvider() {
}
}

/**
* 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>
Expand Down