Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions shopify_function/tests/macro_hygiene_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use shopify_function::prelude::*;
use shopify_function::wasm_api::Deserialize;

#[typegen([
type Ok {
value: String
}

type Err {
value: String
}

union Result = Ok | Err

type Some {
value: String
}

type None {
// this doesn't really make sense but types must have one field
value: String
}

union Option = Some | None

type Query {
result: Result!
option: Option!
}
], enums_as_str = ["__TypeKind"])]
mod schema {
#[query([
query Query {
result {
__typename
... on Ok {
value
}
... on Err {
value
}
}
option {
__typename
... on Some {
value
}
... on None {
value
}
}
}
])]
pub mod query {}
}

#[test]
fn test_macro_hygiene() {
let value = serde_json::json!({
"result": {
"__typename": "Ok",
"value": "test",
},
"option": {
"__typename": "Some",
"value": "test",
},
});
let context = shopify_function::wasm_api::Context::new_with_input(value);
let value = context.input_get().unwrap();

let result = schema::query::Query::deserialize(&value).unwrap();
assert!(matches!(
result.result(),
schema::query::query::Result::Ok(_)
));
assert!(matches!(
result.option(),
schema::query::query::Option::Some(_)
));
}
44 changes: 25 additions & 19 deletions shopify_function_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,11 @@ impl CodeGenerator for ShopifyFunctionCodeGenerator {
impl shopify_function::wasm_api::Deserialize for #name_ident {
fn deserialize(value: &shopify_function::wasm_api::Value) -> ::std::result::Result<Self, shopify_function::wasm_api::read::Error> {
let typename = value.get_obj_prop("__typename");
let typename_str: String = shopify_function::wasm_api::Deserialize::deserialize(&typename)?;
let typename_str: ::std::string::String = shopify_function::wasm_api::Deserialize::deserialize(&typename)?;

match typename_str.as_str() {
#(#match_arms)*
_ => Ok(Self::Other),
_ => ::std::result::Result::Ok(Self::Other),
}
}
}
Expand Down Expand Up @@ -445,7 +445,7 @@ impl CodeGenerator for ShopifyFunctionCodeGenerator {
}
}

fn as_str(&self) -> &str {
fn as_str(&self) -> &::std::primitive::str {
match self {
#(#as_str_match_arms)*
Self::Other => panic!("Cannot serialize `Other` variant"),
Expand All @@ -466,9 +466,9 @@ impl CodeGenerator for ShopifyFunctionCodeGenerator {
let deserialize_impl = parse_quote! {
impl shopify_function::wasm_api::Deserialize for #name_ident {
fn deserialize(value: &shopify_function::wasm_api::Value) -> ::std::result::Result<Self, shopify_function::wasm_api::read::Error> {
let str_value: String = shopify_function::wasm_api::Deserialize::deserialize(value)?;
let str_value: ::std::string::String = shopify_function::wasm_api::Deserialize::deserialize(value)?;

Ok(Self::from_str(&str_value))
::std::result::Result::Ok(Self::from_str(&str_value))
}
}
};
Expand Down Expand Up @@ -521,7 +521,7 @@ impl CodeGenerator for ShopifyFunctionCodeGenerator {
context.write_object(
|context| {
#(#field_statements)*
Ok(())
::std::result::Result::Ok(())
},
#num_fields,
)
Expand All @@ -542,7 +542,7 @@ impl CodeGenerator for ShopifyFunctionCodeGenerator {
let deserialize_impl = parse_quote! {
impl shopify_function::wasm_api::Deserialize for #name_ident {
fn deserialize(value: &shopify_function::wasm_api::Value) -> ::std::result::Result<Self, shopify_function::wasm_api::read::Error> {
Ok(Self {
::std::result::Result::Ok(Self {
#(#field_values),*
})
}
Expand Down Expand Up @@ -581,7 +581,7 @@ impl CodeGenerator for ShopifyFunctionCodeGenerator {
match self {
#(#match_arms)*
}
Ok(())
::std::result::Result::Ok(())
}, 1)
}
}
Expand All @@ -597,7 +597,7 @@ impl CodeGenerator for ShopifyFunctionCodeGenerator {
parse_quote! {
#field_name_lit_str => {
let value = shopify_function::wasm_api::Deserialize::deserialize(&field_value)?;
Ok(Self::#variant_ident(value))
::std::result::Result::Ok(Self::#variant_ident(value))
}
}
})
Expand All @@ -606,22 +606,22 @@ impl CodeGenerator for ShopifyFunctionCodeGenerator {
let deserialize_impl = parse_quote! {
impl shopify_function::wasm_api::Deserialize for #name_ident {
fn deserialize(value: &shopify_function::wasm_api::Value) -> ::std::result::Result<Self, shopify_function::wasm_api::read::Error> {
let Some(obj_len) = value.obj_len() else {
return Err(shopify_function::wasm_api::read::Error::InvalidType);
let ::std::option::Option::Some(obj_len) = value.obj_len() else {
return ::std::result::Result::Err(shopify_function::wasm_api::read::Error::InvalidType);
};

if obj_len != 1 {
return Err(shopify_function::wasm_api::read::Error::InvalidType);
return ::std::result::Result::Err(shopify_function::wasm_api::read::Error::InvalidType);
}

let Some(field_name) = value.get_obj_key_at_index(0) else {
return Err(shopify_function::wasm_api::read::Error::InvalidType);
let ::std::option::Option::Some(field_name) = value.get_obj_key_at_index(0) else {
return ::std::result::Result::Err(shopify_function::wasm_api::read::Error::InvalidType);
};
let field_value = value.get_at_index(0);

match field_name.as_str() {
#(#deserialize_match_arms)*
_ => Err(shopify_function::wasm_api::read::Error::InvalidType),
_ => ::std::result::Result::Err(shopify_function::wasm_api::read::Error::InvalidType),
}
}
}
Expand All @@ -634,21 +634,27 @@ impl CodeGenerator for ShopifyFunctionCodeGenerator {
&self,
_enum_type_definition: &impl EnumTypeDefinition,
) -> Vec<syn::Attribute> {
vec![parse_quote! { #[derive(Debug, PartialEq, Clone, Copy)] }]
vec![
parse_quote! { #[derive(::std::fmt::Debug, ::std::cmp::PartialEq, ::std::clone::Clone, ::std::marker::Copy)] },
]
}

fn attributes_for_input_object(
&self,
_input_object_type_definition: &impl InputObjectTypeDefinition,
) -> Vec<syn::Attribute> {
vec![parse_quote! { #[derive(Debug, PartialEq, Clone)] }]
vec![
parse_quote! { #[derive(::std::fmt::Debug, ::std::cmp::PartialEq, ::std::clone::Clone)] },
]
}

fn attributes_for_one_of_input_object(
&self,
_input_object_type_definition: &impl InputObjectTypeDefinition,
) -> Vec<syn::Attribute> {
vec![parse_quote! { #[derive(Debug, PartialEq, Clone)] }]
vec![
parse_quote! { #[derive(::std::fmt::Debug, ::std::cmp::PartialEq, ::std::clone::Clone)] },
]
}
}

Expand Down Expand Up @@ -761,7 +767,7 @@ fn derive_deserialize_for_derive_input(input: &syn::DeriveInput) -> syn::Result<
let deserialize_impl = parse_quote! {
impl shopify_function::wasm_api::Deserialize for #name_ident {
fn deserialize(value: &shopify_function::wasm_api::Value) -> ::std::result::Result<Self, shopify_function::wasm_api::read::Error> {
Ok(Self {
::std::result::Result::Ok(Self {
#(#field_values),*
})
}
Expand Down