reify gson, use jvm 1.8 for kotlin compile in sample

This commit is contained in:
Brian Plummer 2019-05-28 08:17:23 -04:00
parent e4b8a682a0
commit 3d1ebd751a
6 changed files with 28 additions and 40 deletions

View file

@ -31,6 +31,12 @@ android {
}
}
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
testImplementation libraries.junit

View file

@ -69,7 +69,7 @@ class SampleApp : Application() {
return StoreBuilder.parsedWithKey<BarCode, BufferedSource, RedditData>()
.fetcher { key -> fetcher(key).await().source() }
.persister(newPersister())
.parser(MoshiParserFactory.createSourceParser(moshi, RedditData::class.java))
.parser(MoshiParserFactory.createSourceParser(moshi))
.open()
}

View file

@ -2,10 +2,10 @@ package com.nytimes.android.external.store3.middleware
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.nytimes.android.external.store3.base.Parser
import java.io.Reader
import java.lang.reflect.Type
import okio.BufferedSource
@ -18,37 +18,43 @@ object GsonParserFactory {
* Returns a new Parser which parses from [Reader] to the specified type, using
* a new default configured [Gson] instance.
*/
fun <T> createReaderParser(type: Type): Parser<Reader, T> = createReaderParser(Gson(), type)
inline fun <reified T> createReaderParser(): Parser<Reader, T> = createReaderParser(Gson())
/**
* Returns a new Parser which parses from [Reader] to the specified type, using
* the provided [Gson] instance.
*/
fun <T> createReaderParser(gson: Gson, type: Type): Parser<Reader, T> = GsonReaderParser(gson, type)
inline fun <reified T> createReaderParser(gson: Gson): Parser<Reader, T> = GsonReaderParser(gson, object : TypeToken<T>() {}.type)
/**
* Returns a new Parser which parses from [Reader] to the specified type, using
* a new default configured [Gson] instance.
*/
fun <T> createSourceParser(type: Type): Parser<BufferedSource, T> = createSourceParser(Gson(), type)
inline fun <reified T> createSourceParser(): Parser<BufferedSource, T> = createSourceParser(Gson())
/**
* Returns a new Parser which parses from [BufferedSource] to the specified type, using
* the provided [Gson] instance.
*/
fun <T> createSourceParser(gson: Gson, type: Type): Parser<BufferedSource, T> = GsonSourceParser(gson, type)
inline fun <reified T> createSourceParser(gson: Gson): Parser<BufferedSource, T> = GsonSourceParser(gson, object : TypeToken<T>() {}.type)
/*
object : TypeToken<T>() {
}.type)
*/
/**
* Returns a new Parser which parses from a String to the specified type, using
* a new default [Gson] instance.
*/
fun <T> createStringParser(type: Class<T>): Parser<String, T> = createStringParser(Gson(), type)
inline fun <reified T> createStringParser(): Parser<String, T> = createStringParser(Gson())
/**
* Returns a new Parser which parses from a String to the specified type, using
* the provided [Gson] instance.
*/
fun <T> createStringParser(gson: Gson, type: Type): Parser<String, T> = GsonStringParser(gson, type)
inline fun <reified T> createStringParser(gson: Gson): Parser<String, T> = GsonStringParser(gson, object : TypeToken<T>() {}.type)
}

View file

@ -25,7 +25,7 @@ class GenericParserStoreTest {
@Test
fun testSimple() = runBlocking<Unit> {
val parser = GsonParserFactory.createSourceParser<Foo>(Gson(), Foo::class.java)
val parser = GsonParserFactory.createSourceParser<Foo>(Gson())
val simpleStore = StoreBuilder.parsedWithKey<BarCode, BufferedSource, Foo>()
.persister(persister)

View file

@ -1,12 +1,10 @@
package com.nytimes.android.external.store3
import com.google.gson.Gson
import com.nhaarman.mockitokotlin2.mock
import com.nytimes.android.external.store3.middleware.GsonParserFactory
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import java.lang.reflect.Type
class GsonParserFactoryTest {
@ -14,49 +12,30 @@ class GsonParserFactoryTest {
@JvmField
var expectedException = ExpectedException.none()
private val type: Type = mock()
private val gson = Gson()
@Test
fun shouldCreateParsersProperly() {
GsonParserFactory.createReaderParser<Any>(gson, type)
GsonParserFactory.createSourceParser<Any>(gson, type)
GsonParserFactory.createStringParser<Any>(gson, type)
}
@Test
fun shouldThrowExceptionWhenCreatingReaderWithNullType() {
expectedException.expect(NullPointerException::class.java)
GsonParserFactory.createReaderParser<Any>(gson, null!!)
GsonParserFactory.createReaderParser<Any>(gson)
GsonParserFactory.createSourceParser<Any>(gson)
GsonParserFactory.createStringParser<Any>(gson)
}
@Test
fun shouldThrowExceptionWhenCreatingReaderWithNullGson() {
expectedException.expect(NullPointerException::class.java)
GsonParserFactory.createReaderParser<Any>(null!!, type)
}
@Test
fun shouldThrowExceptionWhenCreatingSourceWithNullType() {
expectedException.expect(NullPointerException::class.java)
GsonParserFactory.createSourceParser<Any>(gson, null!!)
GsonParserFactory.createReaderParser<Any>(null!!)
}
@Test
fun shouldThrowExceptionWhenCreatingSourceWithNullGson() {
expectedException.expect(NullPointerException::class.java)
GsonParserFactory.createSourceParser<Any>(null!!, type)
}
@Test
fun shouldThrowExceptionWhenCreatingStringWithNullType() {
expectedException.expect(NullPointerException::class.java)
GsonParserFactory.createStringParser<Any>(gson, null!!)
GsonParserFactory.createSourceParser<Any>(null!!)
}
@Test
fun shouldThrowExceptionWhenCreatingStringWithNullGson() {
expectedException.expect(NullPointerException::class.java)
GsonParserFactory.createStringParser<Any>(null!!, type)
GsonParserFactory.createStringParser<Any>(null!!)
}
}

View file

@ -2,7 +2,6 @@ package com.nytimes.android.external.store3
import com.google.common.base.Charsets.UTF_8
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import com.nytimes.android.external.store3.base.Fetcher
@ -27,9 +26,7 @@ class GsonSourceListParserTest {
@Test
fun testSimple() = runBlocking<Unit> {
val parser = GsonParserFactory.createSourceParser<List<Foo>>(Gson(), object : TypeToken<List<Foo>>() {
}.type)
val parser = GsonParserFactory.createSourceParser<List<Foo>>(Gson())
val simpleStore = StoreBuilder.parsedWithKey<BarCode, BufferedSource, List<Foo>>()