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

Add CLI option to sea-orm-cli to skip primary keys with serde on generation of entities #846

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions sea-orm-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ pub enum GenerateSubcommands {
help = "The datetime crate to use for generating entities."
)]
date_time_crate: DateTimeCrate,

#[clap(
action,
long,
help = "Generate a serde field attribute for the primary keys to skip them during deserialization if they're not present"
)]
skip_primary_key_deserialization: bool,
},
}

Expand Down
2 changes: 2 additions & 0 deletions sea-orm-cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub async fn run_generate_command(
database_url,
with_serde,
date_time_crate,
skip_primary_key_deserialization,
} => {
if verbose {
let _ = tracing_subscriber::fmt()
Expand Down Expand Up @@ -171,6 +172,7 @@ pub async fn run_generate_command(
WithSerde::from_str(&with_serde).unwrap(),
date_time_crate.into(),
schema_name,
skip_primary_key_deserialization,
);
let output = EntityTransformer::transform(table_stmts)?.generate(&writer_context);

Expand Down
64 changes: 54 additions & 10 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub struct EntityWriterContext {
pub(crate) with_serde: WithSerde,
pub(crate) date_time_crate: DateTimeCrate,
pub(crate) schema_name: Option<String>,
pub(crate) skip_primary_key_deserialization: bool,
}

impl WithSerde {
Expand Down Expand Up @@ -99,12 +100,14 @@ impl EntityWriterContext {
with_serde: WithSerde,
date_time_crate: DateTimeCrate,
schema_name: Option<String>,
skip_primary_key_deserialization: bool,
) -> Self {
Self {
expanded_format,
with_serde,
date_time_crate,
schema_name,
skip_primary_key_deserialization,
}
}
}
Expand Down Expand Up @@ -145,13 +148,15 @@ impl EntityWriter {
&context.with_serde,
&context.date_time_crate,
&context.schema_name,
context.skip_primary_key_deserialization,
)
} else {
Self::gen_compact_code_blocks(
entity,
&context.with_serde,
&context.date_time_crate,
&context.schema_name,
context.skip_primary_key_deserialization,
)
};
Self::write(&mut lines, code_blocks);
Expand Down Expand Up @@ -241,6 +246,7 @@ impl EntityWriter {
with_serde: &WithSerde,
date_time_crate: &DateTimeCrate,
schema_name: &Option<String>,
_skip_primary_key_deserialization: bool,
) -> Vec<TokenStream> {
let mut imports = Self::gen_import(with_serde);
imports.extend(Self::gen_import_active_enum(entity));
Expand All @@ -267,12 +273,19 @@ impl EntityWriter {
with_serde: &WithSerde,
date_time_crate: &DateTimeCrate,
schema_name: &Option<String>,
skip_primary_key_deserialization: bool,
) -> Vec<TokenStream> {
let mut imports = Self::gen_import(with_serde);
imports.extend(Self::gen_import_active_enum(entity));
let mut code_blocks = vec![
imports,
Self::gen_compact_model_struct(entity, with_serde, date_time_crate, schema_name),
Self::gen_compact_model_struct(
entity,
with_serde,
date_time_crate,
schema_name,
skip_primary_key_deserialization,
),
];
let relation_defs = if entity.get_relation_enum_name().is_empty() {
vec![
Expand Down Expand Up @@ -553,6 +566,7 @@ impl EntityWriter {
with_serde: &WithSerde,
date_time_crate: &DateTimeCrate,
schema_name: &Option<String>,
skip_primary_key_deserialization: bool,
) -> TokenStream {
let table_name = entity.table_name.as_str();
let column_names_snake_case = entity.get_column_names_snake_case();
Expand All @@ -567,6 +581,7 @@ impl EntityWriter {
.iter()
.map(|col| {
let mut attrs: Punctuated<_, Comma> = Punctuated::new();
let mut skip_pk_deserialization = false;
if !col.is_snake_case_name() {
let column_name = &col.name;
attrs.push(quote! { column_name = #column_name });
Expand All @@ -576,6 +591,12 @@ impl EntityWriter {
if !col.auto_increment {
attrs.push(quote! { auto_increment = false });
}

// if deserialization with serde is even desired, set it to what the user
// wants
if with_serde == &WithSerde::Deserialize || with_serde == &WithSerde::Both {
skip_pk_deserialization = skip_primary_key_deserialization;
}
}
if let Some(ts) = col.get_col_type_attrs() {
attrs.extend(vec![ts]);
Expand All @@ -594,8 +615,15 @@ impl EntityWriter {
}
ts = quote! { #ts #attr };
}
quote! {
#[sea_orm(#ts)]
if skip_pk_deserialization {
quote! {
#[sea_orm(#ts)]
#[serde(skip_deserialization)]
}
} else {
quote! {
#[sea_orm(#ts)]
}
Comment on lines +618 to +626
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Witcher01, sorry for the delay! Thanks for the great contributions!! Looks good overall :)

I have a minor nitpick. Please check if the refactoring make any sense

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries! I'm not sure what you mean, my commit looks good to me, tests run, and running it gives me the right results, too.
Could you elaborate? Thanks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to keep a mutable skip_pk_deserialization and that's why refactored the code :P

}
} else {
TokenStream::new()
Expand Down Expand Up @@ -1073,7 +1101,8 @@ mod tests {
entity,
&crate::WithSerde::None,
&crate::DateTimeCrate::Chrono,
&None
&None,
false,
)
.into_iter()
.skip(1)
Expand All @@ -1089,7 +1118,8 @@ mod tests {
entity,
&crate::WithSerde::None,
&crate::DateTimeCrate::Chrono,
&Some("public".to_owned())
&Some("public".to_owned()),
false,
)
.into_iter()
.skip(1)
Expand All @@ -1105,7 +1135,8 @@ mod tests {
entity,
&crate::WithSerde::None,
&crate::DateTimeCrate::Chrono,
&Some("schema_name".to_owned())
&Some("schema_name".to_owned()),
false,
)
.into_iter()
.skip(1)
Expand Down Expand Up @@ -1149,7 +1180,8 @@ mod tests {
entity,
&crate::WithSerde::None,
&crate::DateTimeCrate::Chrono,
&None
&None,
false,
)
.into_iter()
.skip(1)
Expand All @@ -1165,7 +1197,8 @@ mod tests {
entity,
&crate::WithSerde::None,
&crate::DateTimeCrate::Chrono,
&Some("public".to_owned())
&Some("public".to_owned()),
false,
)
.into_iter()
.skip(1)
Expand All @@ -1181,7 +1214,8 @@ mod tests {
entity,
&crate::WithSerde::None,
&crate::DateTimeCrate::Chrono,
&Some("schema_name".to_owned())
&Some("schema_name".to_owned()),
false,
)
.into_iter()
.skip(1)
Expand Down Expand Up @@ -1211,6 +1245,7 @@ mod tests {
None,
),
Box::new(EntityWriter::gen_compact_code_blocks),
false,
)?;
assert_serde_variant_results(
&cake_entity,
Expand All @@ -1220,6 +1255,7 @@ mod tests {
None,
),
Box::new(EntityWriter::gen_compact_code_blocks),
false,
)?;
assert_serde_variant_results(
&cake_entity,
Expand All @@ -1229,6 +1265,7 @@ mod tests {
None,
),
Box::new(EntityWriter::gen_compact_code_blocks),
false,
)?;
assert_serde_variant_results(
&cake_entity,
Expand All @@ -1238,6 +1275,7 @@ mod tests {
None,
),
Box::new(EntityWriter::gen_compact_code_blocks),
false,
)?;

// Expanded code blocks
Expand All @@ -1249,6 +1287,7 @@ mod tests {
None,
),
Box::new(EntityWriter::gen_expanded_code_blocks),
false,
)?;
assert_serde_variant_results(
&cake_entity,
Expand All @@ -1258,6 +1297,7 @@ mod tests {
None,
),
Box::new(EntityWriter::gen_expanded_code_blocks),
false,
)?;
assert_serde_variant_results(
&cake_entity,
Expand All @@ -1267,6 +1307,7 @@ mod tests {
None,
),
Box::new(EntityWriter::gen_expanded_code_blocks),
false,
)?;
assert_serde_variant_results(
&cake_entity,
Expand All @@ -1276,6 +1317,7 @@ mod tests {
None,
),
Box::new(EntityWriter::gen_expanded_code_blocks),
false,
)?;

Ok(())
Expand All @@ -1286,8 +1328,9 @@ mod tests {
cake_entity: &Entity,
entity_serde_variant: &(String, WithSerde, Option<String>),
generator: Box<
dyn Fn(&Entity, &WithSerde, &DateTimeCrate, &Option<String>) -> Vec<TokenStream>,
dyn Fn(&Entity, &WithSerde, &DateTimeCrate, &Option<String>, bool) -> Vec<TokenStream>,
>,
primary_key_auto_increment: bool,
) -> io::Result<()> {
let mut reader = BufReader::new(entity_serde_variant.0.as_bytes());
let mut lines: Vec<String> = Vec::new();
Expand All @@ -1306,6 +1349,7 @@ mod tests {
&entity_serde_variant.1,
&DateTimeCrate::Chrono,
&entity_serde_variant.2,
false,
)
.into_iter()
.fold(TokenStream::new(), |mut acc, tok| {
Expand Down