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

Cli serde skip deserialize for primary key option #1186

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add CLI option to skip primary keys with serde
Implements: #841
  • Loading branch information
Witcher01 committed Jul 6, 2022
commit fea009a9f1d05a07feb7c012e31e836ce0cddb9a
7 changes: 7 additions & 0 deletions sea-orm-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,12 @@ pub enum GenerateSubcommands {
help = "Automatically derive serde Serialize / Deserialize traits for the entity (none, serialize, deserialize, both)"
)]
with_serde: String,

#[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,
},
}
12 changes: 7 additions & 5 deletions sea-orm-cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub async fn run_generate_command(
database_schema,
database_url,
with_serde,
skip_primary_key_deserialization,
} => {
if verbose {
let _ = tracing_subscriber::fmt()
Expand Down Expand Up @@ -88,9 +89,7 @@ pub async fn run_generate_command(
}
};

let filter_skip_tables = |table: &String| -> bool {
!ignore_tables.contains(table)
};
let filter_skip_tables = |table: &String| -> bool { !ignore_tables.contains(table) };

let database_name = if !is_sqlite {
// The database name should be the first element of the path string
Expand Down Expand Up @@ -175,8 +174,11 @@ pub async fn run_generate_command(
_ => unimplemented!("{} is not supported", url.scheme()),
};

let output = EntityTransformer::transform(table_stmts)?
.generate(expanded_format, WithSerde::from_str(&with_serde).unwrap());
let output = EntityTransformer::transform(table_stmts)?.generate(
expanded_format,
WithSerde::from_str(&with_serde).unwrap(),
skip_primary_key_deserialization,
);

let dir = Path::new(&output_dir);
fs::create_dir_all(dir)?;
Expand Down
63 changes: 53 additions & 10 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,18 @@ impl FromStr for WithSerde {
}

impl EntityWriter {
pub fn generate(self, expanded_format: bool, with_serde: WithSerde) -> WriterOutput {
pub fn generate(
self,
expanded_format: bool,
with_serde: WithSerde,
skip_primary_key_deserialization: bool,
) -> WriterOutput {
let mut files = Vec::new();
files.extend(self.write_entities(expanded_format, &with_serde));
files.extend(self.write_entities(
expanded_format,
&with_serde,
skip_primary_key_deserialization,
));
files.push(self.write_mod());
files.push(self.write_prelude());
if !self.enums.is_empty() {
Expand All @@ -91,7 +100,12 @@ impl EntityWriter {
WriterOutput { files }
}

pub fn write_entities(&self, expanded_format: bool, with_serde: &WithSerde) -> Vec<OutputFile> {
pub fn write_entities(
&self,
expanded_format: bool,
with_serde: &WithSerde,
skip_primary_key_deserialization: bool,
) -> Vec<OutputFile> {
self.entities
.iter()
.map(|entity| {
Expand All @@ -112,7 +126,11 @@ impl EntityWriter {
let code_blocks = if expanded_format {
Self::gen_expanded_code_blocks(entity, with_serde)
} else {
Self::gen_compact_code_blocks(entity, with_serde)
Self::gen_compact_code_blocks(
entity,
with_serde,
skip_primary_key_deserialization,
)
};
Self::write(&mut lines, code_blocks);
OutputFile {
Expand Down Expand Up @@ -217,10 +235,17 @@ impl EntityWriter {
code_blocks
}

pub fn gen_compact_code_blocks(entity: &Entity, with_serde: &WithSerde) -> Vec<TokenStream> {
pub fn gen_compact_code_blocks(
entity: &Entity,
with_serde: &WithSerde,
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)];
let mut code_blocks = vec![
imports,
Self::gen_compact_model_struct(entity, with_serde, skip_primary_key_deserialization),
];
let relation_defs = if entity.get_relation_enum_name().is_empty() {
vec![
Self::gen_relation_enum(entity),
Expand Down Expand Up @@ -479,7 +504,11 @@ impl EntityWriter {
}
}

pub fn gen_compact_model_struct(entity: &Entity, with_serde: &WithSerde) -> TokenStream {
pub fn gen_compact_model_struct(
entity: &Entity,
with_serde: &WithSerde,
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();
let column_rs_types = entity.get_column_rs_types();
Expand All @@ -493,6 +522,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 @@ -502,6 +532,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 @@ -520,9 +556,16 @@ impl EntityWriter {
}
ts = quote! { #ts #attr };
}
quote! {
#[sea_orm(#ts)]
}
return if skip_pk_deserialization {
quote! {
#[sea_orm(#ts)]
#[serde(skip_deserialization)]
}
} else {
quote! {
#[sea_orm(#ts)]
}
};
} else {
TokenStream::new()
}
Expand Down