Skip to content

Commit

Permalink
Remove deprecated Faker.Builder#config fun
Browse files Browse the repository at this point in the history
resolves #80
  • Loading branch information
serpro69 committed Oct 3, 2021
1 parent 15bc80d commit abe4eaa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
37 changes: 24 additions & 13 deletions core/src/main/kotlin/io/github/serpro69/kfaker/Faker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -197,24 +197,35 @@ class Faker @JvmOverloads constructor(internal val config: FakerConfig = fakerCo
val zelda: Zelda = Zelda(fakerService)

@FakerDsl
/**
* DSL builder for creating instances of [Faker]
*/
class Builder internal constructor(){
private var config = io.github.serpro69.kfaker.fakerConfig { }
/**
* @property config faker configuration for the [Faker] instance
* which will be created with this [Faker.Builder].
*/
private var config: FakerConfig = io.github.serpro69.kfaker.fakerConfig { }

@Deprecated(
message = "This API is unstable and might change in the final 1.8.0 release.",
level = DeprecationLevel.WARNING,
replaceWith = ReplaceWith(expression = "fakerConfig{ }")
)
fun config(block: FakerConfig.Builder.() -> Unit) {
/**
* Sets [config] configuration for this [Faker.Builder]
* using the results of the [block] function.
*
* This [config] will then be used when an instance of [Faker] is created using this [Faker.Builder]
*/
fun fakerConfig(block: ConfigBuilder) {
config = io.github.serpro69.kfaker.fakerConfig(block)
}

fun fakerConfig(block: FakerConfig.Builder.() -> Unit) {
config = io.github.serpro69.kfaker.fakerConfig(block)
}

internal fun build() = Faker(config)
/**
* Builds an instance of [Faker] with this [config].
*/
internal fun build(): Faker = Faker(config)
}
}

fun faker(block: Faker.Builder.() -> Unit) = Faker.Builder().apply(block).build()
/**
* Applies the the [block] function to [Faker.Builder]
* and returns as an instance of [Faker] from that builder.
*/
fun faker(block: Faker.Builder.() -> Unit): Faker = Faker.Builder().apply(block).build()
15 changes: 12 additions & 3 deletions core/src/main/kotlin/io/github/serpro69/kfaker/FakerConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,18 @@ class FakerConfig private constructor(
ReplaceWith("fakerConfig { }"),
level = DeprecationLevel.WARNING
)
fun FakerConfig.Builder.create(block: FakerConfig.Builder.() -> Unit) = this.apply(block).build()
fun FakerConfig.Builder.create(block: ConfigBuilder): FakerConfig = this.apply(block).build()

/**
* Creates an instance of [FakerConfig] with the config properties that were passed to the function [block].
* Applies the the [block] function to [ConfigBuilder]
* and returns as an instance of [FakerConfig] from that builder.
*/
fun fakerConfig(block: FakerConfig.Builder.() -> Unit) = FakerConfig.Builder().apply(block).build()
fun fakerConfig(block: ConfigBuilder): FakerConfig = FakerConfig.Builder().apply(block).build()

/**
* Lambda with [FakerConfig.Builder] receiver type that returns a [Unit].
*
* Used with DSL functions to construct an instance of [FakerConfig]
* by applying the results of the function to the [FakerConfig.Builder].
*/
typealias ConfigBuilder = FakerConfig.Builder.() -> Unit

0 comments on commit abe4eaa

Please sign in to comment.