Enable and apply Ktfmt to xplat/simplesql, xplat/sonar, and xplat/spectrum

Summary: As title.

Reviewed By: zertosh

Differential Revision: D30425160

fbshipit-source-id: c72d270d7cd3f30990aac55e33e8f72d60ed5fe2
This commit is contained in:
Omer Strulovich
2021-08-19 07:27:32 -07:00
committed by Facebook GitHub Bot
parent 8e2a839f9d
commit 1db39b8171
18 changed files with 359 additions and 387 deletions

View File

@@ -16,35 +16,32 @@ import com.facebook.flipper.plugins.retrofit2protobuf.adapter.RetrofitServiceToG
import com.facebook.flipper.plugins.retrofit2protobuf.model.CallNestedMessagesPayload
object SendProtobufToFlipperFromRetrofit {
operator fun invoke(baseUrl: String, service: Class<*>) {
getNetworkPlugin()?.addProtobufDefinitions(
baseUrl,
generateProtobufDefinitions(service).toFlipperArray()
)
}
operator fun invoke(baseUrl: String, service: Class<*>) {
getNetworkPlugin()
?.addProtobufDefinitions(baseUrl, generateProtobufDefinitions(service).toFlipperArray())
}
private fun getNetworkPlugin(): NetworkFlipperPlugin? {
return AndroidFlipperClient.getInstanceIfInitialized()?.let { client ->
client.getPlugin(NetworkFlipperPlugin.ID) as? NetworkFlipperPlugin
}
private fun getNetworkPlugin(): NetworkFlipperPlugin? {
return AndroidFlipperClient.getInstanceIfInitialized()?.let { client ->
client.getPlugin(NetworkFlipperPlugin.ID) as? NetworkFlipperPlugin
}
}
private fun generateProtobufDefinitions(service: Class<*>): List<CallNestedMessagesPayload> {
return RetrofitServiceToGenericCallDefinitions(service).let { definitions ->
GenericCallDefinitionsToMessageDefinitionsIfProtobuf(definitions)
}.let { messages ->
messages.map {
CallNestedMessagesPayload(
path = it.path,
method = it.method,
requestMessageFullName = it.requestMessageFullName,
requestDefinitions = it.requestModel,
responseMessageFullName = it.responseMessageFullName,
responseDefinitions = it.responseModel
)
}
private fun generateProtobufDefinitions(service: Class<*>): List<CallNestedMessagesPayload> {
return RetrofitServiceToGenericCallDefinitions(service)
.let { definitions -> GenericCallDefinitionsToMessageDefinitionsIfProtobuf(definitions) }
.let { messages ->
messages.map {
CallNestedMessagesPayload(
path = it.path,
method = it.method,
requestMessageFullName = it.requestMessageFullName,
requestDefinitions = it.requestModel,
responseMessageFullName = it.responseMessageFullName,
responseDefinitions = it.responseModel)
}
}
}
}
}
private fun Iterable<FlipperValue>.toFlipperArray(): FlipperArray =

View File

@@ -12,23 +12,22 @@ import com.facebook.flipper.plugins.retrofit2protobuf.model.GenericCallDefinitio
import me.haroldmartin.protobufjavatoprotobufjs.ProtobufGeneratedJavaToProtobufJs
internal object GenericCallDefinitionsToMessageDefinitionsIfProtobuf {
operator fun invoke(callDefinitions: List<GenericCallDefinition>): List<FullNamedMessagesCallDefinition> {
return callDefinitions.mapNotNull { definition ->
val responseRootAndMessages = definition.responseType?.let {
ProtobufGeneratedJavaToProtobufJs(it)
}
val requestRootAndMessages = definition.requestType?.let {
ProtobufGeneratedJavaToProtobufJs(it)
}
operator fun invoke(
callDefinitions: List<GenericCallDefinition>
): List<FullNamedMessagesCallDefinition> {
return callDefinitions.mapNotNull { definition ->
val responseRootAndMessages =
definition.responseType?.let { ProtobufGeneratedJavaToProtobufJs(it) }
val requestRootAndMessages =
definition.requestType?.let { ProtobufGeneratedJavaToProtobufJs(it) }
FullNamedMessagesCallDefinition(
path = definition.path,
method = definition.method,
responseMessageFullName = responseRootAndMessages?.rootFullName,
responseModel = responseRootAndMessages?.descriptors,
requestMessageFullName = requestRootAndMessages?.rootFullName,
requestModel = requestRootAndMessages?.descriptors
)
}
FullNamedMessagesCallDefinition(
path = definition.path,
method = definition.method,
responseMessageFullName = responseRootAndMessages?.rootFullName,
responseModel = responseRootAndMessages?.descriptors,
requestMessageFullName = requestRootAndMessages?.rootFullName,
requestModel = requestRootAndMessages?.descriptors)
}
}
}

View File

@@ -13,65 +13,64 @@ import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
internal object RetrofitServiceToGenericCallDefinitions {
@Suppress("LoopWithTooManyJumpStatements")
operator fun invoke(service: Class<*>): List<GenericCallDefinition> {
val methodToProtobufDefinition = mutableListOf<GenericCallDefinition>()
for (method in service.declaredMethods) {
val responseType = method.innerGenericReturnClass ?: continue
val (path, httpMethod) = method.annotations.urlPathAndMethod ?: continue
methodToProtobufDefinition.add(
GenericCallDefinition(
path = path,
method = httpMethod,
responseType = responseType,
requestType = method.requestBodyType
)
)
}
return methodToProtobufDefinition
@Suppress("LoopWithTooManyJumpStatements")
operator fun invoke(service: Class<*>): List<GenericCallDefinition> {
val methodToProtobufDefinition = mutableListOf<GenericCallDefinition>()
for (method in service.declaredMethods) {
val responseType = method.innerGenericReturnClass ?: continue
val (path, httpMethod) = method.annotations.urlPathAndMethod ?: continue
methodToProtobufDefinition.add(
GenericCallDefinition(
path = path,
method = httpMethod,
responseType = responseType,
requestType = method.requestBodyType))
}
return methodToProtobufDefinition
}
}
private val Array<Annotation>.urlPathAndMethod: Pair<String, String>?
get() {
var path: Pair<String, String>? = null
for (a in this) {
path = when (a.annotationClass) {
retrofit2.http.DELETE::class -> (a as retrofit2.http.DELETE).value to "DELETE"
retrofit2.http.GET::class -> (a as retrofit2.http.GET).value to "GET"
retrofit2.http.HEAD::class -> (a as retrofit2.http.HEAD).value to "HEAD"
retrofit2.http.OPTIONS::class -> (a as retrofit2.http.OPTIONS).value to "OPTIONS"
retrofit2.http.PATCH::class -> (a as retrofit2.http.PATCH).value to "PATCH"
retrofit2.http.POST::class -> (a as retrofit2.http.POST).value to "POST"
retrofit2.http.PUT::class -> (a as retrofit2.http.PUT).value to "PUT"
else -> null
}
if (path != null) break
}
return path
get() {
var path: Pair<String, String>? = null
for (a in this) {
path =
when (a.annotationClass) {
retrofit2.http.DELETE::class -> (a as retrofit2.http.DELETE).value to "DELETE"
retrofit2.http.GET::class -> (a as retrofit2.http.GET).value to "GET"
retrofit2.http.HEAD::class -> (a as retrofit2.http.HEAD).value to "HEAD"
retrofit2.http.OPTIONS::class -> (a as retrofit2.http.OPTIONS).value to "OPTIONS"
retrofit2.http.PATCH::class -> (a as retrofit2.http.PATCH).value to "PATCH"
retrofit2.http.POST::class -> (a as retrofit2.http.POST).value to "POST"
retrofit2.http.PUT::class -> (a as retrofit2.http.PUT).value to "PUT"
else -> null
}
if (path != null) break
}
return path
}
private val Method.requestBodyType: Class<*>?
get() {
parameterAnnotations.forEachIndexed { index, annotations ->
annotations.forEach { annotation ->
if (annotation.annotationClass == retrofit2.http.Body::class) {
return parameterTypes[index]
}
}
get() {
parameterAnnotations.forEachIndexed { index, annotations ->
annotations.forEach { annotation ->
if (annotation.annotationClass == retrofit2.http.Body::class) {
return parameterTypes[index]
}
return null
}
}
return null
}
private val Method.innerGenericReturnClass: Class<*>?
get() = (genericReturnType as? ParameterizedType)?.innerGenericType as? Class<*>
get() = (genericReturnType as? ParameterizedType)?.innerGenericType as? Class<*>
private val ParameterizedType?.innerGenericType: Type?
get() {
val innerType = this?.actualTypeArguments?.get(0)
return if (innerType is ParameterizedType) {
innerType.innerGenericType
} else {
innerType
}
get() {
val innerType = this?.actualTypeArguments?.get(0)
return if (innerType is ParameterizedType) {
innerType.innerGenericType
} else {
innerType
}
}

View File

@@ -19,29 +19,30 @@ internal data class CallNestedMessagesPayload(
val responseMessageFullName: String?,
val responseDefinitions: Map<String, Any>?
) : FlipperValue {
override fun toFlipperObject(): FlipperObject {
return FlipperObject.Builder()
.put("path", path)
.put("method", method)
.put("requestMessageFullName", requestMessageFullName)
.put("requestDefinitions", requestDefinitions?.toFlipperObject())
.put("responseMessageFullName", responseMessageFullName)
.put("responseDefinitions", responseDefinitions?.toFlipperObject())
.build()
}
override fun toFlipperObject(): FlipperObject {
return FlipperObject.Builder()
.put("path", path)
.put("method", method)
.put("requestMessageFullName", requestMessageFullName)
.put("requestDefinitions", requestDefinitions?.toFlipperObject())
.put("responseMessageFullName", responseMessageFullName)
.put("responseDefinitions", responseDefinitions?.toFlipperObject())
.build()
}
}
private fun Map<*, *>.toFlipperObject(): FlipperObject {
val builder = FlipperObject.Builder()
this.forEach { (key, value) ->
val castValue = when (value) {
is Map<*, *> -> value.toFlipperObject()
is Iterable<*> -> value.toFlipperArray()
else -> value
val builder = FlipperObject.Builder()
this.forEach { (key, value) ->
val castValue =
when (value) {
is Map<*, *> -> value.toFlipperObject()
is Iterable<*> -> value.toFlipperArray()
else -> value
}
builder.put(key as String, castValue)
}
return builder.build()
builder.put(key as String, castValue)
}
return builder.build()
}
private fun Iterable<*>.toFlipperArray(): FlipperArray =