-
Notifications
You must be signed in to change notification settings - Fork 0
Reorganize folder structure: group Resolvers with their related Strategies #73
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import os | ||
| import re | ||
|
|
||
| def fix_file(filepath): | ||
| if not os.path.isfile(filepath): return | ||
| with open(filepath, 'r') as f: | ||
| content = f.read() | ||
|
|
||
| # Replace the actual path in the file | ||
| new_content = content | ||
| # We used 'klass', 'obj', 'enm' in the directories. Let's stick with them. | ||
| # The error message said e.g. e: file:///app/src/main/kotlin/dev/appoutlet/some/config/SomeConfig.kt:11:36 Unresolved reference 'class'. | ||
| # This is because I imported dev.appoutlet.some.resolver.klass.ClassResolver but maybe SomeConfig was using 'class' somewhere? | ||
|
|
||
| # Wait, I see: | ||
| # e: file:///app/src/main/kotlin/dev/appoutlet/some/config/SomeConfig.kt:11:36 Unresolved reference 'class'. | ||
|
|
||
| # Let's check that line in SomeConfig.kt | ||
| return new_content | ||
|
|
||
| # (script incomplete, just checking) | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,110 +1,56 @@ | ||||||||||||||||||||||||||
| package dev.appoutlet.some | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| import dev.appoutlet.some.config.NullableStrategy | ||||||||||||||||||||||||||
| import dev.appoutlet.some.config.SomeConfig | ||||||||||||||||||||||||||
| import dev.appoutlet.some.config.SomeConfigBuilder | ||||||||||||||||||||||||||
| import dev.appoutlet.some.config.buildSomeConfig | ||||||||||||||||||||||||||
| import dev.appoutlet.some.core.ResolverChain | ||||||||||||||||||||||||||
| import dev.appoutlet.some.core.TypeResolver | ||||||||||||||||||||||||||
| import dev.appoutlet.some.core.get | ||||||||||||||||||||||||||
| import dev.appoutlet.some.resolver.nullable.NullableStrategy | ||||||||||||||||||||||||||
| import kotlin.random.Random | ||||||||||||||||||||||||||
| import kotlin.reflect.typeOf | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * Fixture generator configured with a resolver chain and shared random source. | ||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||
| * Instances are created by [someSetup] and can be reused to generate multiple values with the same configuration. | ||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||
| * @param resolvers Ordered resolver list used to generate values. | ||||||||||||||||||||||||||
| * @param random Random source shared by resolvers created for this instance. | ||||||||||||||||||||||||||
| * @param config Configuration used by this generator. | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| class Some( | ||||||||||||||||||||||||||
| val resolvers: List<TypeResolver>, | ||||||||||||||||||||||||||
| val random: Random, | ||||||||||||||||||||||||||
| val config: SomeConfig | ||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * Generates a fixture value of type [T] using this instance's configuration. | ||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||
| * @param T Type to generate. | ||||||||||||||||||||||||||
| * @return Generated value of type [T]. | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| @Suppress("MemberNameEqualsClassName") | ||||||||||||||||||||||||||
| inline fun <reified T> some(): T { | ||||||||||||||||||||||||||
| val nullableStrategy = config[NullableStrategy::class] | ||||||||||||||||||||||||||
| val session = ResolverChain(resolvers, nullableStrategy) | ||||||||||||||||||||||||||
| return session.resolve(typeOf<T>()) as T | ||||||||||||||||||||||||||
| inline fun <reified T : Any> some(): T { | ||||||||||||||||||||||||||
| val strategy = config.get<NullableStrategy>() ?: NullableStrategy.default | ||||||||||||||||||||||||||
| val chain = ResolverChain(resolvers, strategy) | ||||||||||||||||||||||||||
| return chain.resolve(typeOf<T>()) as T | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
12
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Breaking Changes & Nullability Regression
class Some(
val resolvers: List<TypeResolver>,
val random: Random,
val config: SomeConfig
) {
inline fun <reified T> some(): T {
val strategy = config.get<NullableStrategy>() ?: NullableStrategy.default
val chain = ResolverChain(resolvers, strategy)
return chain.resolve(typeOf<T>()) as T
}
inline operator fun <reified T> invoke(): T = some()
inline operator fun <reified T> invoke(noinline configuration: SomeConfigBuilder.() -> Unit = {}): T {
val aggregatedConfig = this.config.toBuilder().apply(configuration).build()
return Some(aggregatedConfig.buildResolvers(random), random, aggregatedConfig).some()
}
} |
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * Generates a fixture value of type [T]. | ||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||
| * Enables concise usage such as `some<User>()` when `some` is a [Some] instance. | ||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||
| * @param T Type to generate. | ||||||||||||||||||||||||||
| * @return Generated value of type [T]. | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| inline operator fun <reified T> invoke(): T = some() | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * Generates a fixture value of type [T] with per-call configuration overrides. | ||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||
| * Overrides are applied to a copy of this instance's configuration and do not mutate the original [Some]. | ||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||
| * @param T Type to generate. | ||||||||||||||||||||||||||
| * @param config Configuration overrides for this call. | ||||||||||||||||||||||||||
| * @return Generated value of type [T]. | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| inline operator fun <reified T> invoke(noinline config: SomeConfigBuilder.() -> Unit = {}): T { | ||||||||||||||||||||||||||
| val aggregatedConfig = this.config.toBuilder().apply(config).build() | ||||||||||||||||||||||||||
| return Some(aggregatedConfig.buildResolvers(random), random, aggregatedConfig).some() | ||||||||||||||||||||||||||
| inline fun <reified T : Any> some(noinline configuration: (SomeConfigBuilder.() -> Unit)? = null): T { | ||||||||||||||||||||||||||
| val config = if (configuration != null) { | ||||||||||||||||||||||||||
| buildSomeConfig(configuration) | ||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||
| defaultSomeConfig | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| val some = Some(config.buildResolvers(), config.buildRandom(), config) | ||||||||||||||||||||||||||
| return some.some<T>() | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
+24
to
32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Performance Regression & Nullability Constraint
inline fun <reified T> some(noinline configuration: (SomeConfigBuilder.() -> Unit)? = null): T {
if (configuration == null) {
return someDefault<T>()
}
val config = buildSomeConfig(configuration)
val some = Some(config.buildResolvers(), config.buildRandom(), config)
return some.some<T>()
} |
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * Creates a reusable [Some] generator. | ||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||
| * Use this when multiple fixtures should share the same configuration and random source. | ||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||
| * @param config Configuration applied to the created generator. | ||||||||||||||||||||||||||
| * @return A configured [Some] instance. | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| fun someSetup(config: SomeConfigBuilder.() -> Unit = {}): Some { | ||||||||||||||||||||||||||
| val someConfig = buildSomeConfig(config) | ||||||||||||||||||||||||||
| val random = someConfig.buildRandom() | ||||||||||||||||||||||||||
| return Some(someConfig.buildResolvers(random), random, someConfig) | ||||||||||||||||||||||||||
| fun someSetup(configuration: SomeConfigBuilder.() -> Unit = {}): Some { | ||||||||||||||||||||||||||
| val config = buildSomeConfig(configuration) | ||||||||||||||||||||||||||
| return Some(config.buildResolvers(), config.buildRandom(), config) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * Lazily-created default configuration used by top-level [some]. | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| val defaultConfig: SomeConfig by lazy { SomeConfig() } | ||||||||||||||||||||||||||
| @PublishedApi | ||||||||||||||||||||||||||
| internal val defaultSomeConfig: SomeConfig by lazy { buildSomeConfig() } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * Lazily-created default resolver chain used by top-level [some]. | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| val defaultResolvers: List<TypeResolver> by lazy { defaultConfig.buildResolvers() } | ||||||||||||||||||||||||||
| @PublishedApi | ||||||||||||||||||||||||||
| internal val defaultResolvers: List<TypeResolver> by lazy { | ||||||||||||||||||||||||||
| defaultSomeConfig.buildResolvers() | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * Generates a fixture value using the default configuration. | ||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||
| * @param T Type to generate. | ||||||||||||||||||||||||||
| * @return Generated value of type [T]. | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| inline fun <reified T> some(): T { | ||||||||||||||||||||||||||
| val nullableStrategy = defaultConfig[NullableStrategy::class] | ||||||||||||||||||||||||||
| val session = ResolverChain(defaultResolvers, nullableStrategy) | ||||||||||||||||||||||||||
| return session.resolve(typeOf<T>()) as T | ||||||||||||||||||||||||||
| @PublishedApi | ||||||||||||||||||||||||||
| internal inline fun <reified T : Any> someDefault(): T { | ||||||||||||||||||||||||||
| val strategy = defaultSomeConfig.get<NullableStrategy>() ?: NullableStrategy.default | ||||||||||||||||||||||||||
| val chain = ResolverChain(defaultResolvers, strategy) | ||||||||||||||||||||||||||
| return chain.resolve(typeOf<T>()) as T | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
+47
to
52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nullability Constraint RegressionRemove the
Suggested change
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * Generates a fixture value using one-off configuration overrides. | ||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||
| * @param T Type to generate. | ||||||||||||||||||||||||||
| * @param config Configuration applied only to this generation call. | ||||||||||||||||||||||||||
| * @return Generated value of type [T]. | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| inline fun <reified T> some(noinline config: SomeConfigBuilder.() -> Unit = {}): T { | ||||||||||||||||||||||||||
| val someSetup = someSetup(config) | ||||||||||||||||||||||||||
| return someSetup.some<T>() | ||||||||||||||||||||||||||
| fun buildSomeConfig(configuration: SomeConfigBuilder.() -> Unit = {}): SomeConfig { | ||||||||||||||||||||||||||
| return SomeConfigBuilder().apply(configuration).build() | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incomplete Script
This script is incomplete, unused, and contains commented-out thoughts. It should be removed from the repository to maintain a clean codebase.