diff --git a/upub/core/src/ext.rs b/upub/core/src/ext.rs index 2b02a15..9d9899f 100644 --- a/upub/core/src/ext.rs +++ b/upub/core/src/ext.rs @@ -64,7 +64,30 @@ impl Default for JsonVec { } } -impl sea_orm::TryGetableFromJson for JsonVec {} +// TODO we need this dummy to access the default implementation, which needs to be wrapped to catch +// nulls. is there a way to directly call super::try_get_from_json ?? i think this gets +// compiled into a lot of variants... +#[derive(serde::Deserialize)] +struct DummyVec(pub Vec); +impl sea_orm::TryGetableFromJson for DummyVec {} + +impl sea_orm::TryGetableFromJson for JsonVec { + fn try_get_from_json(res: &sea_orm::QueryResult, idx: I) -> Result { + match DummyVec::try_get_from_json(res, idx) { + Ok(DummyVec(x)) => Ok(Self(x)), + Err(sea_orm::TryGetError::Null(_)) => Ok(Self::default()), + Err(e) => Err(e), + } + } + + fn from_json_vec(value: serde_json::Value) -> Result, sea_orm::TryGetError> { + match DummyVec::from_json_vec(value) { + Ok(x) => Ok(x.into_iter().map(|x| JsonVec(x.0)).collect()), + Err(sea_orm::TryGetError::Null(_)) => Ok(vec![]), + Err(e) => Err(e), + } + } +} impl std::convert::From> for sea_orm::Value { fn from(source: JsonVec) -> Self {