Skip to content

Commit

Permalink
Merge pull request #20 from ltttttttttttt/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
ltttttttttttt authored Dec 1, 2024
2 parents 4f68955 + 2f161ba commit 4ec73d9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
38 changes: 19 additions & 19 deletions LazyPeopleHttp/src/jvmMain/kotlin/com/lt/lazy_people_http/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.lt.lazy_people_http
import com.google.devtools.ksp.processing.*
import com.google.devtools.ksp.symbol.*
import com.lt.lazy_people_http.options.*
import com.lt.lazy_people_http.options.ReplaceRule._packageName
import com.lt.lazy_people_http.options.ReplaceRule._type
import com.lt.lazy_people_http.provider.*
import org.jetbrains.kotlin.js.descriptorUtils.*
import org.jetbrains.kotlin.types.*
Expand Down Expand Up @@ -49,13 +51,13 @@ internal fun getKSTypeInfo(ks: KSTypeReference): KSTypeInfo {
}
}
//完整type字符串
val thisTypeName = if (LazyPeopleHttpVisitor.typeShowPackage)
val thisTypeName =
ksType.declaration.let {
it.qualifiedName?.asString()
?: "${it.packageName.asString()}.${it.simpleName.asString()}"
// it.qualifiedName?.asString()?: "${it.packageName.asString()}.${it.simpleName.asString()}"
LazyPeopleHttpVisitor.typeContent
._packageName(it.packageName.asString())
._type(it.simpleName.asString())
}
else
ksType.declaration.simpleName.asString()
//是否可空
val nullable = if (ksType.nullability == Nullability.NULLABLE) LazyPeopleHttpVisitor.nullabilityType else ""
return KSTypeInfo(
Expand All @@ -81,7 +83,7 @@ internal fun getKSTypeArguments(ks: KSTypeReference): List<String> {
//type对象
val ksType = ks.resolve()
//如果是typealias类型
return if (ksType.declaration is KSTypeAlias && LazyPeopleHttpVisitor.typeShowPackage) {
return if (ksType.declaration is KSTypeAlias) {
val kotlinType = getKotlinTypeMethod.invoke(ksType) as KotlinType
kotlinType.arguments.map {
getKotlinTypeInfo(it.type)
Expand Down Expand Up @@ -118,19 +120,11 @@ internal fun getKSTypeOutermostName(ks: KSTypeReference): String {
internal fun getNewAnnotationString(ksa: KSAnnotation): String {
val ksType = ksa.annotationType.resolve()
//完整type字符串
val typeName = if (LazyPeopleHttpVisitor.typeShowPackage)
ksType.declaration.let {
val name = it.qualifiedName?.asString()
if (name != null)
return@let name
val packageName = it.packageName.asString()
return@let if (packageName.isEmpty())
ksa.shortName.asString()
else
"$packageName.${it.simpleName.asString()}"
val typeName = ksType.declaration.let {
return@let LazyPeopleHttpVisitor.typeContent
._packageName(it.packageName.asString())
._type(it.simpleName.asString())
}
else
ksType.declaration.simpleName.asString()
val args = StringBuilder()
ksa.arguments.forEach {
val value = it.value
Expand Down Expand Up @@ -167,7 +161,13 @@ internal fun getNewAnnotationString(ksa: KSAnnotation): String {

//通过[KotlinType]获取完整的泛型信息
private fun getKotlinTypeInfo(type: KotlinType): String {
val typeString = type.getKotlinTypeFqName(false)
var typeString = type.getKotlinTypeFqName(false)
val lastIndex = typeString.lastIndexOf(".")
if (lastIndex > 0) {
typeString = LazyPeopleHttpVisitor.typeContent
._packageName(typeString.substring(0, lastIndex))
._type(typeString.substring(lastIndex + 1))
}
val arguments = type.arguments
if (arguments.isEmpty())
return typeString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ interface CustomizeOutputFileBean {
//[suspendFunContent]等同于[funContent]
val suspendFunEqualsFunContent: Boolean

//类型是否显示包名
val typeShowPackage: Boolean
//类型显示
val typeContent: String

//输出目录
val outputDir: String
Expand Down Expand Up @@ -129,7 +129,7 @@ class CustomizeOutputFileBeanImpl(
" }\n\n",
),
override val suspendFunEqualsFunContent: Boolean = false,
override val typeShowPackage: Boolean = true,
override val typeContent: String = "##packageName##.##type##",
override val outputDir: String = "",
override val nullabilityType: String = "##type##?",
) : CustomizeOutputFileBean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal class LazyPeopleHttpVisitor(
private val json = Json { ignoreUnknownKeys = true }

companion object {
var typeShowPackage = true
var typeContent = ""
var nullabilityType = ""
}

Expand All @@ -69,7 +69,7 @@ internal class LazyPeopleHttpVisitor(
else
beans.addAll(json.decodeFromString<List<CustomizeOutputFileBeanImpl>>(jsonFile.readText()))
beans.forEach { bean ->
typeShowPackage = bean.typeShowPackage
typeContent = bean.typeContent
nullabilityType = bean.nullabilityType
val fileName = bean.fileName._className(className)._originalClassName(originalClassName)
val file = environment.codeGenerator.createNewFile(
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ const val ktorVersion = "3.0.0"//ktor版本
const val serializationJsonVersion = "1.6.3"//json序列号版本
const val kspVersion = "$kotlinVersion-1.0.21"//ksp版本

const val mVersion = "2.1.4"//此库的版本
const val mVersion = "2.1.5"//此库的版本
3 changes: 2 additions & 1 deletion common/customizeOutputFile.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
{
},
{
"fileName": "##className##2",
"extensionName": "js",
"fileTopContent": "import { HttpRequest } from \"../util/HttpUtil\";\n\nclass ##className## {\n\n",
"fileBottomContent": "\tfunction _toJson(value) {\n\t\tif (value == undefined || value == null)\n\t\t\treturn value\n\t\tswitch (typeof(value)) {\n\t\t\tcase \"number\":\n\t\t\tcase \"string\":\n\t\t\tcase \"boolean\":\n\t\t\t\treturn value.toString()\n\t\t\tdefault:\n\t\t\t\treturn JSON.stringify(value)\n\t\t}\n\t}\n\n\tfunction _lazyPeopleHttpFlatten(map) {\n\t\tconst array = []\n\t\tObject.entries(map).forEach((entrie) => {\n\t\t\tarray.push(entrie[0]);\n\t\t\tarray.push(entrie[1])\n\t\t})\n\t\treturn array\n\t}\n}",
"suspendFunEqualsFunContent": true,
"typeShowPackage": false,
"typeContent": "bean.##type##",
"outputDir": "build",
"nullabilityType": "##type## | null",
"funContent": {
Expand Down

0 comments on commit 4ec73d9

Please sign in to comment.