Skip to content
This repository has been archived by the owner on Jul 25, 2023. It is now read-only.

Commit

Permalink
Correct FieldEncoder and FieldDecoder's behavior regarding zeroed uin…
Browse files Browse the repository at this point in the history
…t32:s
  • Loading branch information
rootmos committed May 19, 2018
1 parent 9fb4e3b commit a63c5df
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,18 @@ object FieldDecoder extends MidPriorityFieldDecoder {
*
* @group Utilities
*/
final private[protoless] def native[A](r: CIS => A, expectedType: FieldType): RepeatableFieldDecoder[A] = new RepeatableFieldDecoder[A] {
final private[protoless] def native[A](r: CIS => A, expectedType: FieldType, default: Option[A] = None): RepeatableFieldDecoder[A] = new RepeatableFieldDecoder[A] {
override def fieldType: FieldType = expectedType
override def read(input: CIS, index: Int): Result[A] = {
val tag = readTag(input, index)

// Check that the fieldNumber match the current index
if (tag.fieldNumber != index) Left(MissingField(index, expectedType, tag.wireType, tag.fieldNumber))
if (tag.fieldNumber != index) {
default match {
case Some(a) => Right(a)
case _ => Left(MissingField(index, expectedType, tag.wireType, tag.fieldNumber))
}
}
// Check if the type match, except if fieldType=2 for repeatedfields
else if (tag.wireType != 2 && tag.wireType != expectedType.getWireType) Left(WrongFieldType(expectedType, tag.fieldNumber, tag.wireType))
// else try to decode the input
Expand All @@ -233,7 +238,7 @@ object FieldDecoder extends MidPriorityFieldDecoder {
/**
* @group DecodingNative
*/
implicit final val decodeUInt: RepeatableFieldDecoder[Int @@ Unsigned] = native(cis => tag.unsigned(cis.readUInt32()), FieldType.UINT32)
implicit final val decodeUInt: RepeatableFieldDecoder[Int @@ Unsigned] = native(cis => tag.unsigned(cis.readUInt32()), FieldType.UINT32, Some(tag.unsigned(0)))

/**
* @group DecodingNative
Expand Down
14 changes: 10 additions & 4 deletions modules/core/src/main/scala/io/protoless/fields/FieldEncoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ object FieldEncoder extends MidPriorityFieldEncoder {
/**
* @group EncodingNative
*/
implicit final val encodeUnsignedInt: RepeatableFieldEncoder[Int @@ Unsigned] = native(_.writeUInt32, _.writeUInt32NoTag, FieldType.UINT32)
implicit final val encodeUnsignedInt: RepeatableFieldEncoder[Int @@ Unsigned] = native(_.writeUInt32, _.writeUInt32NoTag, FieldType.UINT32, Some(unsigned(0)))

/**
* @group EncodingNative
Expand Down Expand Up @@ -358,13 +358,19 @@ trait FieldEncoderHelpers {
*
* @group Utilities
*/
final protected def native[A](nativeWrite: COS => (Int, A) => Unit, nativeWriteRepeated: COS => A => Unit, nativeFieldType: FieldType): RepeatableFieldEncoder[A] = new RepeatableFieldEncoder[A] {
final protected def native[A](nativeWrite: COS => (Int, A) => Unit, nativeWriteRepeated: COS => A => Unit, nativeFieldType: FieldType, default: Option[A] = None): RepeatableFieldEncoder[A] = new RepeatableFieldEncoder[A] {
override def write(index: Int, a: A, output: COS): Unit = {
nativeWrite(output)(index, a)
default match {
case Some(`a`) =>
case _ => nativeWrite(output)(index, a)
}
}

override def writeRepeated(a: A, output: COS): Unit = {
nativeWriteRepeated(output)(a)
default match {
case Some(`a`) =>
case _ => nativeWriteRepeated(output)(a)
}
}

override def fieldType: FieldType = nativeFieldType
Expand Down

0 comments on commit a63c5df

Please sign in to comment.