Skip to content
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

Introduce Nullable trait to permit custom Option<T> #112

Merged
merged 5 commits into from
Aug 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
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(), ">")
Copy link
Member

Choose a reason for hiding this comment

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

Um... so you think the type name shouldn't be Option<_>?

Copy link
Author

Choose a reason for hiding this comment

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

Probably it should, but I failed to find a way to generate a static string from a method calling another method... And then I forgot about it

Copy link
Author

Choose a reason for hiding this comment

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

maybe whit const trait fn stabilized it's possible, I don't know

Copy link
Author

Choose a reason for hiding this comment

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

I was thinking https://doc.rust-lang.org/std/any/fn.type_name.html could have been a solution, but looking at the example I saw why it can't be

Copy link
Member

@tyt2y3 tyt2y3 Aug 28, 2021

Choose a reason for hiding this comment

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

So this type_name is mainly for diagnostic purpose. May be we wouldn't mind generating the string dynamically.

Copy link
Author

Choose a reason for hiding this comment

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

If you change the return type to Cow<'static, str> you can still return static str in normal types and allocate String only in options

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