-
Notifications
You must be signed in to change notification settings - Fork 62
Make parameters classes easier to work with #509
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
base: master
Are you sure you want to change the base?
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 |
---|---|---|
|
@@ -18,12 +18,13 @@ import kotlin.reflect.KClass | |
import kotlin.reflect.full.findAnnotation | ||
|
||
@Singleton | ||
class FixedSetBackend @Inject constructor( | ||
class FixedSetBackend | ||
@Inject | ||
constructor( | ||
private val injector: Injector, | ||
@ForFixedSetBackend private val backfills: MutableMap<String, KClass<out FixedSetBackfill<*>>>, | ||
private val datastore: FixedSetDatastore, | ||
) : BackfillBackend { | ||
|
||
/** Creates Backfill instances. Each backfill ID gets a new Backfill instance. */ | ||
private fun getBackfill(name: String): FixedSetBackfill<*>? { | ||
val backfillClass = backfills[name] | ||
|
@@ -34,13 +35,16 @@ class FixedSetBackend @Inject constructor( | |
} | ||
} | ||
|
||
private fun <Param : Any> createOperator( | ||
backfill: FixedSetBackfill<Param>, | ||
) = FixedSetBackfillOperator( | ||
backfill = backfill, | ||
datastore = datastore, | ||
parametersOperator = BackfilaParametersOperator(parametersClass(backfill::class)), | ||
) | ||
@Suppress("UNCHECKED_CAST") | ||
private fun <Param : Any> createOperator(backfill: FixedSetBackfill<Param>) = | ||
FixedSetBackfillOperator( | ||
backfill = backfill, | ||
datastore = datastore, | ||
parametersOperator = | ||
BackfilaParametersOperator( | ||
parametersClass(backfill::class), | ||
) as BackfilaParametersOperator<Param>, | ||
) | ||
|
||
override fun create(backfillName: String): BackfillOperator? { | ||
val backfill = getBackfill(backfillName) | ||
|
@@ -53,26 +57,26 @@ class FixedSetBackend @Inject constructor( | |
return null | ||
} | ||
|
||
override fun backfills(): Set<BackfillRegistration> { | ||
return backfills.map { | ||
BackfillRegistration( | ||
name = it.key, | ||
description = it.value.findAnnotation<Description>()?.text, | ||
parametersClass = parametersClass(it.value as KClass<FixedSetBackfill<Any>>), | ||
deleteBy = it.value.findAnnotation<DeleteBy>()?.parseDeleteByDate(), | ||
unit = BackfillUnit.ITEMS.displayName, | ||
) | ||
}.toSet() | ||
} | ||
override fun backfills(): Set<BackfillRegistration> = | ||
backfills | ||
.map { | ||
BackfillRegistration( | ||
name = it.key, | ||
description = it.value.findAnnotation<Description>()?.text, | ||
parametersClass = parametersClass(it.value), | ||
deleteBy = it.value.findAnnotation<DeleteBy>()?.parseDeleteByDate(), | ||
unit = BackfillUnit.ITEMS.displayName, | ||
) | ||
}.toSet() | ||
|
||
private fun <T : Any> parametersClass(backfillClass: KClass<out FixedSetBackfill<T>>): KClass<T> { | ||
private fun parametersClass(backfillClass: KClass<out FixedSetBackfill<*>>): KClass<*> { | ||
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. Yeah I don't think that removing this anchoring to the type is what I'm looking for. I don't want parameters to be able to be any type for a particular backfill but only the parameterized type for that specific backfill. 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. You still get that here. Try it for yourself, the types still have to match up |
||
// Like MyBackfill. | ||
val thisType = TypeLiteral.get(backfillClass.java) | ||
|
||
// Like Backfill<MyDataClass>. | ||
val supertype = thisType.getSupertype(FixedSetBackfill::class.java).type as ParameterizedType | ||
|
||
// Like MyDataClass | ||
return (Types.getRawType(supertype.actualTypeArguments[0]) as Class<T>).kotlin | ||
return (Types.getRawType(supertype.actualTypeArguments[0])).kotlin | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,18 +36,21 @@ class JooqBackend @Inject constructor( | |
BackfillRegistration( | ||
name = it.key, | ||
description = it.value.findAnnotation<Description>()?.text, | ||
parametersClass = parametersClass(it.value as KClass<JooqBackfill<*, Any>>), | ||
parametersClass = parametersClass(it.value), | ||
deleteBy = it.value.findAnnotation<DeleteBy>()?.parseDeleteByDate(), | ||
unit = BackfillUnit.ITEMS.displayName, | ||
) | ||
}.toSet() | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
private fun <K : Any, Param : Any> createJooqOperator( | ||
backfill: JooqBackfill<K, Param>, | ||
) = JooqBackfillOperator( | ||
backfill, | ||
BackfilaParametersOperator(parametersClass(backfill::class)), | ||
BackfilaParametersOperator( | ||
parametersClass(backfill::class), | ||
) as BackfilaParametersOperator<Param>, | ||
) | ||
|
||
/** Creates Backfill instances. Each backfill ID gets a new Backfill instance. */ | ||
|
@@ -60,14 +63,14 @@ class JooqBackend @Inject constructor( | |
} | ||
} | ||
|
||
private fun <T : Any> parametersClass(backfillClass: KClass<out JooqBackfill<*, T>>): KClass<T> { | ||
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. Does removing the generic mean the type isn't known at the callsite? 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. Before, we were always casting the type to |
||
private fun parametersClass(backfillClass: KClass<out JooqBackfill<*, *>>): KClass<*> { | ||
// Like MyBackfill. | ||
val thisType = TypeLiteral.get(backfillClass.java) | ||
|
||
// Like Backfill<Key, MyDataClass>. | ||
val supertype = thisType.getSupertype(JooqBackfill::class.java).type as ParameterizedType | ||
|
||
// Like MyDataClass | ||
return (Types.getRawType(supertype.actualTypeArguments[1]) as Class<T>).kotlin | ||
return (Types.getRawType(supertype.actualTypeArguments[1])).kotlin | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,18 +35,20 @@ class S3DatasourceBackend @Inject constructor( | |
} | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
private fun <R : Any, Param : Any> createS3DatasourceOperator( | ||
backfill: S3DatasourceBackfill<R, Param>, | ||
) = S3DatasourceBackfillOperator( | ||
backfill, | ||
BackfilaParametersOperator(parametersClass(backfill::class)), | ||
BackfilaParametersOperator( | ||
parametersClass(backfill::class), | ||
) as BackfilaParametersOperator<Param>, | ||
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. Why do we end up needing to cast here and didn't before? 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. We were casting to |
||
s3Service, | ||
) | ||
|
||
override fun create(backfillName: String): BackfillOperator? { | ||
return getBackfill(backfillName)?.let { backfill -> | ||
@Suppress("UNCHECKED_CAST") // We don't know the types statically, so fake them. | ||
return createS3DatasourceOperator(backfill as S3DatasourceBackfill<Any, Any>) | ||
return createS3DatasourceOperator(backfill) | ||
} | ||
} | ||
|
||
|
@@ -55,21 +57,21 @@ class S3DatasourceBackend @Inject constructor( | |
BackfillRegistration( | ||
name = it.key, | ||
description = it.value.findAnnotation<Description>()?.text, | ||
parametersClass = parametersClass(it.value as KClass<S3DatasourceBackfill<Any, Any>>), | ||
parametersClass = parametersClass(it.value), | ||
deleteBy = it.value.findAnnotation<DeleteBy>()?.parseDeleteByDate(), | ||
unit = BackfillUnit.BYTES.displayName, | ||
) | ||
}.toSet() | ||
} | ||
|
||
private fun <R : Any, P : Any> parametersClass(backfillClass: KClass<out S3DatasourceBackfill<R, P>>): KClass<P> { | ||
private fun parametersClass(backfillClass: KClass<out S3DatasourceBackfill<*, *>>): KClass<*> { | ||
// Like MyBackfill. | ||
val thisType = TypeLiteral.get(backfillClass.java) | ||
|
||
// Like S3DatasourceBackfill<MyParameterClass>. | ||
val supertype = thisType.getSupertype(S3DatasourceBackfill::class.java).type as ParameterizedType | ||
|
||
// Like MyParameterClass | ||
return (Types.getRawType(supertype.actualTypeArguments[1]) as Class<P>).kotlin | ||
return (Types.getRawType(supertype.actualTypeArguments[1])).kotlin | ||
} | ||
} |
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.
Do we need this? I don't know that I like casting the operator, I'd rather cast the Param type exclusively.
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.
Before we were always creating this with a parameters class of
Any
. Here we are casting to the actual parameters type