Fix TarsDecoder

fix #762
This commit is contained in:
Karlatemp 2021-01-03 22:24:28 +08:00
parent 9c60961271
commit 1eecefd16e
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
2 changed files with 34 additions and 14 deletions

View File

@ -58,7 +58,7 @@ internal class TarsDecoder(
} }
override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder { override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder {
this@TarsDecoder.pushTag(TarsTagListElement) this@TarsDecoder.pushTag(TarsTagListElement())
return this@TarsDecoder.beginStructure(descriptor) return this@TarsDecoder.beginStructure(descriptor)
} }
@ -79,7 +79,9 @@ internal class TarsDecoder(
override fun decodeCollectionSize(descriptor: SerialDescriptor): Int { override fun decodeCollectionSize(descriptor: SerialDescriptor): Int {
// 不要读下一个 head // 不要读下一个 head
return input.currentHead.let { input.readTarsIntValue(it) } return input.currentHead.let { input.readTarsIntValue(it) }.also {
println { "SimpleByteArrayReader.decodeCollectionSize: $it" }
}
} }
} }
@ -96,7 +98,7 @@ internal class TarsDecoder(
} }
override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder { override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder {
this@TarsDecoder.pushTag(TarsTagListElement) this@TarsDecoder.pushTag(TarsTagListElement())
return this@TarsDecoder.beginStructure(descriptor) return this@TarsDecoder.beginStructure(descriptor)
} }
@ -130,6 +132,7 @@ internal class TarsDecoder(
override fun decodeElementIndex(descriptor: SerialDescriptor): Int = error("stub") override fun decodeElementIndex(descriptor: SerialDescriptor): Int = error("stub")
override fun endStructure(descriptor: SerialDescriptor) { override fun endStructure(descriptor: SerialDescriptor) {
println { "MapReader.endStructure: ${input.currentHeadOrNull}" }
this@TarsDecoder.endStructure(descriptor) this@TarsDecoder.endStructure(descriptor)
} }
@ -137,9 +140,11 @@ internal class TarsDecoder(
println { "MapReader.beginStructure: ${input.currentHead}" } println { "MapReader.beginStructure: ${input.currentHead}" }
this@TarsDecoder.pushTag( this@TarsDecoder.pushTag(
when (input.currentHead.tag) { when (input.currentHead.tag) {
0 -> TarsTagMapEntryKey 0 -> TarsTagMapEntryKey()
1 -> TarsTagMapEntryValue 1 -> TarsTagMapEntryValue()
else -> error("illegal map entry head: ${input.currentHead.tag}") else -> error("illegal map entry head: ${input.currentHead.tag}")
}.also {
println("MapReader.pushTag $it")
} }
) )
return this@TarsDecoder.beginStructure(descriptor) return this@TarsDecoder.beginStructure(descriptor)
@ -155,20 +160,30 @@ internal class TarsDecoder(
override fun decodeBoolean(): Boolean = input.useHead { input.readTarsBooleanValue(it) } override fun decodeBoolean(): Boolean = input.useHead { input.readTarsBooleanValue(it) }
override fun decodeChar(): Char = decodeByte().toChar() override fun decodeChar(): Char = decodeByte().toChar()
override fun decodeEnum(enumDescriptor: SerialDescriptor): Int = decodeInt() override fun decodeEnum(enumDescriptor: SerialDescriptor): Int = decodeInt()
override fun decodeString(): String = input.useHead { input.readTarsStringValue(it) } override fun decodeString(): String = input.useHead { head ->
input.readTarsStringValue(head).also {
println { "MapReader.decodeString: $it" }
}
}
override fun decodeCollectionSize(descriptor: SerialDescriptor): Int { override fun decodeCollectionSize(descriptor: SerialDescriptor): Int {
println { "decodeCollectionSize in MapReader: ${descriptor.serialName}" } println { "decodeCollectionSize in MapReader: ${descriptor.serialName}" }
// 不读下一个 head // 不读下一个 head
return input.useHead { input.readTarsIntValue(it) } return input.useHead { head ->
input.readTarsIntValue(head).also {
println { "decodeCollectionSize = $it" }
}
}
} }
} }
override fun endStructure(descriptor: SerialDescriptor) { override fun endStructure(descriptor: SerialDescriptor) {
structureHierarchy-- structureHierarchy--
println { "endStructure: ${descriptor.serialName}" } println { "endStructure: ${descriptor.serialName}, $currentTagOrNull, ${input.currentHeadOrNull}" }
if (currentTagOrNull?.isSimpleByteArray == true) { if (currentTagOrNull?.isSimpleByteArray == true) {
println { "endStructure: prepareNextHead() called" }
currentTag.isSimpleByteArray = false
input.prepareNextHead() // read to next head input.prepareNextHead() // read to next head
} }
if (descriptor.kind == StructureKind.CLASS) { if (descriptor.kind == StructureKind.CLASS) {
@ -215,14 +230,14 @@ internal class TarsDecoder(
override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder { override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder {
println() println()
println { "beginStructure: ${descriptor.serialName}" } println { "beginStructure: ${descriptor.serialName}, ${descriptor.kind}" }
structureHierarchy++ structureHierarchy++
return when (descriptor.kind) { return when (descriptor.kind) {
is PrimitiveKind -> this@TarsDecoder is PrimitiveKind -> this@TarsDecoder
StructureKind.MAP -> { StructureKind.MAP -> {
//println("!! MAP")
val tag = popTag() val tag = popTag()
// println("!! MAP, tag=$tag")
return input.skipToHeadAndUseIfPossibleOrFail(tag.id) { return input.skipToHeadAndUseIfPossibleOrFail(tag.id) {
it.checkType(Tars.MAP, "beginStructure", tag, descriptor) it.checkType(Tars.MAP, "beginStructure", tag, descriptor)
MapReader MapReader
@ -315,7 +330,9 @@ internal class TarsDecoder(
} }
override fun decodeTaggedByte(tag: TarsTag): Byte = override fun decodeTaggedByte(tag: TarsTag): Byte =
kotlin.runCatching { input.skipToHeadAndUseIfPossibleOrFail(tag.id) { input.readTarsByteValue(it) } }.getOrElse { kotlin.runCatching {
input.skipToHeadAndUseIfPossibleOrFail(tag.id) { input.readTarsByteValue(it) }
}.getOrElse {
throw IllegalStateException("$tag", it) throw IllegalStateException("$tag", it)
} }

View File

@ -20,21 +20,24 @@ internal abstract class TarsTag {
internal var isSimpleByteArray: Boolean = false internal var isSimpleByteArray: Boolean = false
} }
internal object TarsTagListElement : TarsTag() { // For thread-safely, Don't use object
internal class TarsTagListElement : TarsTag() {
override val id: Int get() = 0 override val id: Int get() = 0
override fun toString(): String { override fun toString(): String {
return "TarsTagListElement" return "TarsTagListElement"
} }
} }
internal object TarsTagMapEntryKey : TarsTag() { // For thread-safely, Don't use object
internal class TarsTagMapEntryKey : TarsTag() {
override val id: Int get() = 0 override val id: Int get() = 0
override fun toString(): String { override fun toString(): String {
return "TarsTagMapEntryKey" return "TarsTagMapEntryKey"
} }
} }
internal object TarsTagMapEntryValue : TarsTag() { // For thread-safely, Don't use object
internal class TarsTagMapEntryValue : TarsTag() {
override val id: Int get() = 1 override val id: Int get() = 1
override fun toString(): String { override fun toString(): String {
return "TarsTagMapEntryValue" return "TarsTagMapEntryValue"