-
Notifications
You must be signed in to change notification settings - Fork 40
/
SnsEvent.scala
153 lines (128 loc) · 4.94 KB
/
SnsEvent.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright 2021 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package feral.lambda
package events
import cats.syntax.all._
import io.circe.Decoder
import io.circe.jawn.decode
import io.circe.scodec._
import scodec.bits.ByteVector
import java.time.Instant
import java.util.UUID
final case class SnsEvent(
records: List[SnsEventRecord]
)
object SnsEvent {
implicit val decoder: Decoder[SnsEvent] =
Decoder.forProduct1("Records")(SnsEvent.apply)
}
final case class SnsEventRecord(
eventVersion: String,
eventSubscriptionArn: String,
eventSource: String,
sns: SnsMessage
)
object SnsEventRecord {
implicit val decoder: Decoder[SnsEventRecord] = Decoder.forProduct4(
"EventVersion",
"EventSubscriptionArn",
"EventSource",
"Sns"
)(SnsEventRecord.apply)
}
final case class SnsMessage(
signature: String,
messageId: UUID,
`type`: String,
topicArn: String,
messageAttributes: Map[String, SnsMessageAttribute],
signatureVersion: String,
timestamp: Instant,
signingCertUrl: String,
message: String,
unsubscribeUrl: String,
subject: Option[String]
)
object SnsMessage {
private[this] implicit val instantDecoder: Decoder[Instant] = Decoder.decodeInstant
implicit val decoder: Decoder[SnsMessage] = Decoder.forProduct11(
"Signature",
"MessageId",
"Type",
"TopicArn",
"MessageAttributes",
"SignatureVersion",
"Timestamp",
"SigningCertUrl",
"Message",
"UnsubscribeUrl",
"Subject"
)(SnsMessage.apply)
}
sealed abstract class SnsMessageAttribute
object SnsMessageAttribute {
final case class String(value: Predef.String) extends SnsMessageAttribute
final case class Binary(value: ByteVector) extends SnsMessageAttribute
final case class Number(value: BigDecimal) extends SnsMessageAttribute
final case class StringArray(value: List[SnsMessageAttributeArrayMember])
extends SnsMessageAttribute
final case class Unknown(
`type`: Predef.String,
value: Option[Predef.String]
) extends SnsMessageAttribute
implicit val decoder: Decoder[SnsMessageAttribute] = {
val getString: Decoder[Predef.String] = Decoder.instance(_.get[Predef.String]("Value"))
val getByteVector: Decoder[ByteVector] = Decoder.instance(_.get[ByteVector]("Value"))
val getNumber: Decoder[BigDecimal] = Decoder.instance(_.get[BigDecimal]("Value"))
/*
See https://docs.aws.amazon.com/sns/latest/dg/sns-message-attributes.html
In particular, for both Number and String.Array:
> This data type isn't supported for AWS Lambda subscriptions. If you specify this data type for Lambda endpoints,
> it's passed as the String data type in the JSON payload that Amazon SNS delivers to Lambda."
Circe is smart enough to handle this for BigDecimal, but no such luck for this particular mess.
*/
val getStringArray: Decoder[List[SnsMessageAttributeArrayMember]] =
getString.emapTry(decode[List[SnsMessageAttributeArrayMember]](_).toTry)
Decoder.instance(_.get[Predef.String]("Type")).flatMap {
case "String" => getString.map(SnsMessageAttribute.String.apply)
case "Binary" => getByteVector.map(SnsMessageAttribute.Binary.apply)
case "Number" => getNumber.map(SnsMessageAttribute.Number.apply)
case "String.Array" => getStringArray.map(SnsMessageAttribute.StringArray.apply)
case someType =>
Decoder.instance(i =>
i.get[Option[Predef.String]]("Value").map(SnsMessageAttribute.Unknown(someType, _)))
}
}
}
sealed abstract class SnsMessageAttributeArrayMember
object SnsMessageAttributeArrayMember {
final case class String(value: Predef.String) extends SnsMessageAttributeArrayMember
final case class Number(value: BigDecimal) extends SnsMessageAttributeArrayMember
final case class Boolean(value: scala.Boolean) extends SnsMessageAttributeArrayMember
implicit val decoder: Decoder[SnsMessageAttributeArrayMember] = {
val bool: Decoder[SnsMessageAttributeArrayMember.Boolean] =
Decoder.decodeBoolean.map(SnsMessageAttributeArrayMember.Boolean.apply)
val number: Decoder[SnsMessageAttributeArrayMember.Number] =
Decoder.decodeBigDecimal.map(SnsMessageAttributeArrayMember.Number.apply)
val string: Decoder[SnsMessageAttributeArrayMember.String] =
Decoder.decodeString.map(SnsMessageAttributeArrayMember.String.apply)
List[Decoder[SnsMessageAttributeArrayMember]](
bool.widen,
number.widen,
string.widen
).reduce(_ or _)
}
}