Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove usage of KV #1020

Merged
merged 1 commit into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions common-api/src/main/kotlin/com/itangcent/common/kit/KVUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,17 @@ object KVUtils {
* get description of options
*/
fun getOptionDesc(options: List<Map<String, Any?>>): String? {
return options.stream()
.map { concat(it["value"]?.toString(), it["desc"]?.toString()) }
.filter { it != null }
return options.asSequence()
.mapNotNull { concat(it["value"]?.toString(), it["desc"]?.toString()) }
.joinToString("\n")
}

/**
* get description of constants
*/
fun getConstantDesc(constants: List<Map<String, Any?>>): String? {
return constants.stream()
.map { concat(it["name"]?.toString(), it["desc"]?.toString()) }
.filter { it != null }
return constants.asSequence()
.mapNotNull { concat(it["name"]?.toString(), it["desc"]?.toString()) }
.joinToString("\n")
}

Expand Down Expand Up @@ -97,11 +95,9 @@ object KVUtils {

@Suppress("UNCHECKED_CAST")
fun addComment(info: HashMap<Any, Any?>, field: String, comment: String?) {
var comments = info[Attrs.COMMENT_ATTR]
val comments = info[Attrs.COMMENT_ATTR]
if (comments == null) {
comments = KV<String, Any?>()
info[Attrs.COMMENT_ATTR] = comments
comments[field] = comment
info[Attrs.COMMENT_ATTR] = linkedMapOf(field to comment)
} else {
val oldComment = (comments as HashMap<Any?, Any?>)[field]
if (oldComment == null) {
Expand Down Expand Up @@ -146,19 +142,15 @@ object KVUtils {

@Suppress("UNCHECKED_CAST")
fun addOptions(info: HashMap<Any, Any?>, field: String, options: ArrayList<HashMap<String, Any?>>) {
var comments = info[Attrs.COMMENT_ATTR]
val comments = info[Attrs.COMMENT_ATTR]
if (comments == null) {
comments = KV<String, Any?>()
info[Attrs.COMMENT_ATTR] = comments
comments["$field@options"] = options
info[Attrs.COMMENT_ATTR] = linkedMapOf("$field@options" to options)
} else {
val oldOptions = (comments as HashMap<Any?, Any?>)["$field@options"]
if (oldOptions == null) {
comments["$field@options"] = options
} else {
val mergeOptions: ArrayList<Any?> = ArrayList(oldOptions as ArrayList<*>)
mergeOptions.addAll(options)
comments["$field@options"] = mergeOptions
comments["$field@options"] = (oldOptions as ArrayList<*>) + options
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fun Request.header(name: String): String? {
}
val lowerName = name.lowercase()
return this.headers!!
.stream()
.asSequence()
.filter { it.name?.lowercase() == lowerName }
.map { it.value }
.firstOrNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object RequestUtils {
return mutableBody
}
if (body is List<*>) {
return body.stream().map { toRawBody(it, copy) }.toList()
return body.map { toRawBody(it, copy) }
}
if (body is Array<*>) {
return body.mapToTypedArray { toRawBody(it, copy) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.itangcent.common.kit

import com.itangcent.common.utils.KV
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test

Expand All @@ -14,7 +13,7 @@ class KitUtilsTest {
assertEquals(null, null.toJson())
assertEquals("str", "str".toJson())
assertEquals("1", 1.toJson())
assertEquals("{\"a\":\"b\"}", KV.by("a", "b").toJson())
assertEquals("{\"a\":\"b\"}", linkedMapOf("a" to "b").toJson())
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ plugin_version=2.6.1.212.0
kotlin.code.style=official
kotlin_version=1.8.0
junit_version=5.9.2
itangcent_intellij_version=1.5.4
itangcent_intellij_version=1.5.5
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import com.itangcent.intellij.util.FileType
import java.util.*
import java.util.concurrent.BlockingQueue
import java.util.concurrent.LinkedBlockingQueue
import kotlin.streams.toList

@Singleton
open class ClassApiExporterHelper {
Expand Down Expand Up @@ -69,13 +68,13 @@ open class ClassApiExporterHelper {

companion object : Log()

fun extractParamComment(psiMethod: PsiMethod): KV<String, Any>? {
fun extractParamComment(psiMethod: PsiMethod): MutableMap<String, Any?>? {
val subTagMap = docHelper!!.getSubTagMapOfDocComment(psiMethod, "param")
if (subTagMap.isEmpty()) {
return null
}

val methodParamComment: KV<String, Any> = KV.create()
val methodParamComment = linkedMapOf<String, Any?>()
val parameters = psiMethod.parameterList.parameters
subTagMap.entries.forEach { entry ->
val name: String = entry.key
Expand Down Expand Up @@ -115,7 +114,7 @@ open class ClassApiExporterHelper {
return linkResolver!!.linkToMethod(linkMethod)
}

override fun linkToUnresolved(plainText: String): String? {
override fun linkToUnresolved(plainText: String): String {
return plainText
}
})
Expand Down Expand Up @@ -181,20 +180,20 @@ open class ClassApiExporterHelper {
fun foreachPsiMethod(cls: PsiClass, handle: (PsiMethod) -> Unit) {
actionContext.runInReadUI {
jvmClassHelper!!.getAllMethods(cls)
.stream()
.asSequence()
.filter { !shouldIgnore(it) }
.forEach(handle)
}
}

fun export(): List<Doc> {
val docs: MutableList<Doc> = Collections.synchronizedList(java.util.ArrayList())
val docs: MutableList<Doc> = Collections.synchronizedList(ArrayList())
export { docs.add(it) }
return docs
}

fun export(handle: (Doc) -> Unit) {
logger.info("Start find apis...")
logger.info("Start export api...")
val psiClassQueue: BlockingQueue<PsiClass> = LinkedBlockingQueue()

val boundary = actionContext.createBoundary()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ class DefaultMethodInferHelper : MethodInferHelper {
obj is Variable -> valueOf(obj.getValue())
obj is ObjectHolder -> valueOf(obj.getOrResolve())
obj is MutableMap<*, *> -> {
val copy = KV.create<Any?, Any?>()
val copy = linkedMapOf<Any?, Any?>()
obj.entries.forEach { copy[valueOf(it.key)] = valueOf(it.value) }
return copy
}
Expand Down Expand Up @@ -697,7 +697,7 @@ class DefaultMethodInferHelper : MethodInferHelper {
return null
}
actionContext!!.checkStatus()
val kv = KV.create<String, Any?>()
val fields = linkedMapOf<String, Any?>()
for (field in jvmClassHelper!!.getAllFields(psiClass)) {
if (jvmClassHelper.isStaticFinal(field)) {
continue
Expand All @@ -706,18 +706,18 @@ class DefaultMethodInferHelper : MethodInferHelper {
val name = psiClassHelper!!.getJsonFieldName(field)

if (type is PsiPrimitiveType) { //primitive Type
kv[name] = PsiTypesUtil.getDefaultValue(type)
fields[name] = PsiTypesUtil.getDefaultValue(type)
continue
}
//reference Type
if (psiClassHelper.isNormalType(type)) {//normal Type
kv[name] = psiClassHelper.getDefaultValue(type)
fields[name] = psiClassHelper.getDefaultValue(type)
continue
}

kv[name] = DirectVariable { getSimpleFields(type, psiClass, deep + 1) }
fields[name] = DirectVariable { getSimpleFields(type, psiClass, deep + 1) }
}
return kv
return fields
}

private fun getSimpleFields(psiType: PsiType?, context: PsiElement): Any? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -646,10 +646,10 @@ abstract class RequestClassExporter : ClassExporter {
}
})
} else {
val fields = typeObject.asKV()
val comment = fields.getAsKv(Attrs.COMMENT_ATTR)
val required = fields.getAsKv(Attrs.REQUIRED_ATTR)
val defaultVal = fields.getAsKv(Attrs.DEFAULT_VALUE_ATTR)
val fields = typeObject.asHashMap()
val comment = fields.getSub(Attrs.COMMENT_ATTR)
val required = fields.getSub(Attrs.REQUIRED_ATTR)
val defaultVal = fields.getSub(Attrs.DEFAULT_VALUE_ATTR)
parameterExportContext.setExt("parent", fields)
fields.forEachValid { filedName, fieldVal ->
parameterExportContext.setExt("key", filedName)
Expand All @@ -659,7 +659,7 @@ abstract class RequestClassExporter : ClassExporter {
}
requestBuilderListener.addParam(
parameterExportContext,
request, filedName, tinyQueryParam(fv),
request, filedName.toString(), tinyQueryParam(fv),
required?.getAs(filedName) ?: false,
KVUtils.getUltimateComment(comment, filedName)
)
Expand Down Expand Up @@ -728,10 +728,10 @@ abstract class RequestClassExporter : ClassExporter {
}
})
} else {
val fields = typeObject.asKV()
val comment = fields.getAsKv(Attrs.COMMENT_ATTR)
val required = fields.getAsKv(Attrs.REQUIRED_ATTR)
val defaultVal = fields.getAsKv(Attrs.DEFAULT_VALUE_ATTR)
val fields = typeObject.asHashMap()
val comment = fields.getSub(Attrs.COMMENT_ATTR)
val required = fields.getSub(Attrs.REQUIRED_ATTR)
val defaultVal = fields.getSub(Attrs.DEFAULT_VALUE_ATTR)
requestBuilderListener.addHeaderIfMissed(
parameterExportContext,
request, "Content-Type", "application/x-www-form-urlencoded"
Expand All @@ -743,14 +743,14 @@ abstract class RequestClassExporter : ClassExporter {
if (fv == Magics.FILE_STR) {
requestBuilderListener.addFormFileParam(
parameterExportContext,
request, filedName,
request, filedName.toString(),
required?.getAs(filedName) ?: false,
KVUtils.getUltimateComment(comment, filedName)
)
} else {
requestBuilderListener.addFormParam(
parameterExportContext,
request, filedName, fv?.takeIfNotOriginal()?.toString(),
request, filedName.toString(), fv?.takeIfNotOriginal()?.toString(),
required?.getAs(filedName) ?: false,
KVUtils.getUltimateComment(comment, filedName)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.intellij.psi.PsiElement
import com.intellij.psi.PsiMethod
import com.itangcent.common.logger.traceError
import com.itangcent.common.model.MethodDoc
import com.itangcent.common.utils.KV
import com.itangcent.idea.plugin.api.ClassApiExporterHelper
import com.itangcent.idea.plugin.api.export.Orders
import com.itangcent.idea.plugin.api.export.condition.ConditionOnDoc
Expand Down Expand Up @@ -80,13 +79,13 @@ open class SimpleGenericMethodDocClassExporter : ClassExporter {
else -> {
logger!!.info("search api from: $clsQualifiedName")

val kv = KV.create<String, Any?>()
val fields = linkedMapOf<String, Any?>()

processClass(cls, kv)
processClass(cls, fields)

classApiExporterHelper.foreachPsiMethod(cls) { method ->
if (isApi(method) && methodFilter?.checkMethod(method) != false) {
exportMethodApi(cls, method, kv, docHandle)
exportMethodApi(cls, method, fields, docHandle)
}
}
}
Expand All @@ -99,7 +98,7 @@ open class SimpleGenericMethodDocClassExporter : ClassExporter {
}

@Suppress("UNUSED")
protected fun processClass(cls: PsiClass, kv: KV<String, Any?>) {
protected fun processClass(cls: PsiClass, fields: MutableMap<String, Any?>) {
}

@Suppress("UNUSED")
Expand Down Expand Up @@ -132,7 +131,7 @@ open class SimpleGenericMethodDocClassExporter : ClassExporter {
}

private fun exportMethodApi(
psiClass: PsiClass, method: PsiMethod, kv: KV<String, Any?>,
psiClass: PsiClass, method: PsiMethod, fields: MutableMap<String, Any?>,
docHandle: DocHandle,
) {

Expand All @@ -142,12 +141,12 @@ open class SimpleGenericMethodDocClassExporter : ClassExporter {

methodDoc.resource = PsiMethodResource(method, psiClass)

processMethod(method, kv, methodDoc)
processMethod(method, fields, methodDoc)

docHandle(methodDoc)
}

protected open fun processMethod(method: PsiMethod, kv: KV<String, Any?>, methodDoc: MethodDoc) {
protected open fun processMethod(method: PsiMethod, fields: MutableMap<String, Any?>, methodDoc: MethodDoc) {
methodDoc.name = apiHelper!!.nameOfApi(method)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ import com.itangcent.http.contentType
import com.itangcent.idea.plugin.settings.helper.PostmanSettingsHelper
import com.itangcent.idea.utils.resolveGsonLazily
import com.itangcent.intellij.context.ActionContext
import com.itangcent.intellij.extend.*
import com.itangcent.intellij.extend.acquireGreedy
import com.itangcent.intellij.extend.asJsonElement
import com.itangcent.intellij.extend.asMap
import com.itangcent.intellij.extend.rx.Throttle
import com.itangcent.intellij.extend.rx.ThrottleHelper
import com.itangcent.intellij.extend.sub
import com.itangcent.intellij.logger.Logger
import com.itangcent.suv.http.HttpClientProvider
import org.apache.http.entity.ContentType
Expand Down Expand Up @@ -118,7 +121,7 @@ open class DefaultPostmanApiHelper : PostmanApiHelper {
.post(COLLECTION)
.contentType(ContentType.APPLICATION_JSON)
.header("x-api-key", postmanSettingsHelper.getPrivateToken())
.body(KV.by("collection", collection))
.body(linkedMapOf("collection" to collection))

workspaceId?.let { request.query("workspace", it) }

Expand Down Expand Up @@ -205,7 +208,7 @@ open class DefaultPostmanApiHelper : PostmanApiHelper {
val request = getHttpClient().put("$COLLECTION/$collectionId")
.contentType(ContentType.APPLICATION_JSON)
.header("x-api-key", postmanSettingsHelper.getPrivateToken())
.body(GsonUtils.toJson(KV.by("collection", apiInfo)).resolveGsonLazily())
.body(GsonUtils.toJson(linkedMapOf("collection" to apiInfo)).resolveGsonLazily())

try {
beforeRequest(request)
Expand Down
Loading