fix #925 support bigdecimal
This commit is contained in:
parent
e09494ae03
commit
318c3dfc97
|
@ -68,6 +68,7 @@ opentelemetry = { version = "0.17.0", optional = true, default-features = false,
|
||||||
"trace",
|
"trace",
|
||||||
] }
|
] }
|
||||||
rust_decimal = { version = "1.14.3", optional = true }
|
rust_decimal = { version = "1.14.3", optional = true }
|
||||||
|
bigdecimal = { version = "0.3.0", optional = true }
|
||||||
secrecy = { version = "0.8.0", optional = true }
|
secrecy = { version = "0.8.0", optional = true }
|
||||||
smol_str = { version = "0.1.21", optional = true }
|
smol_str = { version = "0.1.21", optional = true }
|
||||||
time = { version = "0.3.5", optional = true, features = [
|
time = { version = "0.3.5", optional = true, features = [
|
||||||
|
|
31
src/types/external/big_decimal.rs
vendored
Normal file
31
src/types/external/big_decimal.rs
vendored
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
use bigdecimal::BigDecimal;
|
||||||
|
|
||||||
|
use crate::{InputValueError, InputValueResult, Scalar, ScalarType, Value};
|
||||||
|
|
||||||
|
#[Scalar(internal, name = "BigDecimal")]
|
||||||
|
impl ScalarType for BigDecimal {
|
||||||
|
fn parse(value: Value) -> InputValueResult<Self> {
|
||||||
|
match &value {
|
||||||
|
Value::Number(n) => {
|
||||||
|
if let Some(f) = n.as_f64() {
|
||||||
|
return BigDecimal::try_from(f).map_err(InputValueError::custom);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(f) = n.as_i64() {
|
||||||
|
return Ok(BigDecimal::from(f));
|
||||||
|
}
|
||||||
|
|
||||||
|
// unwrap safe here, because we have check the other possibility
|
||||||
|
Ok(BigDecimal::from(n.as_u64().unwrap()))
|
||||||
|
},
|
||||||
|
Value::String(s) => Ok(BigDecimal::from_str(s)?),
|
||||||
|
_ => Err(InputValueError::expected_type(value)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_value(&self) -> Value {
|
||||||
|
Value::String(self.to_string())
|
||||||
|
}
|
||||||
|
}
|
12
src/types/external/decimal.rs
vendored
12
src/types/external/decimal.rs
vendored
|
@ -9,6 +9,18 @@ impl ScalarType for Decimal {
|
||||||
fn parse(value: Value) -> InputValueResult<Self> {
|
fn parse(value: Value) -> InputValueResult<Self> {
|
||||||
match &value {
|
match &value {
|
||||||
Value::String(s) => Ok(Decimal::from_str(s)?),
|
Value::String(s) => Ok(Decimal::from_str(s)?),
|
||||||
|
Value::Number(n) => {
|
||||||
|
if let Some(f) = n.as_f64() {
|
||||||
|
return Decimal::try_from(f).map_err(InputValueError::custom);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(f) = n.as_i64() {
|
||||||
|
return Ok(Decimal::from(f));
|
||||||
|
}
|
||||||
|
|
||||||
|
// unwrap safe here, because we have check the other possibility
|
||||||
|
Ok(Decimal::from(n.as_u64().unwrap()))
|
||||||
|
},
|
||||||
_ => Err(InputValueError::expected_type(value)),
|
_ => Err(InputValueError::expected_type(value)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
2
src/types/external/mod.rs
vendored
2
src/types/external/mod.rs
vendored
|
@ -23,6 +23,8 @@ mod chrono_tz;
|
||||||
mod datetime;
|
mod datetime;
|
||||||
#[cfg(feature = "decimal")]
|
#[cfg(feature = "decimal")]
|
||||||
mod decimal;
|
mod decimal;
|
||||||
|
#[cfg(feature = "bigdecimal")]
|
||||||
|
mod big_decimal;
|
||||||
#[cfg(feature = "chrono-duration")]
|
#[cfg(feature = "chrono-duration")]
|
||||||
mod duration;
|
mod duration;
|
||||||
#[cfg(feature = "chrono")]
|
#[cfg(feature = "chrono")]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user