@@ -960,15 +960,36 @@ fn get_record(
960960 let mut vals: Vec < VVal > = vec ! [ ] ;
961961
962962 for ( name, tpe) in name_type_pairs {
963- let json_value = json_map. get ( name) . unwrap_or ( & Value :: Null ) ;
964-
965- match validate_function_parameters ( json_value, tpe. clone ( ) ) {
966- Ok ( result) => vals. push ( VVal { val : Some ( result) } ) ,
967- Err ( errs) => errors. extend ( errs) ,
963+ if let Some ( json_value) = json_map. get ( name) {
964+ match validate_function_parameters ( json_value, tpe. clone ( ) ) {
965+ Ok ( result) => vals. push ( VVal { val : Some ( result) } ) ,
966+ Err ( value_errors) => errors. extend (
967+ value_errors
968+ . iter ( )
969+ . map ( |err| format ! ( "Invalid value for the key {}. Error: {}" , name, err) )
970+ . collect :: < Vec < _ > > ( ) ,
971+ ) ,
972+ }
973+ } else {
974+ match tpe {
975+ Type :: Option ( _) => {
976+ vals. push ( VVal {
977+ val : Some ( Val :: Option ( Box :: new ( ValOption {
978+ discriminant : 0 ,
979+ value : None ,
980+ } ) ) ) ,
981+ } ) ;
982+ }
983+ _ => errors. push ( format ! ( "Key '{}' not found in json_map" , name) ) ,
984+ }
968985 }
969986 }
970987
971- Ok ( ValRecord { values : vals } )
988+ if errors. is_empty ( ) {
989+ Ok ( ValRecord { values : vals } )
990+ } else {
991+ Err ( errors)
992+ }
972993}
973994
974995fn get_enum ( input_json : & Value , names : Vec < String > ) -> Result < ValEnum , Vec < String > > {
@@ -1064,7 +1085,7 @@ fn get_variant(
10641085
10651086#[ cfg( test) ]
10661087mod tests {
1067-
1088+ use serde_json :: json ;
10681089 use std:: collections:: HashSet ;
10691090
10701091 use golem_api_grpc:: proto:: golem:: template:: { NameTypePair , TypePrimitive , TypeRecord } ;
@@ -1934,4 +1955,52 @@ mod tests {
19341955 } ) )
19351956 ) ;
19361957 }
1958+
1959+ #[ test]
1960+ fn test_get_record ( ) {
1961+ // Test case where all keys are present
1962+ let input_json = json ! ( {
1963+ "key1" : "value1" ,
1964+ "key2" : "value2" ,
1965+ } ) ;
1966+
1967+ let key1 = "key1" . to_string ( ) ;
1968+ let key2 = "key2" . to_string ( ) ;
1969+
1970+ let name_type_pairs: Vec < ( & String , & Type ) > = vec ! [
1971+ (
1972+ & key1,
1973+ & Type :: Primitive ( TypePrimitive {
1974+ primitive: PrimitiveType :: Str as i32 ,
1975+ } ) ,
1976+ ) ,
1977+ (
1978+ & key2,
1979+ & Type :: Primitive ( TypePrimitive {
1980+ primitive: PrimitiveType :: Str as i32 ,
1981+ } ) ,
1982+ ) ,
1983+ ] ;
1984+
1985+ let result = get_record ( & input_json, name_type_pairs. clone ( ) ) ;
1986+ let expected_result = Ok ( ValRecord {
1987+ values : vec ! [
1988+ VVal {
1989+ val: Some ( Val :: String ( "value1" . to_string( ) ) ) ,
1990+ } ,
1991+ VVal {
1992+ val: Some ( Val :: String ( "value2" . to_string( ) ) ) ,
1993+ } ,
1994+ ] ,
1995+ } ) ;
1996+ assert_eq ! ( result, expected_result) ;
1997+
1998+ // Test case where a key is missing
1999+ let input_json = json ! ( {
2000+ "key1" : "value1" ,
2001+ } ) ;
2002+
2003+ let result = get_record ( & input_json, name_type_pairs. clone ( ) ) ;
2004+ assert_eq ! ( result. is_err( ) , true ) ;
2005+ }
19372006}
0 commit comments