Skip to content

Commit

Permalink
Refactoring some code of Codec (#287)
Browse files Browse the repository at this point in the history
* Refactoring some code of Codec

* explicit type

---------

Co-authored-by: tangjiafu <[email protected]>
  • Loading branch information
laglangyue and laglangyue authored Apr 9, 2024
1 parent 3dcac49 commit 5e070ae
Showing 1 changed file with 4 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ import scala.util.{ Failure, Success, Try }
object Codecs {
// TODO should this list be made user-extensible?
val supportedCodecs = immutable.Seq(Gzip, Identity)
private val supportedNames: Set[String] = supportedCodecs.map(_.name).toSet
private val supportedByName = supportedCodecs.map(c => c.name -> c).toMap
private val supportedByName: Map[String, Codec] = supportedCodecs.map(c => c.name -> c).toMap

private def extractHeaders(request: jm.HttpMessage): Iterable[jm.HttpHeader] = {
request match {
Expand All @@ -47,16 +46,12 @@ object Codecs {
def negotiate(request: jm.HttpRequest): Codec = {
val headers = extractHeaders(request)
val accepted = `Message-Accept-Encoding`.findIn(headers)

if (accepted.length == 0) {
if (accepted.isEmpty) {
Identity
} else if (accepted.length == 1) {
supportedByName.get(accepted(0)) match {
case Some(codec) => codec
case None => Identity
}
supportedByName.getOrElse(accepted.head, Identity)
} else {
accepted.collectFirst { case a if supportedNames.contains(a) => supportedByName(a) }.getOrElse(Identity)
accepted.collectFirst { case a if supportedByName.contains(a) => supportedByName(a) }.getOrElse(Identity)
}
}

Expand Down

0 comments on commit 5e070ae

Please sign in to comment.