@@ -461,11 +461,27 @@ impl NativeService {
461461
462462 fn struct_impl ( & self ) -> proc_macro2:: TokenStream {
463463 let struct_ident = self . struct_ident ( ) ;
464+ let struct_name = struct_ident. to_string ( ) ;
465+ let has_required_fields = self . methods . iter ( ) . any ( |m| !m. is_optional ( ) ) ;
466+ let required_field_check = if has_required_fields {
467+ let checks = self
468+ . methods
469+ . iter ( )
470+ . map ( |m| m. required_field_check ( & struct_name) )
471+ . collect_vec ( ) ;
472+ quote ! {
473+ let native_struct = native_object. to_struct( ) ?;
474+ #( #checks ) *
475+ }
476+ } else {
477+ quote ! { }
478+ } ;
464479 if let Some ( static_data_type) = & self . static_data_type {
465480 quote ! {
466481 impl <IT : InnerTypes > #struct_ident<IT > {
467482 pub fn try_new( native_object: NativeObjectHandle <IT >) -> Result <Self , CubeError > {
468483 let static_data = #static_data_type:: from_native( native_object. clone( ) ) ?;
484+ #required_field_check
469485 Ok ( Self { native_object, static_data} )
470486 }
471487 }
@@ -474,6 +490,7 @@ impl NativeService {
474490 quote ! {
475491 impl <IT : InnerTypes > #struct_ident<IT > {
476492 pub fn try_new( native_object: NativeObjectHandle <IT >) -> Result <Self , CubeError > {
493+ #required_field_check
477494 Ok ( Self { native_object} )
478495 }
479496 }
@@ -546,6 +563,27 @@ impl NativeMethod {
546563 }
547564 }
548565
566+ fn required_field_check ( & self , struct_name : & str ) -> proc_macro2:: TokenStream {
567+ let & Self { method_params, .. } = & self ;
568+ let js_method_name = method_params
569+ . custom_name
570+ . clone ( )
571+ . unwrap_or_else ( || self . camel_case_name ( ) ) ;
572+ if method_params. is_optional {
573+ quote ! { }
574+ } else {
575+ quote ! {
576+ if !native_struct. has_field( #js_method_name) ? {
577+ return Err ( CubeError :: internal( format!( "Field {} is required for {}" , #js_method_name, #struct_name) ) ) ;
578+ }
579+ }
580+ }
581+ }
582+
583+ fn is_optional ( & self ) -> bool {
584+ self . method_params . is_optional
585+ }
586+
549587 fn method_impl ( & self ) -> proc_macro2:: TokenStream {
550588 let & Self {
551589 ident,
0 commit comments