我正在尝试操纵数据的结构,以便可以将其传递给此功能fun handleRequests(messages: Map<MessageDestination, List<MessageSender>>): Either<Errors.RequestError, Unit>
。但我不断收到此错误:
syntaxerror
Error:(27, 63) Kotlin: Type inference failed. Expected type mismatch: inferred type is Map<MessageDestination, List<Pair<MessageSender, String>>> but Map<MessageDestination, List<MessageSender>> was expected
。
我需要将数据转换为Map<MessageDestination, List<MessageSender>>
,但我不知道该怎么做。这是代码:
package testp.package1.handlers
import arrow.core.Either
import arrow.core.flatMap
import com.amazonaws.services.lambda.runtime.Context
import com.amazonaws.services.lambda.runtime.RequestHandler
interface InterfaceService {
fun handleRequests(messages: Map<MessageDestination, List<MessageSender>>): Either<Errors.RequestError, Unit>
}
abstract class AbstractMessageHandler(
override val service: InterfaceService =
ServiceImpl()) : MessageHandler<MyMessage>() {
abstract val emailType: ServiceImpl.Companion.EmailType
override val emailParser: IMessageParser<MyMessage> = M2MessageParser()
override fun handle(event: List<String>): Either<Errors.RequestError, Pair<List<Errors.RequestError>, Int>> =
emailParser.parseEmails(event)
.map { (error, messages) ->
error to messages.map { myMessage ->
MessageSender(message = myMessage.environment) to myMessage.emailAdrress
}
}
.flatMap { (errors: List<Errors.RequestError>, emailMesssages: List<Pair<MessageSender, String>>) ->
service.handleRequests(emailMesssages.groupBy { MessageDestination(it.second) }).map {
Pair(errors, emailMesssages.size)
}
}
}
data class MessageDestination(val emailAddress: String)
data class MyMessage(val environment: String, val emailAdrress: String, val phoneId: String)
data class MessageSender(val message: String)
class ServiceImpl : InterfaceService {
override fun handleRequests(messages: Map<MessageDestination, List<MessageSender>>): Either<Errors.RequestError, Unit> {
TODO("logic goes her")
}
companion object {
enum class EmailType {
M1, M2
}
}
}
object Errors {
interface RequestError {
val message: String
}
data class UnexpectedError(override val message: String) : RequestError
}
答案 0 :(得分:1)
TL; DR: 您要转换分组在列表中的值,所以:
emailMesssages.groupBy(
{ MessageDestination(it.second) },
{ it.first }
)
长版:
好的,所以从以下内容开始:
emailMesssages: List<Pair<MessageSender, String>>
并且您想要将其转换为键入Map<MessageDestination, List<MessageSender>>
您正在尝试通过emailMesssages.groupBy { MessageDestination(it.second) }
实现这一目标。 groupBy
会按照您指定的键将列表中的项目分组。重要的是,它会将一个键的所有项(在这种情况下为所有Pair
)合并到列表中。
如果有的话(来自official docs的示例):
val words = listOf("a", "abc", "ab", "def", "bc")
val byLength = words.groupBy { it.length }
那么byLength是:
1 -> listOf("a")
2 -> listOf("ab", "bc")
3 -> listOf("abc", "def")
这说明了为什么您输入Map<MessageDestination, List<Pair<MessageSender, String>>>
而不是Map<MessageDestination, List<MessageSender>>
的原因。
您要做的是不对列表中的项目进行分组,而是要对列表中的项目进行转换后的值。
基本上,您想要的是'groupBy'的另一个变体,它具有keySelector和valueTransform:
inline fun <T, K, V> Array<out T>.groupBy(
keySelector: (T) -> K,
valueTransform: (T) -> V
): Map<K, List<V>> (source)`
在您的情况下,看起来像这样:
emailMesssages.groupBy(
{ MessageDestination(it.second) },
{ it.first }
)