fix proto

This commit is contained in:
Simplxs 2023-10-27 17:32:02 +08:00
parent 617874fea1
commit 85cb9d221c
No known key found for this signature in database
GPG Key ID: E23537FF14DD6507
3 changed files with 9 additions and 7 deletions

View File

@ -60,7 +60,7 @@ val ProtoValue.asList: ProtoList
val ProtoValue.asByteArray: ByteArray val ProtoValue.asByteArray: ByteArray
get() = if (this is ProtoMap) { get() = if (this is ProtoMap) {
toByteArray() bytes?.toByteArray() ?: toByteArray()
} else { } else {
(this as ProtoByteString).toByteArray() (this as ProtoByteString).toByteArray()
} }

View File

@ -7,9 +7,11 @@ import kotlinx.serialization.json.JsonElement
import moe.fuqiuluo.shamrock.tools.jsonObject import moe.fuqiuluo.shamrock.tools.jsonObject
class ProtoMap( class ProtoMap(
val value: HashMap<Int, ProtoValue> val value: HashMap<Int, ProtoValue>,
val bytes: ByteString?
): ProtoValue { ): ProtoValue {
constructor(): this(hashMapOf()) constructor(): this(hashMapOf(), null)
constructor(value: HashMap<Int, ProtoValue>): this(value, null)
override fun has(vararg tags: Int): Boolean { override fun has(vararg tags: Int): Boolean {
var curMap: ProtoMap = this var curMap: ProtoMap = this
@ -83,7 +85,7 @@ class ProtoMap(
return@forEachIndexed return@forEachIndexed
} }
if (!curProtoMap.contains(tag)) { if (!curProtoMap.contains(tag)) {
val tmp = ProtoMap(hashMapOf()) val tmp = ProtoMap()
curProtoMap[tag] = tmp curProtoMap[tag] = tmp
curProtoMap = tmp curProtoMap = tmp
} else { } else {

View File

@ -7,7 +7,7 @@ import com.google.protobuf.UnknownFieldSet
object ProtoUtils { object ProtoUtils {
fun decodeFromByteArray(data: ByteArray): ProtoMap { fun decodeFromByteArray(data: ByteArray): ProtoMap {
val unknownFieldSet = UnknownFieldSet.parseFrom(data) val unknownFieldSet = UnknownFieldSet.parseFrom(data)
val dest = ProtoMap(hashMapOf()) val dest = ProtoMap(hashMapOf(), ByteString.copyFrom(data))
printUnknownFieldSet(unknownFieldSet, dest) printUnknownFieldSet(unknownFieldSet, dest)
return dest return dest
} }
@ -97,7 +97,7 @@ object ProtoUtils {
field.lengthDelimitedList.forEach { field.lengthDelimitedList.forEach {
try { try {
val unknownFieldSet = UnknownFieldSet.parseFrom(it) val unknownFieldSet = UnknownFieldSet.parseFrom(it)
val map = ProtoMap(hashMapOf()) val map = ProtoMap(hashMapOf(), it)
printUnknownFieldSet(unknownFieldSet, map) printUnknownFieldSet(unknownFieldSet, map)
dest[tag] = map dest[tag] = map
} catch (e: Throwable) { } catch (e: Throwable) {
@ -105,7 +105,7 @@ object ProtoUtils {
} }
} }
field.groupList.forEach { field.groupList.forEach {
val map = ProtoMap(hashMapOf()) val map = ProtoMap()
printUnknownFieldSet(it, map) printUnknownFieldSet(it, map)
dest[tag] = map dest[tag] = map
} }