Skip to content

Commit

Permalink
Merge pull request #112 from nappa85/master
Browse files Browse the repository at this point in the history
Introduce Nullable trait to permit custom Option<T>
  • Loading branch information
tyt2y3 authored Aug 29, 2021
2 parents cfae4ea + fabae54 commit 3c76e59
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 79 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ bytes = { version = "^1", optional = true }
chrono = { version = "^0", optional = true }
postgres-types = { version = "^0", optional = true }
rust_decimal = { version = "^1", optional = true }
bigdecimal = { version = "^0", optional = true }
bigdecimal = { version = "^0.2", optional = true }
uuid = { version = "^0", optional = true }
thiserror = { version = "^1" }

Expand Down
2 changes: 1 addition & 1 deletion examples/sqlx_mysql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ chrono = "^0"
uuid = { version = "^0", features = ["serde", "v4"] }
serde_json = "^1"
rust_decimal = { version = "^1" }
bigdecimal = { version = "^0" }
bigdecimal = { version = "^0.2" }
async-std = { version = "1.8", features = [ "attributes" ] }
sea-query = { path = "../../", features = [
"sqlx-mysql",
Expand Down
2 changes: 1 addition & 1 deletion examples/sqlx_postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ chrono = "^0"
uuid = { version = "^0", features = ["serde", "v4"] }
serde_json = "^1"
rust_decimal = { version = "^1" }
bigdecimal = { version = "^0" }
bigdecimal = { version = "^0.2" }
async-std = { version = "1.8", features = [ "attributes" ] }
sea-query = { path = "../../", features = [
"sqlx-postgres",
Expand Down
129 changes: 53 additions & 76 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ pub trait IntoValueTuple {
fn into_value_tuple(self) -> ValueTuple;
}

pub trait Nullable {
fn null() -> Value;
}

impl Value {
pub fn unwrap<T>(self) -> T
where
Expand All @@ -113,9 +117,9 @@ macro_rules! type_to_value {
}
}

impl From<Option<$type>> for Value {
fn from(x: Option<$type>) -> Value {
Value::$name(x)
impl Nullable for $type {
fn null() -> Value {
Value::$name(None)
}
}

Expand All @@ -140,25 +144,6 @@ macro_rules! type_to_value {
Default::default()
}
}

impl ValueType for Option<$type> {
fn unwrap(v: Value) -> Self {
match v {
Value::$name(x) => x,
_ => panic!("type error"),
}
}

fn type_name() -> &'static str {
concat!("Option<", stringify!($type), ">")
}
}

impl ValueTypeDefault for Option<$type> {
fn default() -> Self {
Default::default()
}
}
};
}

Expand All @@ -170,12 +155,9 @@ macro_rules! type_to_box_value {
}
}

impl From<Option<$type>> for Value {
fn from(x: Option<$type>) -> Value {
match x {
Some(v) => Value::$name(Some(Box::new(v))),
None => Value::$name(None),
}
impl Nullable for $type {
fn null() -> Value {
Value::$name(None)
}
}

Expand All @@ -191,26 +173,6 @@ macro_rules! type_to_box_value {
stringify!($type)
}
}

impl ValueType for Option<$type> {
fn unwrap(v: Value) -> Self {
match v {
Value::$name(Some(x)) => Some(*x),
Value::$name(None) => None,
_ => panic!("type error"),
}
}

fn type_name() -> &'static str {
concat!("Option<", stringify!($type), ">")
}
}

impl ValueTypeDefault for Option<$type> {
fn default() -> Self {
Default::default()
}
}
};
}

Expand Down Expand Up @@ -249,6 +211,46 @@ impl<'a> From<&'a str> for Value {
}
}

impl<'a> Nullable for &'a str {
fn null() -> Value {
Value::String(None)
}
}

impl<T> From<Option<T>> for Value
where T: Into<Value> + Nullable {
fn from(x: Option<T>) -> Value {
match x {
Some(v) => v.into(),
None => T::null(),
}
}
}

impl<T> ValueType for Option<T>
where T: ValueType + Nullable {
fn unwrap(v: Value) -> Self {
if v == T::null() {
None
}
else {
Some(v.unwrap())
}
}

fn type_name() -> &'static str {
//concat!("Option<", T::type_name(), ">")
T::type_name()
}
}

impl<T> ValueTypeDefault for Option<T>
where T: ValueType + Nullable {
fn default() -> Self {
Default::default()
}
}

type_to_box_value!(Vec<u8>, Bytes);
impl_value_type_default!(Vec<u8>);
type_to_box_value!(String, String);
Expand Down Expand Up @@ -297,12 +299,6 @@ mod with_chrono {
}
}

impl ValueTypeDefault for Option<DateTime<FixedOffset>> {
fn default() -> Self {
Default::default()
}
}

impl<Tz> From<DateTime<Tz>> for Value
where
Tz: TimeZone,
Expand All @@ -313,15 +309,9 @@ mod with_chrono {
}
}

impl<Tz> From<Option<DateTime<Tz>>> for Value
where
Tz: TimeZone,
{
fn from(x: Option<DateTime<Tz>>) -> Value {
match x {
Some(v) => From::<DateTime<Tz>>::from(v),
None => Value::DateTimeWithTimeZone(None),
}
impl Nullable for DateTime<FixedOffset> {
fn null() -> Value {
Value::DateTimeWithTimeZone(None)
}
}

Expand All @@ -337,19 +327,6 @@ mod with_chrono {
stringify!(DateTime<FixedOffset>)
}
}

impl ValueType for Option<DateTime<FixedOffset>> {
fn unwrap(v: Value) -> Self {
match v {
Value::DateTimeWithTimeZone(Some(x)) => Some(*x),
_ => panic!("type error"),
}
}

fn type_name() -> &'static str {
stringify!(Option<DateTime<FixedOffset>>)
}
}
}

#[cfg(feature = "with-rust_decimal")]
Expand Down

0 comments on commit 3c76e59

Please sign in to comment.