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

mapping null values with unknown type to Value enum #838

Open
Upbolt opened this issue Nov 19, 2024 · 2 comments
Open

mapping null values with unknown type to Value enum #838

Upbolt opened this issue Nov 19, 2024 · 2 comments

Comments

@Upbolt
Copy link

Upbolt commented Nov 19, 2024

trying to use cloudflare d1 with sea-orm through the example proxy integration, but

https://github.com/SeaQL/sea-orm/blob/5f1a9d2a46c9b0776c57311782e72dab8e5c6948/examples/proxy_cloudflare_worker_example/src/orm.rs#L63-L97

null values hit the _ case since internally D1Result::results() returns a javascript array which will treat null values as their own type when deserialized through serde, making it incompatible with sea-query's Value enum, which requires to know the type ahead of time

sea-query/src/value.rs

Lines 127 to 216 in b91ba14

pub enum Value {
Bool(Option<bool>),
TinyInt(Option<i8>),
SmallInt(Option<i16>),
Int(Option<i32>),
BigInt(Option<i64>),
TinyUnsigned(Option<u8>),
SmallUnsigned(Option<u16>),
Unsigned(Option<u32>),
BigUnsigned(Option<u64>),
Float(Option<f32>),
Double(Option<f64>),
String(Option<Box<String>>),
Char(Option<char>),
#[allow(clippy::box_collection)]
Bytes(Option<Box<Vec<u8>>>),
#[cfg(feature = "with-json")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-json")))]
Json(Option<Box<Json>>),
#[cfg(feature = "with-chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
ChronoDate(Option<Box<NaiveDate>>),
#[cfg(feature = "with-chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
ChronoTime(Option<Box<NaiveTime>>),
#[cfg(feature = "with-chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
ChronoDateTime(Option<Box<NaiveDateTime>>),
#[cfg(feature = "with-chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
ChronoDateTimeUtc(Option<Box<DateTime<Utc>>>),
#[cfg(feature = "with-chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
ChronoDateTimeLocal(Option<Box<DateTime<Local>>>),
#[cfg(feature = "with-chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
ChronoDateTimeWithTimeZone(Option<Box<DateTime<FixedOffset>>>),
#[cfg(feature = "with-time")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
TimeDate(Option<Box<time::Date>>),
#[cfg(feature = "with-time")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
TimeTime(Option<Box<time::Time>>),
#[cfg(feature = "with-time")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
TimeDateTime(Option<Box<PrimitiveDateTime>>),
#[cfg(feature = "with-time")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
TimeDateTimeWithTimeZone(Option<Box<OffsetDateTime>>),
#[cfg(feature = "with-uuid")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-uuid")))]
Uuid(Option<Box<Uuid>>),
#[cfg(feature = "with-rust_decimal")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-rust_decimal")))]
Decimal(Option<Box<Decimal>>),
#[cfg(feature = "with-bigdecimal")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-bigdecimal")))]
BigDecimal(Option<Box<BigDecimal>>),
#[cfg(feature = "postgres-array")]
#[cfg_attr(docsrs, doc(cfg(feature = "postgres-array")))]
Array(ArrayType, Option<Box<Vec<Value>>>),
#[cfg(feature = "postgres-vector")]
#[cfg_attr(docsrs, doc(cfg(feature = "postgres-vector")))]
Vector(Option<Box<pgvector::Vector>>),
#[cfg(feature = "with-ipnetwork")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-ipnetwork")))]
IpNetwork(Option<Box<IpNetwork>>),
#[cfg(feature = "with-mac_address")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-mac_address")))]
MacAddress(Option<Box<MacAddress>>),
}

is there a reasonable way to accommodate for this?

@Upbolt
Copy link
Author

Upbolt commented Nov 22, 2024

a non-ideal solution i found was to make an Option<T> newtype that i use in my models, and in their TryGetable impls return Some(T) if the resulting value != T::default()

this comes with an obvious flaw in that T::default() may actually be a non-None value

@Upbolt
Copy link
Author

Upbolt commented Nov 22, 2024

i'm still fairly new to sea-orm, but seeing how we are given the names of columns it may be possible to extract type information from models using the column's name which can be used to map nulls to their actual Value variants

although at this point i guess it becomes a sea-orm issue and not a sea-query issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant