我有以下代码,无法编译:
Protocol.scala :
object Protocol {
type ProtocolResult[A] = Either[String, A]
}
trait Protocol[A] {
import com.sweetsoft.Protocol.ProtocolResult
def encode(text: String): ProtocolResult[A]
def decode(msg: A): String
}
PcpProtocol.scala:
import cats.implicits._
import com.sweetsoft.Protocol.ProtocolResult
case class PcpProtocol(publicKey: String, topic: String, body: String)
object PcpProtocol {
private type PcpHead = String
private type PcpBody = String
private type PcpKV = String
private val PcpIndicator = "pcp-channel:apc"
private val PcpFlag = "pcp-"
implicit val pcpProtocol: Protocol[PcpProtocol] = new Protocol[PcpProtocol] {
override def encode(text: String): ProtocolResult[PcpProtocol] =
for {
(head, body) <- Either.right(splitRawMsg(text))
pcpValidated <- validatePcp(head)
kv <- Either.right(filterOutPcpFlag(pcpValidated))
(_, pubKey) <- findPublicKey(kv)
(_, topic) <- findTopic(kv)
} yield createPcpProtocol(pubKey)(topic)(body)
override def decode(msg: PcpProtocol): String = ???
}
// Split raw message into body and head
private val splitRawMsg: String => (PcpHead, PcpBody) = (msg: String) => {
msg.split("\n\n").toList match {
case List(headR, bodyR) => (headR, bodyR)
}
}
// Validate, if it is a PCP protocol or not
private val validatePcp: PcpHead => ProtocolResult[PcpHead] = head => {
if (head.contains(PcpIndicator)) {
Right(head)
} else {
Left("The message does not correspond to the PCP protocol.")
}
}
private val filterOutPcpFlag: PcpHead => List[PcpKV] = head => head.split("\n").toList.filter_(!_.contains(PcpFlag))
private val findPublicKey: List[PcpKV] => ProtocolResult[(String, String)] = kv => findKeyValue("PUBLIC:")(kv)
private val findTopic: List[PcpKV] => ProtocolResult[(String, String)] = kv => findKeyValue("TOPIC:")(kv)
private val findKeyValue: String => List[PcpKV] => ProtocolResult[(String, String)] = literal => kv =>
kv
.filter(_.contains(literal))
.foldLeft[Either[String, (String, String)]](Left("Can not find the public key from SAP.")) { (_, a) =>
a.split(":").toList match {
case List(key, value) => Right((key, value))
}
}
private val createPcpProtocol: String => String => String => PcpProtocol =
publicKey =>
topic =>
body =>
PcpProtocol(publicKey, topic, body)
}
编译器抱怨:
value withFilter is not a member of Either[Nothing,(com.sweetsoft.PcpProtocol.PcpHead, com.sweetsoft.PcpProtocol.PcpBody)]
[error] (head, body) <- Either.right(splitRawMsg(text))
我在做什么错了?