All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.app.cash.backfila.client.dynamodb.DynamoDbBackfillModule.kt Maven / Gradle / Ivy

package app.cash.backfila.client.dynamodb

import app.cash.backfila.client.dynamodb.internal.AwsAttributeValueAdapter
import app.cash.backfila.client.dynamodb.internal.DynamoDbBackend
import app.cash.backfila.client.spi.BackfillBackend
import com.google.inject.AbstractModule
import com.google.inject.Binder
import com.google.inject.Provides
import com.google.inject.Singleton
import com.google.inject.TypeLiteral
import com.google.inject.multibindings.MapBinder
import com.google.inject.multibindings.Multibinder
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import javax.inject.Qualifier
import kotlin.reflect.KClass
import kotlin.reflect.jvm.jvmName

class DynamoDbBackfillModule> private constructor(
  private val backfillClass: KClass,
) : AbstractModule() {
  override fun configure() {
    install(DynamoDbBackfillBackendModule)
    // Ensures that the backfill class is injectable. If you are failing this check you probably
    // want to add an @Inject annotation to your class or check that all of your dependencies are provided.
    binder().getProvider(backfillClass.java)
    mapBinder(binder()).addBinding(backfillClass.jvmName).toInstance(backfillClass)
  }

  companion object {
    inline fun > create(): DynamoDbBackfillModule = create(T::class)

    @JvmStatic
    fun > create(backfillClass: KClass): DynamoDbBackfillModule {
      return DynamoDbBackfillModule(backfillClass)
    }

    @JvmStatic
    fun > create(backfillClass: Class): DynamoDbBackfillModule {
      return DynamoDbBackfillModule(backfillClass.kotlin)
    }
  }
}

/**
 * This is a kotlin object so these dependencies are only installed once.
 */
private object DynamoDbBackfillBackendModule : AbstractModule() {
  override fun configure() {
    Multibinder.newSetBinder(binder(), BackfillBackend::class.java).addBinding()
      .to(DynamoDbBackend::class.java)
  }

  /** The DynamoDb Backend needs a modified Moshi */
  @Provides @Singleton @ForDynamoDbBackend
  fun provideDynamoMoshi(): Moshi {
    return Moshi.Builder()
      .add(AwsAttributeValueAdapter)
      .add(KotlinJsonAdapterFactory()) // Must be last.
      .build()
  }
}

private fun mapBinder(binder: Binder) = MapBinder.newMapBinder(
  binder,
  object : TypeLiteral() {},
  object : TypeLiteral>>() {},
  ForDynamoDbBackend::class.java,
)

/** Annotation for specifying dependencies specifically for this Backend. */
@Qualifier annotation class ForDynamoDbBackend




© 2015 - 2024 Weber Informatics LLC | Privacy Policy