From 10e79f158e8cff705aec46c3bdb14946cd56460c Mon Sep 17 00:00:00 2001 From: David Geirola Date: Fri, 13 Sep 2024 11:45:11 +0200 Subject: [PATCH] Fix docs and adds tests for Headers (#975) * Fix docs and adds tests for Headers --- .../scala/dev/profunktor/fs2rabbit/model/Headers.scala | 4 ++-- .../dev/profunktor/fs2rabbit/model/ShortString.scala | 8 +++++--- .../fs2rabbit/model/codec/AmqpFieldIsoCodecSpec.scala | 5 ++++- .../profunktor/fs2rabbit/testing/AmqpPropertiesArbs.scala | 6 ++++++ 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/core/src/main/scala/dev/profunktor/fs2rabbit/model/Headers.scala b/core/src/main/scala/dev/profunktor/fs2rabbit/model/Headers.scala index 5c5fb7f4..26261985 100644 --- a/core/src/main/scala/dev/profunktor/fs2rabbit/model/Headers.scala +++ b/core/src/main/scala/dev/profunktor/fs2rabbit/model/Headers.scala @@ -67,8 +67,8 @@ import scala.util.Try * * .remove("x-custom-header-3") * */ * - * headers.getRaw[IO]("x-custom-header") // IO(StringVal("value")) - * headers.getOptRaw("x-custom-header") // Some(StringVal("value")) + * headers.get[IO]("x-custom-header") // IO(StringVal("value")) + * headers.getOpt("x-custom-header") // Some(StringVal("value")) * * headers.getAs[IO, String]("x-custom-header") // IO("value") * headers.getOptAs[String]("x-custom-header") // Some("value") diff --git a/core/src/main/scala/dev/profunktor/fs2rabbit/model/ShortString.scala b/core/src/main/scala/dev/profunktor/fs2rabbit/model/ShortString.scala index 3283418b..62bd0a4c 100644 --- a/core/src/main/scala/dev/profunktor/fs2rabbit/model/ShortString.scala +++ b/core/src/main/scala/dev/profunktor/fs2rabbit/model/ShortString.scala @@ -26,12 +26,14 @@ sealed abstract case class ShortString private (str: String) object ShortString { val MaxByteLength = 255 + def isValid(str: String): Boolean = + str.getBytes("utf-8").length <= MaxByteLength + def from(str: String): Option[ShortString] = - if (str.getBytes("utf-8").length <= MaxByteLength) { + if(isValid(str)) Some(new ShortString(str) {}) - } else { + else None - } /** This bypasses the safety check that [[from]] has. This is meant only for situations where you are certain the * string cannot be larger than [[MaxByteLength]] (e.g. string literals). diff --git a/core/src/test/scala/dev/profunktor/fs2rabbit/model/codec/AmqpFieldIsoCodecSpec.scala b/core/src/test/scala/dev/profunktor/fs2rabbit/model/codec/AmqpFieldIsoCodecSpec.scala index 5d5b001c..8e058e00 100644 --- a/core/src/test/scala/dev/profunktor/fs2rabbit/model/codec/AmqpFieldIsoCodecSpec.scala +++ b/core/src/test/scala/dev/profunktor/fs2rabbit/model/codec/AmqpFieldIsoCodecSpec.scala @@ -17,7 +17,7 @@ package dev.profunktor.fs2rabbit.model.codec import cats.data.{NonEmptyList, NonEmptySeq} -import dev.profunktor.fs2rabbit.model.AmqpFieldValue +import dev.profunktor.fs2rabbit.model.{AmqpFieldValue, ShortString} import dev.profunktor.fs2rabbit.model.AmqpFieldValue._ import dev.profunktor.fs2rabbit.model.codec.AmqpFieldDecoder.DecodingError import dev.profunktor.fs2rabbit.testing._ @@ -79,6 +79,9 @@ class AmqpFieldIsoCodecSpec extends AnyFunSuite with Matchers { testAmqpFieldCodecIso[List[Int]] testAmqpFieldCodecIso[Set[Int]] + // map + testAmqpFieldCodecIso[Map[ShortString, AmqpFieldValue]] + // cats collections testAmqpFieldCodecIso[NonEmptyList[Int]] testAmqpFieldCodecIso[NonEmptySeq[Int]] diff --git a/core/src/test/scala/dev/profunktor/fs2rabbit/testing/AmqpPropertiesArbs.scala b/core/src/test/scala/dev/profunktor/fs2rabbit/testing/AmqpPropertiesArbs.scala index c1817bbe..d3d7cd0d 100644 --- a/core/src/test/scala/dev/profunktor/fs2rabbit/testing/AmqpPropertiesArbs.scala +++ b/core/src/test/scala/dev/profunktor/fs2rabbit/testing/AmqpPropertiesArbs.scala @@ -140,6 +140,12 @@ object AmqpPropertiesArbs { ) .map(Headers(_)) + implicit val arbShortString: Arbitrary[ShortString] = Arbitrary[ShortString] { + Gen.alphaStr + .filter(ShortString.isValid) + .map(ShortString.unsafeFrom) + } + implicit val amqpProperties: Arbitrary[AmqpProperties] = Arbitrary[AmqpProperties] { for { contentType <- Gen.option(Gen.alphaStr)