@@ -11,9 +11,7 @@ final case class UnsupportedRequestError(message: String) extends Exception(mess
1111
1212final case class UnsupportedEventError (message : String ) extends Exception (message)
1313
14- trait MultipleHandlersSupport [OtherContext ] extends EventHandler , EventHandlerTag {
15-
16- given otherContext : OtherContext
14+ trait MultipleHandlersSupport extends EventHandler , EventHandlerTag {
1715
1816 private val functionNameRegex = " \" (?:function|functionName|handler)\" (?:\\ s*):(?:\\ s*)\" (.+?)\" " .r
1917 private val requestPathRegex = " \" path\" (?:\\ s*):(?:\\ s*)\" (.+?)\" " .r
@@ -26,13 +24,13 @@ trait MultipleHandlersSupport[OtherContext] extends EventHandler, EventHandlerTa
2624 .flatMap(m => Option (m.group(1 )))
2725 } catch { case NonFatal (_) => None }
2826
29- def apiGatewayRequestHandlers : Iterable [ApiGatewayRequestHandler [OtherContext ]]
27+ def apiGatewayRequestHandlers : Iterable [ApiGatewayRequestHandler [ApplicationContext ]]
3028
31- def sqsEventHandlers : Iterable [SqsEventHandler [OtherContext ]]
29+ def sqsEventHandlers : Iterable [SqsEventHandler [ApplicationContext ]]
3230
33- def genericEventHandlers : Iterable [GenericEventHandler [OtherContext ]]
31+ def genericEventHandlers : Iterable [GenericEventHandler [ApplicationContext ]]
3432
35- lazy val sqsEventHandlersMap : Map [String , SqsEventHandler [OtherContext ]] =
33+ lazy val sqsEventHandlersMap : Map [String , SqsEventHandler [ApplicationContext ]] =
3634 sqsEventHandlers
3735 .map(handler =>
3836 (
@@ -47,7 +45,7 @@ trait MultipleHandlersSupport[OtherContext] extends EventHandler, EventHandlerTa
4745 )
4846 .toMap
4947
50- lazy val genericEventHandlersMap : Map [String , GenericEventHandler [OtherContext ]] = genericEventHandlers
48+ lazy val genericEventHandlersMap : Map [String , GenericEventHandler [ApplicationContext ]] = genericEventHandlers
5149 .map(handler =>
5250 (
5351 handler.functionName
@@ -61,9 +59,7 @@ trait MultipleHandlersSupport[OtherContext] extends EventHandler, EventHandlerTa
6159 )
6260 .toMap
6361
64- final override inline def handleRequest (
65- input : String
66- )(using lambdaContext : LambdaContext ): String =
62+ final override def handleRequest (input : String )(using LambdaContext , ApplicationContext ): String =
6763 parseInput(input).match {
6864 case request : ApiGatewayRequest =>
6965 try {
@@ -87,18 +83,23 @@ trait MultipleHandlersSupport[OtherContext] extends EventHandler, EventHandlerTa
8783 sqsEventHandlersMap
8884 .get(functionName)
8985 .flatMap { handler =>
90- lambdaContext.debug(
91- s " [EVENT][ ${index + 1 }] Invoking $functionName with payload ${record.writeAsString}"
92- )
93- val r = handler.handleRecord(getFunctionInput(record))
94- lambdaContext.debug(s " [EVENT][ ${index + 1 }] Handler returned result ${r.getOrElse(" none" )}" )
95- r
86+ handler.handleRecord(getFunctionInput(record))
9687 }
9788 .getOrElse(
9889 throw UnsupportedEventError (write(event))
9990 )
10091 case None =>
101- throw UnsupportedEventError (write(event))
92+ if (sqsEventHandlersMap.size == 1 )
93+ then
94+ sqsEventHandlersMap.head._2
95+ .handleRecord(record)
96+ .getOrElse(
97+ throw UnsupportedEventError (write(event))
98+ )
99+ else
100+ throw UnsupportedEventError (
101+ " Ambiguous SQS event cannot be processed because 'handler' parameter is missing. Add \" handler\" :\" {sqsHandlerName}\" field to your record body."
102+ )
102103 }
103104 } catch handleSqsEventHandlerException(input)
104105 }
@@ -115,7 +116,17 @@ trait MultipleHandlersSupport[OtherContext] extends EventHandler, EventHandlerTa
115116 throw UnsupportedEventError (write(event))
116117 )
117118 case None =>
118- throw UnsupportedEventError (write(event))
119+ if (genericEventHandlersMap.size == 1 )
120+ then
121+ genericEventHandlersMap.head._2
122+ .handleEvent(event)
123+ .getOrElse(
124+ throw UnsupportedEventError (write(event))
125+ )
126+ else
127+ throw UnsupportedEventError (
128+ " Ambiguous generic event cannot be processed because 'handler' parameter is missing. Add \" handler\" :\" {genericHandlerName}\" field to your event body."
129+ )
119130 }
120131
121132 } catch handleGenericEventHandlerException(input)
@@ -203,34 +214,33 @@ trait MultipleHandlersSupport[OtherContext] extends EventHandler, EventHandlerTa
203214
204215 private inline def maybeFunctionName (json : ujson.Value ): Option [String ] =
205216 json
206- .get(" functionName" )
207- .flatMap(_.strOpt)
208- .orElse(
209- json
210- .get(" function" )
211- .flatMap(_.strOpt)
212- )
213- .orElse(
214- json
215- .get(" handler" )
216- .flatMap(_.strOpt)
217- )
217+ .maybeString(" functionName" )
218+ .orElse(json.maybeString(" handlerName" ))
219+ .orElse(json.maybeString(" handler" ))
220+ .orElse(json.maybeString(" function" ))
218221
219222 private inline def getFunctionInput (event : ujson.Value ): ujson.Value =
220- event.get(" functionInputParts" ) match {
221- case Some (ujson.Arr (values)) =>
222- values.foldLeft(ujson.Obj ()) { (a, v) =>
223- v match {
224- case Obj (newFields) => Obj .from(a.value ++ newFields)
225- case other =>
226- throw new Exception (
227- s " Expected functionInputParts array to contain only objects, but got ${other.getClass().getSimpleName()} $other"
228- )
223+ event
224+ .get(" functionInputParts" )
225+ .orElse(event.get(" handlerInputParts" ))
226+ .match {
227+ case Some (ujson.Arr (values)) =>
228+ values.foldLeft(ujson.Obj ()) { (a, v) =>
229+ v match {
230+ case Obj (newFields) => Obj .from(a.value ++ newFields)
231+ case other =>
232+ throw new Exception (
233+ s " Expected functionInputParts array to contain only objects, but got ${other.getClass().getSimpleName()} $other"
234+ )
235+ }
229236 }
230- }
231- case Some (value) => value
232- case None => event.get(" functionInput" ).getOrElse(event)
233- }
237+ case Some (value) => value
238+ case None =>
239+ event
240+ .get(" functionInput" )
241+ .orElse(event.get(" handlerInput" ))
242+ .getOrElse(event)
243+ }
234244
235245 private inline def getFunctionInput (record : SqsEvent .Record ): SqsEvent .Record =
236246 record.copy(body =
0 commit comments