Skip to content
Permalink
Browse files

Add misk-guice module containing former :misk:misk misk.inject package (

  • Loading branch information...
jjestrel committed Jul 24, 2019
1 parent d6408fb commit 0e2fde89f212077b56606482482970b1b41f5845
@@ -83,7 +83,8 @@ class HibernateModule(
// Bind DataSourceService.
val dataSourceDecoratorsKey = setOfType(DataSourceDecorator::class).toKey(qualifier)
val dataSourceDecoratorsProvider = getProvider(dataSourceDecoratorsKey)
bind(keyOf<DataSource>(qualifier)).toProvider(keyOf<DataSourceService>(qualifier)).asSingleton()
bind(keyOf<DataSource>(qualifier)).toProvider(
keyOf<DataSourceService>(qualifier)).asSingleton()
bind(keyOf<DataSourceService>(qualifier)).toProvider(object : Provider<DataSourceService> {
@com.google.inject.Inject(optional = true) var metrics: Metrics? = null
override fun get() = DataSourceService(
@@ -135,7 +136,8 @@ class HibernateModule(
install(ServiceModule<SchemaMigratorService>(qualifier))

// Bind SchemaValidatorService.
val sessionFactoryServiceProvider = getProvider(keyOf<SessionFactoryService>(qualifier))
val sessionFactoryServiceProvider = getProvider(
keyOf<SessionFactoryService>(qualifier))
val schemaValidatorServiceKey = keyOf<SchemaValidatorService>(qualifier)
bind(schemaValidatorServiceKey)
.toProvider(Provider<SchemaValidatorService> {
@@ -159,7 +161,8 @@ class HibernateModule(
bind(keyOf<SessionFactory>(qualifier))
.toProvider(keyOf<SessionFactoryService>(qualifier))
.asSingleton()
bind(keyOf<TransacterService>(qualifier)).to(keyOf<SessionFactoryService>(qualifier))
bind(keyOf<TransacterService>(qualifier)).to(
keyOf<SessionFactoryService>(qualifier))
bind(keyOf<SessionFactoryService>(qualifier)).toProvider(Provider<SessionFactoryService> {
SessionFactoryService(qualifier, config, dataSourceProvider,
hibernateInjectorAccessProvider.get(),
@@ -114,7 +114,8 @@ internal class SchemaValidatorTest {
ValidationDb::class, Environment.TESTING, schemaMigratorProvider, config.data_source)
}).asSingleton()

bind(keyOf<TransacterService>(qualifier)).to(keyOf<SessionFactoryService>(qualifier))
bind(keyOf<TransacterService>(qualifier)).to(
keyOf<SessionFactoryService>(qualifier))
bind(keyOf<SessionFactoryService>(qualifier)).toProvider(Provider<SessionFactoryService> {
SessionFactoryService(
qualifier,
@@ -0,0 +1,14 @@
buildscript {
dependencies {
classpath dep.kotlinNoArgPlugin
}
}

dependencies {
compile dep.guice
compile dep.guiceMultibindings
compile dep.kotlinStdLib
compile dep.kotlinReflection

testCompile project(':misk-testing')
}
@@ -0,0 +1,4 @@
POM_ARTIFACT_ID=misk-inject
POM_NAME=misk-inject
POM_DESCRIPTION=a module to make using guice with kotlin easier
POM_PACKAGING=jar
@@ -68,7 +68,7 @@ inline fun <reified T : Any> setOfType() = setOfType(T::class)
inline fun <reified K : Any, reified V : Any> mapOfType() = mapOfType(K::class, V::class)

fun <K : Any, V : Any> mapOfType(keyType: KClass<K>, valueType: KClass<V>) =
mapOfType(keyType.typeLiteral(), valueType.typeLiteral())
mapOfType(keyType.typeLiteral(), valueType.typeLiteral())

@Suppress("UNCHECKED_CAST") // The type system isn't aware of constructed types.
fun <K, V> mapOfType(keyType: TypeLiteral<K>, valueType: TypeLiteral<V>): TypeLiteral<Map<K, V>> =
@@ -70,7 +70,8 @@ abstract class KAbstractModule : AbstractModule() {
parameterizedType<List<*>>(Types.subtypeOf(type.java)).typeLiteral() as TypeLiteral<List<T>>
val listOfOutTKey = listOfOutT.toKey(annotation)
val listOfTKey = listOfT.toKey(annotation)
bind(listOfOutTKey).toProvider(ListProvider(mutableSetOfTKey, getProvider(mutableSetOfTKey)))
bind(listOfOutTKey).toProvider(
ListProvider(mutableSetOfTKey, getProvider(mutableSetOfTKey)))
bind(setOfOutTKey).to(setOfT.toKey(annotation))
bind(listOfTKey).to(listOfOutTKey)

@@ -15,7 +15,8 @@ class MapBinderTest {
@Inject @TestAnnotation private lateinit var intToName: Map<Int, String>
@Inject @TestAnnotation private lateinit var nameToShape: Map<String, Shape>
@Inject @TestAnnotation private lateinit var shapeToName: Map<Shape, String>
@Inject @TestAnnotation private lateinit var shapeToColor: Map<Shape, Color>
@Inject @TestAnnotation
private lateinit var shapeToColor: Map<Shape, Color>
@Inject private lateinit var unqualifiedShapeToColor: Map<Shape, Color>

@Test
@@ -45,16 +46,24 @@ class TestModule : KAbstractModule() {
newMapBinder<Int, String>(TestAnnotation::class).addBinding(1).toInstance("one")
newMapBinder<Int, String>(TestAnnotation::class).addBinding(2).toInstance("two")

newMapBinder<String, Shape>(TestAnnotation::class).addBinding("square").toInstance(Square())
newMapBinder<String, Shape>(TestAnnotation::class).addBinding("circle").toInstance(Circle())
newMapBinder<String, Shape>(TestAnnotation::class).addBinding("square").toInstance(
Square())
newMapBinder<String, Shape>(TestAnnotation::class).addBinding("circle").toInstance(
Circle())

newMapBinder<Shape, String>(TestAnnotation::class).addBinding(Square()).toInstance("square")
newMapBinder<Shape, String>(TestAnnotation::class).addBinding(Circle()).toInstance("circle")
newMapBinder<Shape, String>(TestAnnotation::class).addBinding(
Square()).toInstance("square")
newMapBinder<Shape, String>(TestAnnotation::class).addBinding(
Circle()).toInstance("circle")

newMapBinder<Shape, Color>(TestAnnotation::class).addBinding(Square()).toInstance(Blue())
newMapBinder<Shape, Color>(TestAnnotation::class).addBinding(Circle()).toInstance(Red())
newMapBinder<Shape, Color>(TestAnnotation::class).addBinding(
Square()).toInstance(Blue())
newMapBinder<Shape, Color>(TestAnnotation::class).addBinding(
Circle()).toInstance(Red())

newMapBinder<Shape, Color>().addBinding(Square()).toInstance(Blue())
newMapBinder<Shape, Color>().addBinding(Circle()).toInstance(Red())
newMapBinder<Shape, Color>().addBinding(Square()).toInstance(
Blue())
newMapBinder<Shape, Color>().addBinding(Circle()).toInstance(
Red())
}
}
@@ -34,8 +34,10 @@ class MultiBindingsModule : KAbstractModule() {
override fun configure() {
newMultibinder<Int>(TestAnnotation::class).addBinding().toInstance(1)

newMultibinder<Color>(TestAnnotation::class).addBinding().toInstance(Blue())
newMultibinder<Color>(TestAnnotation::class).addBinding().toInstance(Red())
newMultibinder<Color>(TestAnnotation::class).addBinding().toInstance(
Blue())
newMultibinder<Color>(TestAnnotation::class).addBinding().toInstance(
Red())

newMultibinder<Color>().addBinding().toInstance(Blue())
}
@@ -49,6 +49,7 @@ dependencies {
compile dep.prometheusClient
compile dep.prometheusHotspot
compile dep.jnrUnixsocket
compile project(':misk-inject')

testCompile dep.kotlinxCoroutines
testCompile dep.mockitoCore
@@ -11,7 +11,6 @@ import misk.web.mediatype.asMediaType
import okhttp3.MediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
@@ -20,3 +20,4 @@ include ':samples:exemplar'
include ':samples:exemplarchat'
include ':misk-metrics-digester'
include ':misk-crypto'
include ':misk-inject'

0 comments on commit 0e2fde8

Please sign in to comment.
You can’t perform that action at this time.