Skip to content

Commit 3b9dca4

Browse files
committed
Introduce Decimal helper type
1 parent b9f004e commit 3b9dca4

File tree

5 files changed

+71
-7
lines changed

5 files changed

+71
-7
lines changed

example/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fn function(input: input::ResponseData) -> Result<output::FunctionResult> {
5959
message: None,
6060
targets,
6161
value: output::Value::Percentage(output::Percentage {
62-
value: config.percentage.to_string(),
62+
value: Decimal(config.percentage),
6363
}),
6464
}],
6565
discount_application_strategy: output::DiscountApplicationStrategy::FIRST,

example/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fn test_discount_with_configuration() -> Result<()> {
101101
},
102102
)],
103103
value: crate::output::Value::Percentage(output::Percentage {
104-
value: "10".to_string(),
104+
value: Decimal(10.0),
105105
}),
106106
}],
107107
discount_application_strategy: crate::output::DiscountApplicationStrategy::FIRST,

shopify_function/src/scalars.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
mod decimal;
2+
13
pub type Boolean = bool;
24
pub type Float = f64;
35
pub type Int = i64;
46
pub type ID = String;
5-
pub type Decimal = String;
7+
pub use decimal::Decimal;
68
pub type Void = ();
79
pub type URL = String;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use serde::{Deserialize, Serialize};
2+
use std::str::FromStr;
3+
4+
/// Convenience wrapper for converting between Shopify's `Decimal` scalar, which
5+
/// is serialized as a `String`, and Rust's `f64`.
6+
#[derive(Deserialize, Serialize, Debug, PartialEq, Clone, Copy)]
7+
#[serde(try_from = "String")]
8+
#[serde(into = "String")]
9+
pub struct Decimal(pub f64);
10+
11+
impl Decimal {
12+
/// Access the value as an `f64`
13+
pub fn as_f64(&self) -> f64 {
14+
self.0
15+
}
16+
}
17+
18+
impl TryFrom<String> for Decimal {
19+
type Error = std::num::ParseFloatError;
20+
21+
fn try_from(value: String) -> Result<Self, Self::Error> {
22+
f64::from_str(value.as_str()).map(Self)
23+
}
24+
}
25+
26+
impl From<Decimal> for String {
27+
fn from(value: Decimal) -> Self {
28+
value.0.to_string()
29+
}
30+
}
31+
32+
impl From<Decimal> for f64 {
33+
fn from(value: Decimal) -> Self {
34+
value.0
35+
}
36+
}
37+
38+
impl From<f64> for Decimal {
39+
fn from(value: f64) -> Self {
40+
Self(value)
41+
}
42+
}
43+
44+
#[cfg(test)]
45+
mod tests {
46+
use super::Decimal;
47+
48+
#[test]
49+
fn test_json_deserialization() {
50+
let decimal_value = serde_json::json!("123.4");
51+
let decimal: Decimal =
52+
serde_json::from_value(decimal_value).expect("Error deserializing from JSON");
53+
assert_eq!(123.4, decimal.as_f64());
54+
}
55+
56+
#[test]
57+
fn test_json_serialization() {
58+
let decimal = Decimal(123.4);
59+
let json_value = serde_json::to_value(decimal).expect("Error serializing to JSON");
60+
assert_eq!(serde_json::json!("123.4"), json_value);
61+
}
62+
}

shopify_function_macro/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ pub fn generate_types(attr: proc_macro::TokenStream) -> proc_macro::TokenStream
175175
#[serde(rename_all(deserialize = "camelCase"))]
176176
#[graphql(
177177
#params,
178-
response_derives = "Clone,Debug,PartialEq,Eq,Deserialize",
179-
variables_derives = "Clone,Debug,PartialEq,Eq,Deserialize",
178+
response_derives = "Clone,Debug,PartialEq,Deserialize",
179+
variables_derives = "Clone,Debug,PartialEq,Deserialize",
180180
skip_serializing_none
181181
)]
182182
struct Input;
@@ -185,8 +185,8 @@ pub fn generate_types(attr: proc_macro::TokenStream) -> proc_macro::TokenStream
185185
#[graphql(
186186
query_path = #OUTPUT_QUERY_FILE_NAME,
187187
schema_path = #schema_path,
188-
response_derives = "Clone,Debug,PartialEq,Eq,Deserialize",
189-
variables_derives = "Clone,Debug,PartialEq,Eq,Deserialize",
188+
response_derives = "Clone,Debug,PartialEq,Deserialize",
189+
variables_derives = "Clone,Debug,PartialEq,Deserialize",
190190
skip_serializing_none
191191
)]
192192
struct Output;

0 commit comments

Comments
 (0)