Skip to content

Commit f3c9dd8

Browse files
committed
👕 refactor moshi util #32
1 parent aa1eb68 commit f3c9dd8

File tree

6 files changed

+48
-47
lines changed

6 files changed

+48
-47
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
kotlin.code.style=official
22
# Versions
33
kotlin_version=1.7.21
4-
retrosheet_version=2.0.1
4+
retrosheet_version=2.0.2
55
retrofit_version=2.9.0
66
moshi_version=1.14.0
77
coroutines_version=1.6.4

retrosheet/src/main/kotlin/com/github/theapache64/retrosheet/RetrosheetInterceptor.kt

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import com.github.theapache64.retrosheet.data.SheetErrorJsonAdapter
1010
import com.github.theapache64.retrosheet.utils.CsvConverter
1111
import com.github.theapache64.retrosheet.utils.JsonValidator
1212
import com.github.theapache64.retrosheet.utils.KeyValueUtils
13-
import com.github.theapache64.retrosheet.utils.MoshiUtils
1413
import com.github.theapache64.retrosheet.utils.SheetUtils
14+
import com.squareup.moshi.Moshi
1515
import java.lang.reflect.Method
1616
import java.lang.reflect.ParameterizedType
1717
import java.net.HttpURLConnection
@@ -32,9 +32,18 @@ class RetrosheetInterceptor
3232
private constructor(
3333
val isLoggingEnabled: Boolean = false,
3434
private val sheets: Map<String, Map<String, String>>,
35-
val forms: Map<String, String>
35+
val forms: Map<String, String>,
36+
internal val moshi: Moshi
3637
) : Interceptor {
3738

39+
private val sheetErrorJsonAdapter by lazy {
40+
SheetErrorJsonAdapter(moshi)
41+
}
42+
43+
private val apiErrorJsonAdapter by lazy {
44+
ApiErrorJsonAdapter(moshi)
45+
}
46+
3847
companion object {
3948
private val TAG = RetrosheetInterceptor::class.java.simpleName
4049
private const val URL_START = "https://docs.google.com/spreadsheets/d"
@@ -48,14 +57,6 @@ private constructor(
4857
"https://docs\\.google\\.com/spreadsheets/d/(?<docId>.+)/(?<params>.+)".toRegex()
4958
}
5059

51-
private val sheetErrorJsonAdapter by lazy {
52-
SheetErrorJsonAdapter(MoshiUtils.moshi)
53-
}
54-
55-
private val apiErrorJsonAdapter by lazy {
56-
ApiErrorJsonAdapter(MoshiUtils.moshi)
57-
}
58-
5960
private fun isReturnTypeList(request: Request): Boolean {
6061
val method = request.tag(Invocation::class.java)?.method() ?: return false
6162
// Trying to find return type using reflection
@@ -115,12 +116,14 @@ private constructor(
115116
private val sheets = mutableMapOf<String, Map<String, String>>()
116117
private val forms = mutableMapOf<String, String>()
117118
private var isLoggingEnabled: Boolean = false
119+
private var moshi = Moshi.Builder().build()
118120

119121
fun build(): RetrosheetInterceptor {
120122
return RetrosheetInterceptor(
121123
isLoggingEnabled,
122124
sheets,
123-
forms
125+
forms,
126+
moshi
124127
)
125128
}
126129

@@ -129,6 +132,11 @@ private constructor(
129132
return this
130133
}
131134

135+
fun setMoshi(moshi: Moshi): Builder {
136+
this.moshi = moshi
137+
return this
138+
}
139+
132140
@Suppress("MemberVisibilityCanBePrivate")
133141
fun addSheet(sheetName: String, columnMap: Map<String, String>): Builder {
134142
ColumnNameVerifier(columnMap.keys).verify()
@@ -184,7 +192,7 @@ private constructor(
184192
val responseBuilder = response.newBuilder()
185193

186194
// Checking if it's a JSON response. If yes, it's an error else, it's the CSV.
187-
val isSpreadsheetError = JsonValidator.isValidJsonObject(responseBody)
195+
val isSpreadsheetError = JsonValidator.isValidJsonObject(responseBody, moshi)
188196
if (isSpreadsheetError) {
189197
// It's the spreadsheet error. let's parse it.
190198

@@ -222,7 +230,7 @@ private constructor(
222230
responseBody = KeyValueUtils.transform(responseBody)
223231
}
224232

225-
val csvJson = CsvConverter.convertCsvToJson(responseBody, isReturnTypeList(request))
233+
val csvJson = CsvConverter.convertCsvToJson(responseBody, isReturnTypeList(request), moshi)
226234
if (csvJson != null) {
227235
jsonRoot = csvJson
228236
if (isLoggingEnabled) {

retrosheet/src/main/kotlin/com/github/theapache64/retrosheet/core/GoogleFormHelper.kt

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.github.theapache64.retrosheet.core
22

33
import com.github.theapache64.retrosheet.RetrosheetInterceptor
44
import com.github.theapache64.retrosheet.annotations.Write
5-
import com.github.theapache64.retrosheet.utils.MoshiUtils
5+
import com.squareup.moshi.Moshi
66
import com.squareup.moshi.Types
77
import java.io.IOException
88
import java.net.HttpURLConnection
@@ -20,28 +20,31 @@ import retrofit2.Invocation
2020
class GoogleFormHelper(
2121
private val chain: Interceptor.Chain,
2222
private val request: Request,
23-
private val retrosheetInterceptor: RetrosheetInterceptor
23+
private val retrosheetInterceptor: RetrosheetInterceptor,
2424
) {
25+
26+
27+
private val stringMapAdapter by lazy {
28+
val mapType = Types.newParameterizedType(Map::class.java, String::class.java, String::class.java)
29+
retrosheetInterceptor.moshi.adapter<Map<String, String>>(mapType)
30+
}
31+
32+
private val listAdapter by lazy {
33+
val type = Types.newParameterizedType(List::class.java, Object::class.java)
34+
retrosheetInterceptor.moshi.adapter<List<Any>>(type)
35+
}
36+
37+
private val anyAdapter by lazy {
38+
retrosheetInterceptor.moshi.adapter(Any::class.java)
39+
}
40+
2541
companion object {
2642

2743
private const val FORM_DATA_SPLIT_1 = "FB_PUBLIC_LOAD_DATA_"
2844
private const val FORM_DATA_SPLIT_2 = "</script>"
2945

3046
const val SOLUTION_UPDATE = "Please update retrosheet to latest version."
3147

32-
private val stringMapAdapter by lazy {
33-
val mapType = Types.newParameterizedType(Map::class.java, String::class.java, String::class.java)
34-
MoshiUtils.moshi.adapter<Map<String, String>>(mapType)
35-
}
36-
37-
private val listAdapter by lazy {
38-
val type = Types.newParameterizedType(List::class.java, Object::class.java)
39-
MoshiUtils.moshi.adapter<List<Any>>(type)
40-
}
41-
42-
private val anyAdapter by lazy {
43-
MoshiUtils.moshi.adapter(Any::class.java)
44-
}
4548

4649
fun isGoogleFormSubmit(request: Request): Boolean {
4750
val isForm = (request.tag(Invocation::class.java)?.method()?.getAnnotation(Write::class.java) != null)

retrosheet/src/main/kotlin/com/github/theapache64/retrosheet/utils/CsvConverter.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.theapache64.retrosheet.utils
22

3+
import com.squareup.moshi.Moshi
34
import com.squareup.moshi.Types
45
import de.siegmar.fastcsv.reader.NamedCsvReader
56

@@ -9,7 +10,8 @@ import de.siegmar.fastcsv.reader.NamedCsvReader
910
object CsvConverter {
1011
fun convertCsvToJson(
1112
csvData: String,
12-
isReturnTypeList: Boolean
13+
isReturnTypeList: Boolean,
14+
moshi: Moshi
1315
): String? {
1416
val items = mutableListOf<Map<String, Any?>>()
1517

@@ -54,13 +56,13 @@ object CsvConverter {
5456
return when {
5557
isReturnTypeList -> {
5658
val type = Types.newParameterizedType(List::class.java, Map::class.java)
57-
val adapter = MoshiUtils.moshi.adapter<List<Map<String, Any?>>>(type)
59+
val adapter = moshi.adapter<List<Map<String, Any?>>>(type)
5860
adapter.toJson(items)
5961
}
6062

6163
items.isNotEmpty() -> {
6264
val type = Types.newParameterizedType(Map::class.java, String::class.java, Any::class.java)
63-
val adapter = MoshiUtils.moshi.adapter<Map<String, Any?>>(type)
65+
val adapter = moshi.adapter<Map<String, Any?>>(type)
6466
adapter.toJson(items.first())
6567
}
6668

retrosheet/src/main/kotlin/com/github/theapache64/retrosheet/utils/JsonValidator.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.theapache64.retrosheet.utils
22

33
import com.squareup.moshi.JsonClass
4+
import com.squareup.moshi.Moshi
45

56
/**
67
* Created by theapache64 : Jul 25 Sat,2020 @ 22:00
@@ -9,9 +10,9 @@ import com.squareup.moshi.JsonClass
910
class Empty
1011

1112
object JsonValidator {
12-
fun isValidJsonObject(input: String): Boolean {
13+
fun isValidJsonObject(input: String, moshi: Moshi): Boolean {
1314
return try {
14-
EmptyJsonAdapter(MoshiUtils.moshi).apply { fromJson(input) }
15+
EmptyJsonAdapter(moshi).apply { fromJson(input) }
1516
true
1617
} catch (e: Exception) {
1718
false

retrosheet/src/main/kotlin/com/github/theapache64/retrosheet/utils/MoshiUtils.kt

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)