Skip to content

Commit 70aa286

Browse files
committed
in work
1 parent fd69d1a commit 70aa286

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

rust/cubesqlplanner/cubesqlplanner/src/cube_bridge/case_variant.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ pub enum CaseVariant {
1616

1717
impl<IT: InnerTypes> NativeDeserialize<IT> for CaseVariant {
1818
fn from_native(native_object: NativeObjectHandle<IT>) -> Result<Self, CubeError> {
19-
match NativeCaseSwitchDefinition::from_native(native_object.clone()) {
19+
let res = match NativeCaseSwitchDefinition::from_native(native_object.clone()) {
2020
Ok(case) => Ok(Self::CaseSwitch(Rc::new(case))),
2121
Err(_) => match NativeCaseDefinition::from_native(native_object) {
2222
Ok(case) => Ok(Self::Case(Rc::new(case))),
2323
Err(_) => Err(CubeError::user(format!("Case or Case Switch expected"))),
2424
},
25-
}
25+
};
26+
res
2627
}
2728
}

rust/cubesqlplanner/cubesqlplanner/src/cube_bridge/measure_definition.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ pub trait MeasureDefinition {
5959
#[nbridge(field, optional)]
6060
fn sql(&self) -> Result<Option<Rc<dyn MemberSql>>, CubeError>;
6161

62-
fn cube(&self) -> Result<Rc<dyn CubeDefinition>, CubeError>;
63-
6462
#[nbridge(field, optional)]
6563
fn case(&self) -> Result<Option<CaseVariant>, CubeError>;
6664

rust/cubesqlplanner/nativebridge/src/lib.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)