Skip to content

Commit bb1e9d2

Browse files
authored
Extract custom type appendix from message attributes (#834)
1 parent 5a87851 commit bb1e9d2

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

rest/rest-sqs/src/main/scala/org/elasticmq/rest/sqs/MessageAttributesSupport.scala

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package org.elasticmq.rest.sqs
22

33
import org.elasticmq.{BinaryMessageAttribute, MessageAttribute, NumberMessageAttribute, StringMessageAttribute}
4-
import spray.json.{JsObject, JsString, JsValue, JsonReader, RootJsonFormat}
54
import spray.json.DefaultJsonProtocol._
5+
import spray.json.{JsObject, JsString, JsValue, JsonReader, RootJsonFormat}
66

77
trait MessageAttributesSupport {
88

9+
private val SomeString = """String\.?(.*)""".r
10+
private val SomeNumber = """Number\.?(.*)""".r
11+
private val SomeBinary = """Binary\.?(.*)""".r
12+
913
implicit val messageAttributeJsonFormat: RootJsonFormat[MessageAttribute] = new RootJsonFormat[MessageAttribute] {
1014

1115
override def write(obj: MessageAttribute): JsValue = obj match {
@@ -25,11 +29,15 @@ trait MessageAttributesSupport {
2529

2630
val dataType = pickField[String]("DataType")
2731
dataType match {
28-
case "Number" => NumberMessageAttribute(pickField[String]("StringValue"))
29-
case "String" => StringMessageAttribute(pickField[String]("StringValue"))
30-
case "Binary" => BinaryMessageAttribute(pickField[Array[Byte]]("BinaryValue"))
32+
case SomeNumber(ct) => NumberMessageAttribute(pickField[String]("StringValue"), customType(ct))
33+
case SomeString(ct) => StringMessageAttribute(pickField[String]("StringValue"), customType(ct))
34+
case SomeBinary(ct) => BinaryMessageAttribute(pickField[Array[Byte]]("BinaryValue"), customType(ct))
35+
case _ =>
36+
throw new Exception("Currently only handles String, Number and Binary typed attributes")
3137
}
3238
}
39+
40+
private def customType(appendix: String) = if (appendix.isEmpty) None else Some(appendix)
3341
}
3442

3543
}

rest/rest-sqs/src/main/scala/org/elasticmq/rest/sqs/SendMessageDirectives.scala

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import scala.xml.Elem
2020
trait SendMessageDirectives {
2121
this: ElasticMQDirectives with SQSLimitsModule with ResponseMarshaller =>
2222

23+
private val SomeString = """String\.?(.*)""".r
24+
private val SomeNumber = """Number\.?(.*)""".r
25+
private val SomeBinary = """Binary\.?(.*)""".r
26+
2327
def sendMessage(p: RequestPayload)(implicit marshallerDependencies: MarshallerDependencies): Route = {
2428
p.action(SendMessageAction) {
2529
val params = p.as[SendMessageActionRequest]
@@ -45,10 +49,10 @@ trait SendMessageDirectives {
4549
val parameterDataType = parameters(s"$prefix.$index.Value.DataType")
4650

4751
val parameterValue = parameterDataType match {
48-
case "String" => StringMessageAttribute(parameters(s"$prefix.$index.Value.StringValue"))
49-
case "Number" => NumberMessageAttribute(parameters(s"$prefix.$index.Value.StringValue"))
50-
case "Binary" =>
51-
BinaryMessageAttribute.fromBase64(parameters(s"MessageAttribute.$index.Value.BinaryValue"))
52+
case SomeString(ct) => StringMessageAttribute(parameters(s"$prefix.$index.Value.StringValue"), customType(ct))
53+
case SomeNumber(ct) => NumberMessageAttribute(parameters(s"$prefix.$index.Value.StringValue"), customType(ct))
54+
case SomeBinary(ct) =>
55+
BinaryMessageAttribute.fromBase64(parameters(s"MessageAttribute.$index.Value.BinaryValue"), customType(ct))
5256
case "" =>
5357
throw new SQSException(s"Attribute '$parameterName' must contain a non-empty attribute type")
5458
case _ =>
@@ -59,6 +63,8 @@ trait SendMessageDirectives {
5963
}
6064
}
6165

66+
private def customType(appendix: String) = if (appendix.isEmpty) None else Some(appendix)
67+
6268
def createMessage(
6369
parameters: SendMessageActionRequest,
6470
queueData: QueueData,

0 commit comments

Comments
 (0)