-
Notifications
You must be signed in to change notification settings - Fork 10
[Draft] Skip null values when serializing in rust #149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
mssalemi
wants to merge
2
commits into
main
Choose a base branch
from
ms.handle-null-fields-wasm-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -272,6 +272,18 @@ pub fn typegen( | |
module.to_token_stream().into() | ||
} | ||
|
||
/// Helper function to determine if a GraphQL input field type is nullable | ||
/// Uses conservative detection to identify Optional fields from GraphQL schema | ||
fn is_input_field_nullable(ivd: &impl InputValueDefinition) -> bool { | ||
// Use std::any::type_name to get type information as a string | ||
let type_name = std::any::type_name_of_val(&ivd.r#type()); | ||
|
||
// only treat fields that are explicitly nullable as Option types | ||
// This prevents incorrectly wrapping required fields in Option<T> | ||
type_name.contains("Option") | ||
|| (type_name.contains("Nullable") && !type_name.contains("NonNull")) | ||
} | ||
|
||
struct ShopifyFunctionCodeGenerator; | ||
|
||
impl CodeGenerator for ShopifyFunctionCodeGenerator { | ||
|
@@ -496,35 +508,76 @@ impl CodeGenerator for ShopifyFunctionCodeGenerator { | |
) -> Vec<syn::ItemImpl> { | ||
let name_ident = names::type_ident(input_object_type_definition.name()); | ||
|
||
// Conditionally serialize fields based on GraphQL schema nullability | ||
// Nullable fields (Option<T>) are only serialized if Some(_) | ||
// Required fields are always serialized | ||
|
||
let field_statements: Vec<syn::Stmt> = input_object_type_definition | ||
.input_field_definitions() | ||
.iter() | ||
.flat_map(|ivd| { | ||
let field_name_ident = names::field_ident(ivd.name()); | ||
let field_name_lit_str = syn::LitStr::new(ivd.name(), Span::mixed_site()); | ||
|
||
vec![ | ||
// Check if this field is nullable in the GraphQL schema | ||
if is_input_field_nullable(ivd) { | ||
// For nullable fields, only serialize if Some(_) | ||
vec![parse_quote! { | ||
if let ::std::option::Option::Some(ref value) = self.#field_name_ident { | ||
context.write_utf8_str(#field_name_lit_str)?; | ||
value.serialize(context)?; | ||
} | ||
}] | ||
} else { | ||
// For required fields, always serialize | ||
vec![ | ||
parse_quote! { | ||
context.write_utf8_str(#field_name_lit_str)?; | ||
}, | ||
parse_quote! { | ||
self.#field_name_ident.serialize(context)?; | ||
}, | ||
] | ||
} | ||
}) | ||
.collect(); | ||
|
||
// Generate field counting statements for dynamic field count calculation | ||
let field_count_statements: Vec<syn::Stmt> = input_object_type_definition | ||
.input_field_definitions() | ||
.iter() | ||
.map(|ivd| { | ||
let field_name_ident = names::field_ident(ivd.name()); | ||
|
||
if is_input_field_nullable(ivd) { | ||
// For nullable fields, count only if Some(_) | ||
parse_quote! { | ||
context.write_utf8_str(#field_name_lit_str)?; | ||
}, | ||
if let ::std::option::Option::Some(_) = self.#field_name_ident { | ||
field_count += 1; | ||
} | ||
} | ||
} else { | ||
// For required fields, always count | ||
parse_quote! { | ||
self.#field_name_ident.serialize(context)?; | ||
}, | ||
] | ||
field_count += 1; | ||
} | ||
} | ||
}) | ||
.collect(); | ||
Comment on lines
+545
to
566
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we just consolidate this into one line where we pre-compute the number of non-nullable fields and add the rest based on value, e.g. we might end up with: let field_count: usize = 5 + self.foo.is_some().into() + self.bar.is_some().into(); |
||
|
||
let num_fields = input_object_type_definition.input_field_definitions().len(); | ||
|
||
let serialize_impl = parse_quote! { | ||
impl shopify_function::wasm_api::Serialize for #name_ident { | ||
fn serialize(&self, context: &mut shopify_function::wasm_api::Context) -> ::std::result::Result<(), shopify_function::wasm_api::write::Error> { | ||
// Calculate dynamic field count based on non-null fields | ||
let mut field_count = 0usize; | ||
#(#field_count_statements)* | ||
|
||
context.write_object( | ||
|context| { | ||
#(#field_statements)* | ||
::std::result::Result::Ok(()) | ||
}, | ||
#num_fields, | ||
field_count, | ||
) | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not super familiar with bluejay, but I think if we have something like:
Then we could avoid using this approach. We need to get the type information so we can match correctly. I ran into issues when trying to use matching because some types were required (aka not optional), and some were optional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we should be using
bluejay
here for the type information