-
Notifications
You must be signed in to change notification settings - Fork 64
Update rust functions #600
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| api_version = "2025-04" | ||
| api_version = "unstable" | ||
|
|
||
| [[extensions]] | ||
| name = "t:name" | ||
|
|
@@ -19,6 +19,8 @@ description = "t:description" | |
|
|
||
| [extensions.build] | ||
| command = "cargo build --target=wasm32-wasip1 --release" | ||
| path = "target/wasm32-wasip1/release/{{handle | replace: " ", "-" | downcase}}.wasm" | ||
| path = "target/wasm32-wasip1/release/discount-function-rust.wasm/{{handle | replace: " ", "-" | downcase}}.wasm" | ||
| watch = [ "src/**/*.rs" ] | ||
|
|
||
| [extensions.ui] | ||
| handle = "{{handle}}" | ||
|
Contributor
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. Do we want this in default? I don't think so?
Contributor
Author
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. Updated to use the handle from the extension generate command. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,19 +10,16 @@ use cart_lines_discounts_generate_run::input::ResponseData; | |
|
|
||
| #[derive(Deserialize)] | ||
| #[serde(rename_all = "camelCase")] | ||
| struct DiscountResponse { | ||
| operations: Operations, | ||
| } | ||
|
|
||
| #[derive(Deserialize)] | ||
| #[serde(rename_all = "camelCase")] | ||
| struct Operations { | ||
| struct OperationItem { | ||
| #[serde(default)] | ||
| add_product_discounts: Option<ProductDiscounts>, | ||
| #[serde(default)] | ||
| add_order_discounts: Option<OrderDiscounts>, | ||
| #[serde(default)] | ||
| add_discount_code_validations: Option<ValidDiscountCodes>, | ||
| // Ignore other operation types that might be in the response but we don't use in cart context | ||
| #[serde(flatten)] | ||
| _other: std::collections::HashMap<String, serde_json::Value>, | ||
|
Contributor
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. Interesting |
||
| } | ||
|
|
||
| #[shopify_function_target( | ||
|
|
@@ -32,25 +29,31 @@ struct Operations { | |
| )] | ||
| fn generate_cart_run(input: ResponseData) -> Result<FunctionCartRunResult> { | ||
| let fetch_result = input.fetch_result.ok_or("Missing fetch result")?; | ||
| let body = fetch_result.body.ok_or("Missing response body")?; | ||
|
|
||
| // Parse the response body directly into our types | ||
| let response: DiscountResponse = | ||
| serde_json::from_str(&body).map_err(|e| format!("Failed to parse JSON: {}", e))?; | ||
| // Use jsonBody which is the only available property | ||
| let json_body = fetch_result.json_body.ok_or("Missing json_body in response")?; | ||
|
|
||
| // Parse using the JSON value | ||
| let operation_items = serde_json::from_value::<Vec<OperationItem>>(json_body) | ||
| .map_err(|e| format!("Failed to convert jsonBody: {}", e))?; | ||
|
|
||
| // Convert the response into operations | ||
| let mut operations = Vec::new(); | ||
|
|
||
| if let Some(validations) = response.operations.add_discount_code_validations { | ||
| operations.push(CartOperation::AddDiscountCodeValidations(validations)); | ||
| } | ||
| // Process each operation item | ||
| for item in operation_items { | ||
| if let Some(validations) = item.add_discount_code_validations { | ||
| operations.push(CartOperation::AddDiscountCodeValidations(validations)); | ||
| } | ||
|
|
||
| if let Some(product_discounts) = response.operations.add_product_discounts { | ||
| operations.push(CartOperation::AddProductDiscounts(product_discounts)); | ||
| } | ||
| if let Some(product_discounts) = item.add_product_discounts { | ||
| operations.push(CartOperation::AddProductDiscounts(product_discounts)); | ||
| } | ||
|
|
||
| if let Some(order_discounts) = response.operations.add_order_discounts { | ||
| operations.push(CartOperation::AddOrderDiscounts(order_discounts)); | ||
| if let Some(order_discounts) = item.add_order_discounts { | ||
| operations.push(CartOperation::AddOrderDiscounts(order_discounts)); | ||
| } | ||
| // Ignore delivery discounts for cart operations | ||
| } | ||
|
|
||
| Ok(FunctionCartRunResult { operations }) | ||
|
|
||
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.
this seems wrong