Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions discounts/rust/discount/default/shopify.extension.toml.liquid
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"
Expand All @@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems wrong

watch = [ "src/**/*.rs" ]

[extensions.ui]
handle = "{{handle}}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want this in default? I don't think so?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use the handle from the extension generate command.

6 changes: 4 additions & 2 deletions discounts/rust/network/default/shopify.extension.toml.liquid
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"
Expand Down Expand Up @@ -29,6 +29,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/function-network-rust.wasm/{{handle | replace: " ", "-" | downcase}}.wasm"
watch = [ "src/**/*.rs" ]

[extensions.ui]
handle = "{{handle}}"
36 changes: 24 additions & 12 deletions discounts/rust/network/default/src/generate_cart_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,21 @@ fn generate_cart_fetch(
let json_body = json!({ "enteredDiscountCodes": entered_discount_codes });

let request = CartFetchHttpRequest {
headers: vec![CartFetchHttpRequestHeader {
name: "accept".to_string(),
value: "application/json".to_string(),
}],
method: CartFetchHttpRequestMethod::GET,
headers: vec![
CartFetchHttpRequestHeader {
name: "accept".to_string(),
value: "application/json".to_string(),
},
CartFetchHttpRequestHeader {
name: "Content-Type".to_string(),
value: "application/json".to_string(),
}
],
method: CartFetchHttpRequestMethod::POST,
policy: CartFetchHttpRequestPolicy {
read_timeout_ms: 2000,
},
url: "<external server url>".to_string(),
url: "<external-server-url>".to_string(),
body: Some(json_body.to_string()),
json_body: Some(json_body.clone()),
};
Expand Down Expand Up @@ -59,15 +65,21 @@ mod tests {
let json_body = json!({ "enteredDiscountCodes": [] });
let expected = FunctionCartFetchResult {
request: Some(CartFetchHttpRequest {
headers: vec![CartFetchHttpRequestHeader {
name: "accept".to_string(),
value: "application/json".to_string(),
}],
method: CartFetchHttpRequestMethod::GET,
headers: vec![
CartFetchHttpRequestHeader {
name: "accept".to_string(),
value: "application/json".to_string(),
},
CartFetchHttpRequestHeader {
name: "Content-Type".to_string(),
value: "application/json".to_string(),
}
],
method: CartFetchHttpRequestMethod::POST,
policy: CartFetchHttpRequestPolicy {
read_timeout_ms: 2000,
},
url: "<external server url>".to_string(),
url: "<external-server-url>".to_string(),
json_body: Some(json_body.clone()),
body: Some(json_body.to_string()),
}),
Expand Down
41 changes: 22 additions & 19 deletions discounts/rust/network/default/src/generate_cart_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting

}

#[shopify_function_target(
Expand All @@ -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 })
Expand Down
36 changes: 24 additions & 12 deletions discounts/rust/network/default/src/generate_delivery_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,21 @@ fn generate_delivery_fetch(
let json_body = json!({ "enteredDiscountCodes": entered_discount_codes });

let request = DeliveryFetchHttpRequest {
headers: vec![DeliveryFetchHttpRequestHeader {
name: "accept".to_string(),
value: "application/json".to_string(),
}],
method: DeliveryFetchHttpRequestMethod::GET,
headers: vec![
DeliveryFetchHttpRequestHeader {
name: "accept".to_string(),
value: "application/json".to_string(),
},
DeliveryFetchHttpRequestHeader {
name: "Content-Type".to_string(),
value: "application/json".to_string(),
}
],
method: DeliveryFetchHttpRequestMethod::POST,
policy: DeliveryFetchHttpRequestPolicy {
read_timeout_ms: 2000,
},
url: "<external server url>".to_string(),
url: "<external-server-url>".to_string(),
body: Some(json_body.to_string()),
json_body: Some(json_body.clone()),
};
Expand Down Expand Up @@ -59,15 +65,21 @@ mod tests {
let json_body = json!({ "enteredDiscountCodes": ["ABC"] });
let expected = FunctionDeliveryFetchResult {
request: Some(DeliveryFetchHttpRequest {
headers: vec![DeliveryFetchHttpRequestHeader {
name: "accept".to_string(),
value: "application/json".to_string(),
}],
method: DeliveryFetchHttpRequestMethod::GET,
headers: vec![
DeliveryFetchHttpRequestHeader {
name: "accept".to_string(),
value: "application/json".to_string(),
},
DeliveryFetchHttpRequestHeader {
name: "Content-Type".to_string(),
value: "application/json".to_string(),
}
],
method: DeliveryFetchHttpRequestMethod::POST,
policy: DeliveryFetchHttpRequestPolicy {
read_timeout_ms: 2000,
},
url: "<external server url>".to_string(),
url: "<external-server-url>".to_string(),
json_body: Some(json_body.clone()),
body: Some(json_body.to_string()),
}),
Expand Down
35 changes: 19 additions & 16 deletions discounts/rust/network/default/src/generate_delivery_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ use cart_delivery_options_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_delivery_discounts: Option<DeliveryDiscounts>,
#[serde(default)]
add_discount_code_validations: Option<ValidDiscountCodes>,
// Ignore any other fields we don't need
#[serde(flatten)]
_other: std::collections::HashMap<String, serde_json::Value>,
}

#[shopify_function_target(
Expand All @@ -30,21 +27,27 @@ struct Operations {
)]
fn generate_delivery_run(input: ResponseData) -> Result<FunctionDeliveryRunResult> {
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(DeliveryOperation::AddDiscountCodeValidations(validations));
}
// Process each operation item
for item in operation_items {
if let Some(validations) = item.add_discount_code_validations {
operations.push(DeliveryOperation::AddDiscountCodeValidations(validations));
}

if let Some(delivery_discounts) = response.operations.add_delivery_discounts {
operations.push(DeliveryOperation::AddDeliveryDiscounts(delivery_discounts));
if let Some(delivery_discounts) = item.add_delivery_discounts {
operations.push(DeliveryOperation::AddDeliveryDiscounts(delivery_discounts));
}
// Ignore cart/order discounts for delivery operations
}

Ok(FunctionDeliveryRunResult { operations })
Expand Down
4 changes: 2 additions & 2 deletions discounts/rust/network/default/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::process;
pub mod generate_cart_fetch;
pub mod generate_cart_run;
pub mod generate_delivery_fetch;
pub mod generate_delivery_run;
pub mod generate_cart_fetch;
pub mod generate_delivery_fetch;

fn main() {
eprintln!("Please invoke a named export.");
Expand Down
Loading