MCPcopy Create free account
hub / github.com/SeaQL/sea-orm / cast_enum_as

Function cast_enum_as

src/entity/column.rs:444–487  ·  view source on GitHub ↗
(expr: Expr, col: &C, f: F)

Source from the content-addressed store, hash-verified

442}
443
444fn cast_enum_as<C, F>(expr: Expr, col: &C, f: F) -> SimpleExpr
445where
446 C: ColumnTrait,
447 F: Fn(Expr, DynIden, &ColumnType) -> SimpleExpr,
448{
449 let col_def = col.def();
450 let col_type = col_def.get_column_type();
451
452 match col_type {
453 #[cfg(all(feature = "with-json", feature = "postgres-array"))]
454 ColumnType::Json | ColumnType::JsonBinary => {
455 use sea_query::ArrayType;
456 use serde_json::Value as Json;
457
458 #[allow(clippy::boxed_local)]
459 fn unbox<T>(boxed: Box<T>) -> T {
460 *boxed
461 }
462
463 let expr = expr.into();
464 match expr {
465 SimpleExpr::Value(Value::Array(ArrayType::Json, Some(json_vec))) => {
466 // flatten Array(Vec<Json>) into Json
467 let json_vec: Vec<Json> = json_vec
468 .into_iter()
469 .filter_map(|val| match val {
470 Value::Json(Some(json)) => Some(unbox(json)),
471 _ => None,
472 })
473 .collect();
474 SimpleExpr::Value(Value::Json(Some(Box::new(json_vec.into()))))
475 }
476 SimpleExpr::Value(Value::Array(ArrayType::Json, None)) => {
477 SimpleExpr::Value(Value::Json(None))
478 }
479 _ => expr,
480 }
481 }
482 _ => match col_type.get_enum_name() {
483 Some(enum_name) => f(expr, SeaRc::clone(enum_name), col_type),
484 None => expr.into(),
485 },
486 }
487}
488
489#[cfg(test)]
490mod tests {

Callers 2

select_enum_asMethod · 0.85
save_enum_asMethod · 0.85

Calls 6

unboxFunction · 0.85
get_column_typeMethod · 0.80
into_iterMethod · 0.80
newFunction · 0.50
defMethod · 0.45
get_enum_nameMethod · 0.45

Tested by

no test coverage detected