Skip to content

Commit

Permalink
🐛 Fix error when parse any field which not imported by context element
Browse files Browse the repository at this point in the history
  • Loading branch information
devkanro committed Jan 11, 2023
1 parent 9bcadd5 commit af42251
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 47 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pluginGroup=io.kanro.idea.plugin.protobuf
pluginName=IntelliJ Protobuf Language Plugin
# SemVer format -> https://semver.org
pluginVersion=1.6.30
pluginVersion=1.6.31
# See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
# for insight into build numbers and IntelliJ Platform versions.
pluginSinceBuild=223
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,22 @@ import com.bybutter.sisyphus.jackson.Json
import com.bybutter.sisyphus.jackson.toJson
import com.bybutter.sisyphus.protobuf.InternalProtoApi
import com.bybutter.sisyphus.protobuf.LocalProtoReflection
import com.bybutter.sisyphus.protobuf.booster.Booster_4CCD27CC8C541E27D15600026AA8457F
import com.bybutter.sisyphus.protobuf.dynamic.DynamicFileSupport
import com.bybutter.sisyphus.protobuf.findMessageSupport
import com.bybutter.sisyphus.protobuf.invoke
import com.bybutter.sisyphus.protobuf.jackson.JacksonReader
import com.bybutter.sisyphus.protobuf.jackson.ProtoModule
import com.bybutter.sisyphus.protobuf.primitives.FileDescriptorSet
import com.intellij.httpClient.execution.common.CommonClientResponse
import com.intellij.httpClient.execution.common.CommonClientResponseBody
import com.intellij.httpClient.execution.common.RequestHandler
import com.intellij.httpClient.execution.common.RunContext
import com.intellij.httpClient.http.request.psi.HttpMessageBody
import com.intellij.httpClient.http.request.psi.HttpRequestMessagesGroup
import com.intellij.json.psi.JsonFile
import com.intellij.json.psi.JsonStringLiteral
import com.intellij.lang.injection.InjectedLanguageManager
import com.intellij.openapi.application.ApplicationManager
import io.grpc.CallOptions
import io.grpc.ClientCall
import io.grpc.Metadata
import io.grpc.MethodDescriptor
import io.grpc.Status
import io.kanro.idea.plugin.protobuf.compile.Protoc
import io.kanro.idea.plugin.protobuf.grpc.referece.GrpcMethodReference
import io.kanro.idea.plugin.protobuf.grpc.referece.GrpcTypeUrlReference
import io.kanro.idea.plugin.protobuf.lang.psi.ProtobufRpcDefinition
import io.kanro.idea.plugin.protobuf.lang.psi.primitive.ProtobufElement
import io.kanro.idea.plugin.protobuf.lang.psi.walkChildren
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import java.util.concurrent.TimeUnit
Expand All @@ -41,44 +29,20 @@ import kotlin.concurrent.withLock
@Suppress("UnstableApiUsage")
object GrpcRequestHandler : RequestHandler<GrpcRequest> {
private fun buildProtoTypes(runContext: RunContext): LocalProtoReflection {
val fileDescriptor = ApplicationManager.getApplication().runReadAction<FileDescriptorSet> {
val httpRequest = runContext.requestInfo.requestPointer.element ?: throw IllegalStateException()
val method = httpRequest.requestTarget?.references?.filterIsInstance<GrpcMethodReference>()?.firstOrNull()
?.resolve() as? ProtobufRpcDefinition ?: throw IllegalStateException()
val contextElements = mutableListOf<ProtobufElement>(method)

val requestBodyGroup = httpRequest.requestBody as? HttpRequestMessagesGroup ?: throw IllegalStateException()
requestBodyGroup.requestMessageList.forEach {
if (it !is HttpMessageBody) return@forEach
val injected = InjectedLanguageManager.getInstance(httpRequest.project).getInjectedPsiFiles(it)
val json = injected?.firstOrNull { it.first is JsonFile }?.first ?: return@forEach

json.walkChildren<JsonStringLiteral>(true) {
it.references.forEach {
if (it !is GrpcTypeUrlReference) return@forEach
(it.resolve() as? ProtobufElement)?.let {
contextElements += it
}
}
}
}
Protoc.compileFiles(contextElements)
}

return LocalProtoReflection().apply {
fileDescriptor.file.forEach {
register(DynamicFileSupport(it))
}
Booster_4CCD27CC8C541E27D15600026AA8457F(this)
}
val httpRequest = runContext.requestInfo.requestPointer.element ?: throw IllegalStateException()
val method = httpRequest.requestTarget?.references?.filterIsInstance<GrpcMethodReference>()?.firstOrNull()
?.resolve() as? ProtobufRpcDefinition ?: throw IllegalStateException()
return ProtoFileReflection(method)
}

@OptIn(InternalProtoApi::class)
override fun execute(request: GrpcRequest, runContext: RunContext): CommonClientResponse {
if (ProtoModule::class.java.canonicalName !in Json.mapper.registeredModuleIds) {
Json.mapper.registerModule(ProtoModule())
}
val types = buildProtoTypes(runContext)
val types = ApplicationManager.getApplication().runReadAction<LocalProtoReflection> {
buildProtoTypes(runContext)
}
val inputSupport = types.findMessageSupport(request.inputType)
val outputSupport = types.findMessageSupport(request.outputType)
val requests = types.invoke {
Expand All @@ -102,8 +66,7 @@ object GrpcRequestHandler : RequestHandler<GrpcRequest> {
}

val call = channel.newCall(
MethodDescriptor.newBuilder(ByteArrayMarshaller, ByteArrayMarshaller)
.setFullMethodName(request.method)
MethodDescriptor.newBuilder(ByteArrayMarshaller, ByteArrayMarshaller).setFullMethodName(request.method)
.setType(methodType).build(), CallOptions.DEFAULT
)

Expand Down Expand Up @@ -176,4 +139,3 @@ object GrpcRequestHandler : RequestHandler<GrpcRequest> {
override fun prepareExecutionEnvironment(request: GrpcRequest, runContext: RunContext) {
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.kanro.idea.plugin.protobuf.grpc.request

import com.bybutter.sisyphus.protobuf.LocalProtoReflection
import com.bybutter.sisyphus.protobuf.ProtoSupport
import com.bybutter.sisyphus.protobuf.booster.Booster_4CCD27CC8C541E27D15600026AA8457F
import com.bybutter.sisyphus.protobuf.dynamic.DynamicFileSupport
import com.intellij.openapi.application.ApplicationManager
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.stubs.StubIndex
import io.kanro.idea.plugin.protobuf.compile.Protoc
import io.kanro.idea.plugin.protobuf.lang.psi.primitive.ProtobufElement
import io.kanro.idea.plugin.protobuf.lang.psi.stub.index.QualifiedNameIndex

class ProtoFileReflection(private val context: ProtobufElement) : LocalProtoReflection() {
init {
Booster_4CCD27CC8C541E27D15600026AA8457F(this)
}

override fun findSupport(name: String): ProtoSupport<*>? {
super.findSupport(name)?.let { return it }

synchronized(this) {
ApplicationManager.getApplication().runReadAction {
doResolve(name.trim('.').substringAfterLast('/'))
}
}

return super.findSupport(name)
}

private fun doResolve(name: String) {
val elements = StubIndex.getElements(
QualifiedNameIndex.key,
name,
context.project,
GlobalSearchScope.allScope(context.project),
ProtobufElement::class.java
).toList()
val descriptorSet = Protoc.compileFiles(elements)
descriptorSet.file.forEach {
register(DynamicFileSupport(it))
}
}
}

0 comments on commit af42251

Please sign in to comment.