Skip to content

Commit 4c0c28a

Browse files
committed
Improve some macro hygiene
1 parent 5d7f99e commit 4c0c28a

File tree

2 files changed

+106
-19
lines changed

2 files changed

+106
-19
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
use shopify_function::prelude::*;
2+
use shopify_function::wasm_api::Deserialize;
3+
4+
#[typegen([
5+
type Ok {
6+
value: String
7+
}
8+
9+
type Err {
10+
value: String
11+
}
12+
13+
union Result = Ok | Err
14+
15+
type Some {
16+
value: String
17+
}
18+
19+
type None {
20+
// this doesn't really make sense but types must have one field
21+
value: String
22+
}
23+
24+
union Option = Some | None
25+
26+
type Query {
27+
result: Result!
28+
option: Option!
29+
}
30+
], enums_as_str = ["__TypeKind"])]
31+
mod schema {
32+
#[query([
33+
query Query {
34+
result {
35+
__typename
36+
... on Ok {
37+
value
38+
}
39+
... on Err {
40+
value
41+
}
42+
}
43+
option {
44+
__typename
45+
... on Some {
46+
value
47+
}
48+
... on None {
49+
value
50+
}
51+
}
52+
}
53+
])]
54+
pub mod query {}
55+
}
56+
57+
#[test]
58+
fn test_macro_hygiene() {
59+
let value = serde_json::json!({
60+
"result": {
61+
"__typename": "Ok",
62+
"value": "test",
63+
},
64+
"option": {
65+
"__typename": "Some",
66+
"value": "test",
67+
},
68+
});
69+
let context = shopify_function::wasm_api::Context::new_with_input(value);
70+
let value = context.input_get().unwrap();
71+
72+
let result = schema::query::Query::deserialize(&value).unwrap();
73+
assert!(matches!(
74+
result.result(),
75+
schema::query::query::Result::Ok(_)
76+
));
77+
assert!(matches!(
78+
result.option(),
79+
schema::query::query::Option::Some(_)
80+
));
81+
}

shopify_function_macro/src/lib.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,11 @@ impl CodeGenerator for ShopifyFunctionCodeGenerator {
393393
impl shopify_function::wasm_api::Deserialize for #name_ident {
394394
fn deserialize(value: &shopify_function::wasm_api::Value) -> ::std::result::Result<Self, shopify_function::wasm_api::read::Error> {
395395
let typename = value.get_obj_prop("__typename");
396-
let typename_str: String = shopify_function::wasm_api::Deserialize::deserialize(&typename)?;
396+
let typename_str: ::std::string::String = shopify_function::wasm_api::Deserialize::deserialize(&typename)?;
397397

398398
match typename_str.as_str() {
399399
#(#match_arms)*
400-
_ => Ok(Self::Other),
400+
_ => ::std::result::Result::Ok(Self::Other),
401401
}
402402
}
403403
}
@@ -445,7 +445,7 @@ impl CodeGenerator for ShopifyFunctionCodeGenerator {
445445
}
446446
}
447447

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

471-
Ok(Self::from_str(&str_value))
471+
::std::result::Result::Ok(Self::from_str(&str_value))
472472
}
473473
}
474474
};
@@ -521,7 +521,7 @@ impl CodeGenerator for ShopifyFunctionCodeGenerator {
521521
context.write_object(
522522
|context| {
523523
#(#field_statements)*
524-
Ok(())
524+
::std::result::Result::Ok(())
525525
},
526526
#num_fields,
527527
)
@@ -542,7 +542,7 @@ impl CodeGenerator for ShopifyFunctionCodeGenerator {
542542
let deserialize_impl = parse_quote! {
543543
impl shopify_function::wasm_api::Deserialize for #name_ident {
544544
fn deserialize(value: &shopify_function::wasm_api::Value) -> ::std::result::Result<Self, shopify_function::wasm_api::read::Error> {
545-
Ok(Self {
545+
::std::result::Result::Ok(Self {
546546
#(#field_values),*
547547
})
548548
}
@@ -581,7 +581,7 @@ impl CodeGenerator for ShopifyFunctionCodeGenerator {
581581
match self {
582582
#(#match_arms)*
583583
}
584-
Ok(())
584+
::std::result::Result::Ok(())
585585
}, 1)
586586
}
587587
}
@@ -597,7 +597,7 @@ impl CodeGenerator for ShopifyFunctionCodeGenerator {
597597
parse_quote! {
598598
#field_name_lit_str => {
599599
let value = shopify_function::wasm_api::Deserialize::deserialize(&field_value)?;
600-
Ok(Self::#variant_ident(value))
600+
::std::result::Result::Ok(Self::#variant_ident(value))
601601
}
602602
}
603603
})
@@ -606,22 +606,22 @@ impl CodeGenerator for ShopifyFunctionCodeGenerator {
606606
let deserialize_impl = parse_quote! {
607607
impl shopify_function::wasm_api::Deserialize for #name_ident {
608608
fn deserialize(value: &shopify_function::wasm_api::Value) -> ::std::result::Result<Self, shopify_function::wasm_api::read::Error> {
609-
let Some(obj_len) = value.obj_len() else {
610-
return Err(shopify_function::wasm_api::read::Error::InvalidType);
609+
let ::std::option::Option::Some(obj_len) = value.obj_len() else {
610+
return ::std::result::Result::Err(shopify_function::wasm_api::read::Error::InvalidType);
611611
};
612612

613613
if obj_len != 1 {
614-
return Err(shopify_function::wasm_api::read::Error::InvalidType);
614+
return ::std::result::Result::Err(shopify_function::wasm_api::read::Error::InvalidType);
615615
}
616616

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

622622
match field_name.as_str() {
623623
#(#deserialize_match_arms)*
624-
_ => Err(shopify_function::wasm_api::read::Error::InvalidType),
624+
_ => ::std::result::Result::Err(shopify_function::wasm_api::read::Error::InvalidType),
625625
}
626626
}
627627
}
@@ -634,21 +634,27 @@ impl CodeGenerator for ShopifyFunctionCodeGenerator {
634634
&self,
635635
_enum_type_definition: &impl EnumTypeDefinition,
636636
) -> Vec<syn::Attribute> {
637-
vec![parse_quote! { #[derive(Debug, PartialEq, Clone, Copy)] }]
637+
vec![
638+
parse_quote! { #[derive(::std::fmt::Debug, ::std::cmp::PartialEq, ::std::clone::Clone, ::std::marker::Copy)] },
639+
]
638640
}
639641

640642
fn attributes_for_input_object(
641643
&self,
642644
_input_object_type_definition: &impl InputObjectTypeDefinition,
643645
) -> Vec<syn::Attribute> {
644-
vec![parse_quote! { #[derive(Debug, PartialEq, Clone)] }]
646+
vec![
647+
parse_quote! { #[derive(::std::fmt::Debug, ::std::cmp::PartialEq, ::std::clone::Clone)] },
648+
]
645649
}
646650

647651
fn attributes_for_one_of_input_object(
648652
&self,
649653
_input_object_type_definition: &impl InputObjectTypeDefinition,
650654
) -> Vec<syn::Attribute> {
651-
vec![parse_quote! { #[derive(Debug, PartialEq, Clone)] }]
655+
vec![
656+
parse_quote! { #[derive(::std::fmt::Debug, ::std::cmp::PartialEq, ::std::clone::Clone)] },
657+
]
652658
}
653659
}
654660

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

0 commit comments

Comments
 (0)