Skip to content

Commit

Permalink
Merge pull request #5 from ltttttttttttt/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
ltttttttttttt authored Jun 26, 2023
2 parents 9ef11a9 + 44ede9d commit d42eaae
Show file tree
Hide file tree
Showing 10 changed files with 355 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ package com.lt.lazy_people_http.annotations
* warning:
*/
@Target(AnnotationTarget.CLASS)
annotation class LazyPeopleHttpService()
annotation class LazyPeopleHttpService
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.lt.lazy_people_http.annotations

/**
* creator: lt 2023/6/21 lt.dygzs@qq.com
* effect : 修饰给接口文件,表示这个文件里的所有方法都会自动加一个中缀
* 相当于方法的url是: BaseUrl + UrlMidSegment的url + 方法的url
* warning:
*/
@Target(AnnotationTarget.CLASS)
annotation class UrlMidSegment(val url: String)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.lt.lazy_people_http.provider

import com.google.devtools.ksp.KspExperimental
import com.google.devtools.ksp.getAnnotationsByType
import com.google.devtools.ksp.getDeclaredFunctions
import com.google.devtools.ksp.processing.Dependencies
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
import com.google.devtools.ksp.symbol.*
Expand Down Expand Up @@ -81,11 +82,16 @@ internal class LazyPeopleHttpVisitor(

//向文件中写入变换后的函数
private fun writeFunction(file: OutputStream, classDeclaration: KSClassDeclaration) {
classDeclaration.getAllFunctions().filter {
classDeclaration.superTypes.mapNotNull {
it.resolve().declaration as? KSClassDeclaration
}.forEach {
writeFunction(file, it)
}
classDeclaration.getDeclaredFunctions().filter {
it.isAbstract
}.forEach {
val functionName = it.simpleName.asString()
val methodInfo = getMethodInfo(it, functionName)
val methodInfo = getMethodInfo(it, functionName, classDeclaration)
val returnType = getKSTypeInfo(it.returnType!!).toString()
val isSuspendFun = Modifier.SUSPEND in it.modifiers
val typeOf =
Expand Down Expand Up @@ -215,16 +221,22 @@ internal class LazyPeopleHttpVisitor(

//获取函数的请求方法相关数据
@OptIn(KspExperimental::class)
private fun getMethodInfo(it: KSFunctionDeclaration, functionName: String): MethodInfo {
private fun getMethodInfo(
it: KSFunctionDeclaration,
functionName: String,
classDeclaration: KSClassDeclaration
): MethodInfo {
val urlMidSegment =
classDeclaration.getAnnotationsByType(UrlMidSegment::class).firstOrNull()?.url ?: ""
val list =
(it.getAnnotationsByType(GET::class) + it.getAnnotationsByType(POST::class)).toList()
if (list.isEmpty())
return MethodInfo(null, functionName.replace("_", "/"))
return MethodInfo(null, urlMidSegment + functionName.replace("_", "/"))
if (list.size > 1)
throw RuntimeException("Function $functionName there are multiple http method annotations")
return when (val annotation = list.first()) {
is GET -> MethodInfo(RequestMethod.GET_QUERY, annotation.url)
is POST -> MethodInfo(RequestMethod.POST_FIELD, annotation.url)
is GET -> MethodInfo(RequestMethod.GET_QUERY, urlMidSegment + annotation.url)
is POST -> MethodInfo(RequestMethod.POST_FIELD, urlMidSegment + annotation.url)
else -> throw RuntimeException("There is a problem with the getMethodInfo function")
}
}
Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ plugins {

dependencies {
...
implementation("io.github.ltttttttttttt:LazyPeopleHttp-lib:$version")//this,such as 1.0.6
ksp("io.github.ltttttttttttt:LazyPeopleHttp:$version")//this,such as 1.0.6
implementation("io.github.ltttttttttttt:LazyPeopleHttp-lib:$version")//this,such as 1.1.1
ksp("io.github.ltttttttttttt:LazyPeopleHttp:$version")//this,such as 1.1.1
}
```

Expand All @@ -59,7 +59,7 @@ plugins {
val commonMain by getting {
dependencies {
...
api("io.github.ltttttttttttt:LazyPeopleHttp-lib:$version")//this,such as 1.0.6
api("io.github.ltttttttttttt:LazyPeopleHttp-lib:$version")//this,such as 1.1.1
}
}

Expand All @@ -77,7 +77,7 @@ Step 2.interface declaration:

```kotlin
@LazyPeopleHttpService
interface HttpFunctions {
interface HttpFunctions : GetHf {
//Standard post request statement
@POST("post/postB")
fun postB(@Field("name") t: String): Call<UserBean>
Expand Down Expand Up @@ -109,6 +109,12 @@ interface HttpFunctions {
//Specific functions can be declared, and no additional methods will be generated at this time
fun ccc(): Int = 0
}

@UrlMidSegment("get/")//All methods in this file will automatically add an infix
interface GetHf {
@GET("getC")//The url equivalent to the method is: BaseUrl + UrlMidSegment url + method url
fun getC2(name: String): Call<NetBean<String>>
}
```

Step 3.interface use:
Expand Down
14 changes: 10 additions & 4 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ plugins {

dependencies {
...
implementation("io.github.ltttttttttttt:LazyPeopleHttp-lib:$version")//this,比如1.0.6
ksp("io.github.ltttttttttttt:LazyPeopleHttp:$version")//this,比如1.0.6
implementation("io.github.ltttttttttttt:LazyPeopleHttp-lib:$version")//this,比如1.1.1
ksp("io.github.ltttttttttttt:LazyPeopleHttp:$version")//this,比如1.1.1
}
```

Expand All @@ -59,7 +59,7 @@ plugins {
val commonMain by getting {
dependencies {
...
api("io.github.ltttttttttttt:LazyPeopleHttp-lib:$version")//this,比如1.0.6
api("io.github.ltttttttttttt:LazyPeopleHttp-lib:$version")//this,比如1.1.1
}
}

Expand All @@ -77,7 +77,7 @@ Step 2.接口声明:

```kotlin
@LazyPeopleHttpService
interface HttpFunctions {
interface HttpFunctions : GetHf {
//标准post请求声明
@POST("post/postB")
fun postB(@Field("name") t: String): Call<UserBean>
Expand Down Expand Up @@ -109,6 +109,12 @@ interface HttpFunctions {
//可以声明具体函数,此时不会生成额外的方法
fun ccc(): Int = 0
}

@UrlMidSegment("get/")//这个文件里的所有方法都会自动加一个中缀
interface GetHf {
@GET("getC")//相当于方法的url是: BaseUrl + UrlMidSegment的url + 方法的url
fun getC2(name: String): Call<NetBean<String>>
}
```

Step 3.接口使用:
Expand Down
6 changes: 3 additions & 3 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/

const val kotlinVersion = "1.8.20"//kotlin版本
const val coroutinesVersion = "1.6.4"//协程版本
const val ktorVersion = "2.2.4"//ktor版本
const val coroutinesVersion = "1.7.1"//协程版本
const val ktorVersion = "2.3.1"//ktor版本
const val serializationJsonVersion = "1.5.0"//json序列号版本
const val kspVersion = "$kotlinVersion-1.0.10"//ksp版本

const val mVersion = "1.0.9"//此库的版本
const val mVersion = "1.1.1"//此库的版本
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ typealias CL<T> = Call<NetBean<List<T>>>
typealias N<T> = NetBean<T>

@LazyPeopleHttpService
interface HttpFunctions {
interface HttpFunctions : PostHf, GetHf {
fun ccc(): Int {
return 0
}
Expand Down Expand Up @@ -58,10 +58,39 @@ interface HttpFunctions {
fun getD(@Url("type") url: String): Call<NetBean<String?>>

@GET("{url}")
fun get(@Url("url") url: String="http://t.weather.sojson.com/api/weather/city/101030100"): Call<MData>
fun get(@Url("url") url: String = "http://t.weather.sojson.com/api/weather/city/101030100"): Call<MData>

@GET("get/getB")
suspend fun suspendGetB(name: String): NetBean<UserBean>
}

suspend fun post_postA(t: String): NetBean<String>
@UrlMidSegment("post/")
interface PostHf {
fun postC(name: String): Call<NetBean<String>>

fun postD(name: String): C<String>

suspend fun postE(name: String): N<String>

fun setUserName(
lastName: String,
@Field("firstName") newName: String
): Call<NetBean<UserBean>>

fun postError(msg: String): Call<NetBean<String?>>

@Header("aaa", "bbb")
fun checkHeader(): Call<NetBean<String?>>

@POST("postA")
suspend fun suspendPostA(t: String): NetBean<String>
}

@UrlMidSegment("get/")
interface GetHf {
@GET("getC")
fun getC2(name: String): Call<NetBean<String>>

@GET("getC3")
fun getC3(name: String): CL<UserBean>
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ fun testAll() {
hf.get().await().cityInfo?.city == "天津市"
)
assert(hf.suspendGetB("2").data.name == "2")
assert(hf.post_postA("123").data == "123")
assert(hf.suspendPostA("123").data == "123")
assert(hf.postC("1").awaitData() == "1")
assert(hf.setUserName("1", "4").awaitData().name == "4 1")
assert(hf.postError("error").await().msg == "error")
assert(hf.checkHeader().awaitData() == "bbb")
assert(hf.getC2("1").awaitData() == "1")
text4 = "测试完成"
}
}
Expand Down
6 changes: 5 additions & 1 deletion desktop/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ compose.desktop {
application {
mainClass = "MainKt"
nativeDistributions {
targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
targetFormats(TargetFormat.Dmg, TargetFormat.Exe, TargetFormat.Deb)
packageName = "LazyPeopleHttp"
packageVersion = "1.0.0"
}
buildTypes.release.proguard {
obfuscate.set(true)//开启混淆
configurationFiles.from(project.file("proguard-rules.pro"))//配置混淆
}
}
}
Loading

0 comments on commit d42eaae

Please sign in to comment.