This repository has been archived by the owner on Aug 12, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
54 changed files
with
380 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
annotations/src/main/java/moe/fuqiuluo/symbols/Protobuf.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package moe.fuqiuluo.symbols | ||
|
||
import kotlinx.serialization.decodeFromByteArray | ||
import kotlinx.serialization.protobuf.ProtoBuf | ||
|
||
import kotlin.reflect.KClass | ||
|
||
interface Protobuf<T: Protobuf<T>> | ||
|
||
inline fun <reified T: Protobuf<T>> KClass<T>.decode(data: ByteArray): T { | ||
return ProtoBuf.decodeFromByteArray(data) | ||
} | ||
|
||
inline fun <reified T: Protobuf<T>> ByteArray.decodeProtobuf(to: KClass<T>? = null): T { | ||
return ProtoBuf.decodeFromByteArray(this) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
processor/src/main/java/moe/fuqiuluo/ksp/impl/ProtobufProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
@file:Suppress("UNCHECKED_CAST") | ||
package moe.fuqiuluo.ksp.impl | ||
|
||
import com.google.devtools.ksp.isInternal | ||
import com.google.devtools.ksp.isPrivate | ||
import com.google.devtools.ksp.processing.CodeGenerator | ||
import com.google.devtools.ksp.processing.Dependencies | ||
import com.google.devtools.ksp.processing.KSPLogger | ||
import com.google.devtools.ksp.processing.Resolver | ||
import com.google.devtools.ksp.processing.SymbolProcessor | ||
import com.google.devtools.ksp.symbol.ClassKind | ||
import com.google.devtools.ksp.symbol.KSAnnotated | ||
import com.google.devtools.ksp.symbol.KSClassDeclaration | ||
import com.google.devtools.ksp.symbol.KSDeclaration | ||
import com.google.devtools.ksp.validate | ||
import com.squareup.kotlinpoet.FileSpec | ||
import kotlinx.serialization.Serializable | ||
|
||
class ProtobufProcessor( | ||
private val codeGenerator: CodeGenerator, | ||
private val logger: KSPLogger | ||
): SymbolProcessor { | ||
override fun process(resolver: Resolver): List<KSAnnotated> { | ||
val symbols = resolver.getSymbolsWithAnnotation(Serializable::class.qualifiedName!!) | ||
val unableToProcess = symbols.filterNot { it.validate() } | ||
val actions = (symbols.filter { | ||
it is KSClassDeclaration && it.validate() && it.classKind == ClassKind.CLASS | ||
} as Sequence<KSClassDeclaration>).filter { | ||
it.superTypes.any { superType -> | ||
superType.resolve().declaration.qualifiedName?.asString() == "moe.fuqiuluo.symbols.Protobuf" | ||
} | ||
}.toList() | ||
|
||
if (actions.isNotEmpty()) { | ||
actions.forEach { clz -> | ||
if (clz.isInternal()) return@forEach | ||
if (clz.isPrivate()) return@forEach | ||
|
||
val packageName = "protobuf.auto" | ||
val fileSpecBuilder = FileSpec.scriptBuilder("FastProtobuf", packageName) | ||
|
||
fileSpecBuilder.addImport("kotlinx.serialization.protobuf", "ProtoBuf") | ||
fileSpecBuilder.addImport("kotlinx.serialization", "decodeFromByteArray") | ||
fileSpecBuilder.addImport("kotlinx.serialization", "encodeToByteArray") | ||
|
||
if (clz.parentDeclaration != null) { | ||
fileSpecBuilder.addImport(clz.importPackage, clz.simpleName.asString()) | ||
} else { | ||
fileSpecBuilder.addImport(clz.packageName.asString(), clz.simpleName.asString()) | ||
} | ||
if (clz.typeParameters.isNotEmpty()) { | ||
val genericType = clz.typeParameters.joinToString(", ") { it.name.asString() } | ||
fileSpecBuilder.addStatement("""inline fun <$genericType> ${clz.simpleName.asString()}<$genericType>.toByteArray() = ProtoBuf.encodeToByteArray(this)""") | ||
} else { | ||
fileSpecBuilder.addStatement("inline fun ${clz.simpleName.asString()}.toByteArray() = ProtoBuf.encodeToByteArray(this)") | ||
} | ||
|
||
codeGenerator.createNewFile( | ||
dependencies = Dependencies.ALL_FILES, | ||
packageName = packageName, | ||
fileName = clz.simpleName.asString() + "\$FP" | ||
).use { outputStream -> | ||
outputStream.writer().use { | ||
fileSpecBuilder.build().writeTo(it) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return unableToProcess.toList() | ||
} | ||
|
||
private val KSDeclaration.importPackage: String | ||
get() = if (parentDeclaration != null) { | ||
parentDeclaration!!.importPackage + "." + parentDeclaration!!.simpleName.asString() | ||
} else packageName.asString() | ||
} |
17 changes: 17 additions & 0 deletions
17
processor/src/main/java/moe/fuqiuluo/ksp/providers/ProtobufProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package moe.fuqiuluo.ksp.providers | ||
|
||
import com.google.auto.service.AutoService | ||
import com.google.devtools.ksp.processing.SymbolProcessor | ||
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment | ||
import com.google.devtools.ksp.processing.SymbolProcessorProvider | ||
import moe.fuqiuluo.ksp.impl.ProtobufProcessor | ||
|
||
@AutoService(SymbolProcessorProvider::class) | ||
class ProtobufProvider: SymbolProcessorProvider { | ||
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor { | ||
return ProtobufProcessor( | ||
environment.codeGenerator, | ||
environment.logger | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
protobuf/src/main/java/protobuf/lightapp/AdaptShareInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
@file:OptIn(ExperimentalSerializationApi::class) | ||
package protobuf.lightapp | ||
|
||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.protobuf.ProtoNumber | ||
import moe.fuqiuluo.symbols.Protobuf | ||
|
||
@Serializable | ||
data class AdaptShareInfoReq( | ||
//@ProtoNumber(1) var extInfo: Any? = null, | ||
@ProtoNumber(2) var appid: String? = null, | ||
@ProtoNumber(3) var title: String? = null, | ||
@ProtoNumber(4) var desc: String? = null, | ||
@ProtoNumber(5) var time: ULong? = null, | ||
@ProtoNumber(6) var scene: UInt? = null, | ||
@ProtoNumber(7) var templetType: UInt? = null, | ||
@ProtoNumber(8) var businessType: UInt? = null, | ||
@ProtoNumber(9) var picUrl: String? = null, | ||
@ProtoNumber(10) var vidUrl: String? = null, | ||
@ProtoNumber(11) var jumpUrl: String? = null, | ||
@ProtoNumber(12) var iconUrl: String? = null, | ||
@ProtoNumber(13) var verType: UInt? = null, | ||
@ProtoNumber(14) var shareType: UInt? = null, | ||
@ProtoNumber(15) var versionId: String? = null, | ||
@ProtoNumber(16) var withShareTicket: UInt? = null, | ||
@ProtoNumber(17) var webURL: String? = null, | ||
//@ProtoNumber(18) var appidRich: Any? = null, | ||
@ProtoNumber(19) var template: Template? = null, | ||
//@ProtoNumber(20) var rcvOpenId: Any? = null, | ||
): Protobuf<AdaptShareInfoReq> | ||
|
||
@Serializable | ||
data class Template( | ||
@ProtoNumber(1) var templateId: UInt? = null, | ||
@ProtoNumber(2) var templateData: ByteArray? = null, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.