@@ -11,17 +11,35 @@ pub async fn handle(
1111) -> Result < Event , actix_web:: Error > {
1212 info ! ( "event: {}" , event) ;
1313
14- let input = match event. data ( ) {
15- Some ( Data :: Binary ( v) ) => from_slice ( v) ?,
16- Some ( Data :: String ( v) ) => from_str ( v) ?,
17- Some ( Data :: Json ( v) ) => v. to_owned ( ) ,
18- None => json ! ( { "name" : config. name } ) ,
14+ // Extract message from event data, or use default
15+ let message = match event. data ( ) {
16+ Some ( Data :: Binary ( v) ) => {
17+ let input: serde_json:: Value = from_slice ( v) ?;
18+ input. get ( "message" )
19+ . and_then ( |v| v. as_str ( ) )
20+ . unwrap_or ( & config. name )
21+ . to_string ( )
22+ } ,
23+ Some ( Data :: String ( v) ) => {
24+ let input: serde_json:: Value = from_str ( v) ?;
25+ input. get ( "message" )
26+ . and_then ( |v| v. as_str ( ) )
27+ . unwrap_or ( & config. name )
28+ . to_string ( )
29+ } ,
30+ Some ( Data :: Json ( v) ) => {
31+ v. get ( "message" )
32+ . and_then ( |v| v. as_str ( ) )
33+ . unwrap_or ( & config. name )
34+ . to_string ( )
35+ } ,
36+ None => config. name . clone ( ) ,
1937 } ;
2038
2139 EventBuilderV10 :: from ( event)
2240 . source ( "func://handler" )
23- . ty ( "func.example " )
24- . data ( "application/json" , json ! ( { "hello " : input [ "name" ] } ) )
41+ . ty ( "func.echo " )
42+ . data ( "application/json" , json ! ( { "message " : message } ) )
2543 . build ( )
2644 . map_err ( ErrorInternalServerError )
2745}
@@ -37,11 +55,11 @@ mod tests {
3755 #[ actix_rt:: test]
3856 async fn valid_input ( ) {
3957 let mut input = Event :: default ( ) ;
40- input. set_data ( "application/json" , json ! ( { "name " : "bootsy " } ) ) ;
58+ input. set_data ( "application/json" , json ! ( { "message " : "test-echo " } ) ) ;
4159 let resp = handle ( input, config ( ) ) . await ;
4260 assert ! ( resp. is_ok( ) ) ;
4361 match resp. unwrap ( ) . data ( ) {
44- Some ( Data :: Json ( output) ) => assert_eq ! ( "bootsy " , output[ "hello " ] ) ,
62+ Some ( Data :: Json ( output) ) => assert_eq ! ( "test-echo " , output[ "message " ] ) ,
4563 _ => panic ! ( ) ,
4664 }
4765 }
@@ -51,7 +69,7 @@ mod tests {
5169 let resp = handle ( Event :: default ( ) , config ( ) ) . await ;
5270 assert ! ( resp. is_ok( ) ) ;
5371 match resp. unwrap ( ) . data ( ) {
54- Some ( Data :: Json ( output) ) => assert_eq ! ( "world" , output[ "hello " ] ) ,
72+ Some ( Data :: Json ( output) ) => assert_eq ! ( "world" , output[ "message " ] ) ,
5573 _ => panic ! ( ) ,
5674 }
5775 }
0 commit comments