Skip to content

flatten for optionals #2934

Open
Open
@cemoktra

Description

@cemoktra

Is your feature request related to a problem? Please describe.
When selecting multiple columns from a left outer join (optional) i might get a set of 5 optional fields which i want to have in one optional struct that is none if nothing to join.

Describe the solution you'd like

#[derive(FromRow)]
struct DbRow {
  id_of_main_table: Uuid,
  otherstuff: (),
  #[sqlx(flatten)]
  inner: Option<Joined>
}

#[derive(FromRow)]
struct Joined {
  id_of_join: Uuid,
  more_joined_stuff: ()
}

Describe alternatives you've considered
Alternatives are having all fields optional and writing a huge tuple match to convert into an optional struct or use a helper crate like tuple_combinator

The manual approach would like:

#[derive(FromRow)]
struct DbRow {
  id_of_main_table: Uuid,
  otherstuff: (),
  //
  id_of_join: Option<Uuid>,
  more_joined_stuff: Option<()>,
}

// some conversion like
let joined = if let (Some(id_of_join),  Some(more_joined_stuff)) = (id_of_join, more_joined_stuff) {
  Some(Joined {
    id_of_join,
    more_joined_stuff
  })
} else {
  None
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions