@@ -91,6 +91,11 @@ internal fun ObjectMessage.writeMsgpack(packer: MessagePacker) {
9191 * Read an ObjectMessage from MessageUnpacker
9292 */
9393internal fun readObjectMessage (unpacker : MessageUnpacker ): ObjectMessage {
94+ if (unpacker.nextFormat == MessageFormat .NIL ) {
95+ unpacker.unpackNil()
96+ return ObjectMessage () // default/empty message
97+ }
98+
9499 val fieldCount = unpacker.unpackMapHeader()
95100
96101 var id: String? = null
@@ -105,7 +110,7 @@ internal fun readObjectMessage(unpacker: MessageUnpacker): ObjectMessage {
105110
106111 for (i in 0 until fieldCount) {
107112 val fieldName = unpacker.unpackString().intern()
108- val fieldFormat = unpacker.getNextFormat()
113+ val fieldFormat = unpacker.nextFormat
109114
110115 if (fieldFormat == MessageFormat .NIL ) {
111116 unpacker.unpackNil()
@@ -207,7 +212,7 @@ private fun ObjectOperation.writeMsgpack(packer: MessagePacker) {
207212private fun readObjectOperation (unpacker : MessageUnpacker ): ObjectOperation {
208213 val fieldCount = unpacker.unpackMapHeader()
209214
210- var action: ObjectOperationAction = ObjectOperationAction . MapCreate // Default value
215+ var action: ObjectOperationAction ? = null
211216 var objectId: String = " "
212217 var mapOp: ObjectMapOp ? = null
213218 var counterOp: ObjectCounterOp ? = null
@@ -219,16 +224,19 @@ private fun readObjectOperation(unpacker: MessageUnpacker): ObjectOperation {
219224
220225 for (i in 0 until fieldCount) {
221226 val fieldName = unpacker.unpackString().intern()
222- val fieldFormat = unpacker.getNextFormat()
227+ val fieldFormat = unpacker.nextFormat
223228
224229 if (fieldFormat == MessageFormat .NIL ) {
225230 unpacker.unpackNil()
226231 continue
227232 }
228233
229234 when (fieldName) {
230- " action" -> action = ObjectOperationAction .entries.find { it.code == unpacker.unpackInt() }
231- ? : throw IllegalArgumentException (" Unknown ObjectOperationAction code" )
235+ " action" -> {
236+ val actionCode = unpacker.unpackInt()
237+ action = ObjectOperationAction .entries.find { it.code == actionCode }
238+ ? : throw IllegalArgumentException (" Unknown ObjectOperationAction code: $actionCode " )
239+ }
232240 " objectId" -> objectId = unpacker.unpackString()
233241 " mapOp" -> mapOp = readObjectMapOp(unpacker)
234242 " counterOp" -> counterOp = readObjectCounterOp(unpacker)
@@ -246,6 +254,10 @@ private fun readObjectOperation(unpacker: MessageUnpacker): ObjectOperation {
246254 }
247255 }
248256
257+ if (action == null ) {
258+ throw IllegalArgumentException (" Missing required 'action' field in ObjectOperation" )
259+ }
260+
249261 return ObjectOperation (
250262 action = action,
251263 objectId = objectId,
@@ -315,7 +327,7 @@ private fun readObjectState(unpacker: MessageUnpacker): ObjectState {
315327
316328 for (i in 0 until fieldCount) {
317329 val fieldName = unpacker.unpackString().intern()
318- val fieldFormat = unpacker.getNextFormat()
330+ val fieldFormat = unpacker.nextFormat
319331
320332 if (fieldFormat == MessageFormat .NIL ) {
321333 unpacker.unpackNil()
@@ -382,7 +394,7 @@ private fun readObjectMapOp(unpacker: MessageUnpacker): ObjectMapOp {
382394
383395 for (i in 0 until fieldCount) {
384396 val fieldName = unpacker.unpackString().intern()
385- val fieldFormat = unpacker.getNextFormat()
397+ val fieldFormat = unpacker.nextFormat
386398
387399 if (fieldFormat == MessageFormat .NIL ) {
388400 unpacker.unpackNil()
@@ -425,7 +437,7 @@ private fun readObjectCounterOp(unpacker: MessageUnpacker): ObjectCounterOp {
425437
426438 for (i in 0 until fieldCount) {
427439 val fieldName = unpacker.unpackString().intern()
428- val fieldFormat = unpacker.getNextFormat()
440+ val fieldFormat = unpacker.nextFormat
429441
430442 if (fieldFormat == MessageFormat .NIL ) {
431443 unpacker.unpackNil()
@@ -478,16 +490,19 @@ private fun readObjectMap(unpacker: MessageUnpacker): ObjectMap {
478490
479491 for (i in 0 until fieldCount) {
480492 val fieldName = unpacker.unpackString().intern()
481- val fieldFormat = unpacker.getNextFormat()
493+ val fieldFormat = unpacker.nextFormat
482494
483495 if (fieldFormat == MessageFormat .NIL ) {
484496 unpacker.unpackNil()
485497 continue
486498 }
487499
488500 when (fieldName) {
489- " semantics" -> semantics = MapSemantics .entries.find { it.code == unpacker.unpackInt() }
490- ? : throw IllegalArgumentException (" Unknown MapSemantics code" )
501+ " semantics" -> {
502+ val semanticsCode = unpacker.unpackInt()
503+ semantics = MapSemantics .entries.find { it.code == semanticsCode }
504+ ? : throw IllegalArgumentException (" Unknown MapSemantics code: $semanticsCode " )
505+ }
491506 " entries" -> {
492507 val mapSize = unpacker.unpackMapHeader()
493508 val tempMap = mutableMapOf<String , ObjectMapEntry >()
@@ -531,7 +546,7 @@ private fun readObjectCounter(unpacker: MessageUnpacker): ObjectCounter {
531546
532547 for (i in 0 until fieldCount) {
533548 val fieldName = unpacker.unpackString().intern()
534- val fieldFormat = unpacker.getNextFormat()
549+ val fieldFormat = unpacker.nextFormat
535550
536551 if (fieldFormat == MessageFormat .NIL ) {
537552 unpacker.unpackNil()
@@ -587,7 +602,7 @@ private fun readObjectMapEntry(unpacker: MessageUnpacker): ObjectMapEntry {
587602
588603 for (i in 0 until fieldCount) {
589604 val fieldName = unpacker.unpackString().intern()
590- val fieldFormat = unpacker.getNextFormat()
605+ val fieldFormat = unpacker.nextFormat
591606
592607 if (fieldFormat == MessageFormat .NIL ) {
593608 unpacker.unpackNil()
@@ -667,7 +682,7 @@ private fun readObjectData(unpacker: MessageUnpacker): ObjectData {
667682
668683 for (i in 0 until fieldCount) {
669684 val fieldName = unpacker.unpackString().intern()
670- val fieldFormat = unpacker.getNextFormat()
685+ val fieldFormat = unpacker.nextFormat
671686
672687 if (fieldFormat == MessageFormat .NIL ) {
673688 unpacker.unpackNil()
0 commit comments