Skip to content

Commit

Permalink
Fix docs and adds tests for Headers (#975)
Browse files Browse the repository at this point in the history
* Fix docs and adds tests for Headers
  • Loading branch information
geirolz authored Sep 13, 2024
1 parent da53f7f commit 10e79f1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand Down Expand Up @@ -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]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 10e79f1

Please sign in to comment.