Skip to content

Commit

Permalink
Cont. Add serde skip options for hidden columns to the CLI (#1320)
Browse files Browse the repository at this point in the history
* Add serde skip options for hidden columns to the CLI (#1171)

* Add serde skip options for hidden columns to the CLI

* Resolve rustfmt and clippy issues

* Use SerdeDeriveOptions instead of WithSerde in tests

* Resolve upstream conflict

Co-authored-by: Billy Chan <[email protected]>

* [CLI] serde_skip_hidden_column

* clippy

* clippy

Co-authored-by: Jacob Trueb <[email protected]>
  • Loading branch information
billy1624 and trueb2 authored Dec 19, 2022
1 parent 3f00725 commit 9282ce2
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 30 deletions.
22 changes: 15 additions & 7 deletions sea-orm-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,21 @@ pub enum GenerateSubcommands {
)]
with_serde: String,

#[clap(
action,
long,
help = "Generate a serde field attribute, '#[serde(skip_deserializing)]', for the primary key fields to skip them during deserialization, this flag will be affective only when '--with-serde' is 'both' or 'deserialize'"
)]
serde_skip_deserializing_primary_key: bool,

#[clap(
action,
long,
default_value = "false",
help = "Opt-in to add skip attributes to hidden columns (i.e. when 'with-serde' enabled and column name starts with an underscore)"
)]
serde_skip_hidden_column: bool,

#[clap(
action,
long,
Expand Down Expand Up @@ -228,13 +243,6 @@ pub enum GenerateSubcommands {
help = "Generate index file as `lib.rs` instead of `mod.rs`."
)]
lib: bool,

#[clap(
action,
long,
help = "Generate a serde field attribute, '#[serde(skip_deserializing)]', for the primary key fields to skip them during deserialization, this flag will be affective only when '--with-serde' is 'both' or 'deserialize'"
)]
serde_skip_deserializing_primary_key: bool,
},
}

Expand Down
6 changes: 4 additions & 2 deletions sea-orm-cli/src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ pub async fn run_generate_command(
database_schema,
database_url,
with_serde,
serde_skip_deserializing_primary_key,
serde_skip_hidden_column,
with_copy_enums,
date_time_crate,
lib,
serde_skip_deserializing_primary_key,
} => {
if verbose {
let _ = tracing_subscriber::fmt()
Expand Down Expand Up @@ -160,12 +161,13 @@ pub async fn run_generate_command(

let writer_context = EntityWriterContext::new(
expanded_format,
WithSerde::from_str(&with_serde).unwrap(),
WithSerde::from_str(&with_serde).expect("Invalid serde derive option"),
with_copy_enums,
date_time_crate.into(),
schema_name,
lib,
serde_skip_deserializing_primary_key,
serde_skip_hidden_column,
);
let output = EntityTransformer::transform(table_stmts)?.generate(&writer_context);

Expand Down
6 changes: 4 additions & 2 deletions sea-orm-codegen/src/entity/base_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,19 @@ impl Entity {
.map_or(quote! {, Eq}, |_| quote! {})
}

pub fn get_serde_skip_deserializing(
pub fn get_column_serde_attributes(
&self,
serde_skip_deserializing_primary_key: bool,
serde_skip_hidden_column: bool,
) -> Vec<TokenStream> {
self.columns
.iter()
.map(|col| {
let is_primary_key = self.primary_keys.iter().any(|pk| pk.name == col.name);
col.get_serde_skip_deserializing(
col.get_serde_attribute(
is_primary_key,
serde_skip_deserializing_primary_key,
serde_skip_hidden_column,
)
})
.collect()
Expand Down
13 changes: 10 additions & 3 deletions sea-orm-codegen/src/entity/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,20 @@ impl Column {
info
}

pub fn get_serde_skip_deserializing(
pub fn get_serde_attribute(
&self,
is_primary_key: bool,
serde_skip_deserializing_primary_key: bool,
serde_skip_hidden_column: bool,
) -> TokenStream {
if serde_skip_deserializing_primary_key && is_primary_key {
quote! { #[serde(skip_deserializing)] }
if self.name.starts_with('_') && serde_skip_hidden_column {
quote! {
#[serde(skip)]
}
} else if serde_skip_deserializing_primary_key && is_primary_key {
quote! {
#[serde(skip_deserializing)]
}
} else {
quote! {}
}
Expand Down
Loading

0 comments on commit 9282ce2

Please sign in to comment.