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) #1318

Merged
merged 4 commits into from
Dec 19, 2022
Merged
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
Prev Previous commit
Next Next commit
[cli] should be #[serde(skip_deserializing)]
billy1624 committed Dec 19, 2022

Unverified

This user has not yet uploaded their public signing key.
commit 2b66b3e37b8e95d29c4999d7151abd72e8a8c2e5
4 changes: 2 additions & 2 deletions sea-orm-cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -232,9 +232,9 @@ pub enum GenerateSubcommands {
#[clap(
action,
long,
help = "Generate a serde field attribute for the primary keys to skip them during deserialization if they're not present, must also use '--with-serde' 'both' or 'deserialize'"
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'"
)]
skip_primary_key_deserialization: bool,
skip_deserializing_primary_key: bool,
},
}

4 changes: 2 additions & 2 deletions sea-orm-cli/src/commands/generate.rs
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ pub async fn run_generate_command(
with_copy_enums,
date_time_crate,
lib,
skip_primary_key_deserialization,
skip_deserializing_primary_key,
} => {
if verbose {
let _ = tracing_subscriber::fmt()
@@ -165,7 +165,7 @@ pub async fn run_generate_command(
date_time_crate.into(),
schema_name,
lib,
skip_primary_key_deserialization,
skip_deserializing_primary_key,
);
let output = EntityTransformer::transform(table_stmts)?.generate(&writer_context);

36 changes: 18 additions & 18 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@ pub struct EntityWriterContext {
pub(crate) date_time_crate: DateTimeCrate,
pub(crate) schema_name: Option<String>,
pub(crate) lib: bool,
pub(crate) skip_primary_key_deserialization: bool,
pub(crate) skip_deserializing_primary_key: bool,
}

impl WithSerde {
@@ -104,7 +104,7 @@ impl EntityWriterContext {
date_time_crate: DateTimeCrate,
schema_name: Option<String>,
lib: bool,
skip_primary_key_deserialization: bool,
skip_deserializing_primary_key: bool,
) -> Self {
Self {
expanded_format,
@@ -113,7 +113,7 @@ impl EntityWriterContext {
date_time_crate,
schema_name,
lib,
skip_primary_key_deserialization,
skip_deserializing_primary_key,
}
}
}
@@ -143,7 +143,7 @@ impl EntityWriter {
.map(|column| column.get_info(&context.date_time_crate))
.collect::<Vec<String>>();
// use must have serde enabled to use this
let skip_primary_key_deserialization = context.skip_primary_key_deserialization
let skip_deserializing_primary_key = context.skip_deserializing_primary_key
&& (context.with_serde == WithSerde::Both
|| context.with_serde == WithSerde::Deserialize);

@@ -160,15 +160,15 @@ impl EntityWriter {
&context.with_serde,
&context.date_time_crate,
&context.schema_name,
skip_primary_key_deserialization,
skip_deserializing_primary_key,
)
} else {
Self::gen_compact_code_blocks(
entity,
&context.with_serde,
&context.date_time_crate,
&context.schema_name,
skip_primary_key_deserialization,
skip_deserializing_primary_key,
)
};
Self::write(&mut lines, code_blocks);
@@ -268,7 +268,7 @@ impl EntityWriter {
with_serde: &WithSerde,
date_time_crate: &DateTimeCrate,
schema_name: &Option<String>,
skip_primary_key_deserialization: bool,
skip_deserializing_primary_key: bool,
) -> Vec<TokenStream> {
let mut imports = Self::gen_import(with_serde);
imports.extend(Self::gen_import_active_enum(entity));
@@ -280,7 +280,7 @@ impl EntityWriter {
entity,
with_serde,
date_time_crate,
skip_primary_key_deserialization,
skip_deserializing_primary_key,
),
Self::gen_column_enum(entity),
Self::gen_primary_key_enum(entity),
@@ -300,7 +300,7 @@ impl EntityWriter {
with_serde: &WithSerde,
date_time_crate: &DateTimeCrate,
schema_name: &Option<String>,
skip_primary_key_deserialization: bool,
skip_deserializing_primary_key: bool,
) -> Vec<TokenStream> {
let mut imports = Self::gen_import(with_serde);
imports.extend(Self::gen_import_active_enum(entity));
@@ -311,7 +311,7 @@ impl EntityWriter {
with_serde,
date_time_crate,
schema_name,
skip_primary_key_deserialization,
skip_deserializing_primary_key,
),
Self::gen_compact_relation_enum(entity),
];
@@ -400,7 +400,7 @@ impl EntityWriter {
entity: &Entity,
with_serde: &WithSerde,
date_time_crate: &DateTimeCrate,
skip_primary_key_deserialization: bool,
skip_deserializing_primary_key: bool,
) -> TokenStream {
let column_names_snake_case = entity.get_column_names_snake_case();
let column_rs_types = entity.get_column_rs_types(date_time_crate);
@@ -415,10 +415,10 @@ impl EntityWriter {
|tokens, (i, field_name)| {
let field_type = column_rs_types.get(i).unwrap();
let is_primary_key = primary_keys.contains(&field_name.to_string());
if is_primary_key && skip_primary_key_deserialization {
if is_primary_key && skip_deserializing_primary_key {
quote! {
#tokens
#[serde(skip_deserialization)]
#[serde(skip_deserializing)]
pub #field_name: #field_type,
}
} else {
@@ -614,7 +614,7 @@ impl EntityWriter {
with_serde: &WithSerde,
date_time_crate: &DateTimeCrate,
schema_name: &Option<String>,
skip_primary_key_deserialization: bool,
skip_deserializing_primary_key: bool,
) -> TokenStream {
let table_name = entity.table_name.as_str();
let column_names_snake_case = entity.get_column_names_snake_case();
@@ -658,10 +658,10 @@ impl EntityWriter {
}
ts = quote! { #ts #attr };
}
if is_primary_key && skip_primary_key_deserialization {
if is_primary_key && skip_deserializing_primary_key {
quote! {
#[sea_orm(#ts)]
#[serde(skip_deserialization)]
#[serde(skip_deserializing)]
}
} else {
quote! {
@@ -1558,7 +1558,7 @@ mod tests {
) -> io::Result<()> {
let mut reader = BufReader::new(entity_serde_variant.0.as_bytes());
let mut lines: Vec<String> = Vec::new();
let skip_primary_key_deserialization = entity_serde_variant.1 == WithSerde::Both
let skip_deserializing_primary_key = entity_serde_variant.1 == WithSerde::Both
|| entity_serde_variant.1 == WithSerde::Deserialize;

reader.read_until(b'\n', &mut Vec::new())?;
@@ -1576,7 +1576,7 @@ mod tests {
&entity_serde_variant.1,
&DateTimeCrate::Chrono,
&entity_serde_variant.2,
skip_primary_key_deserialization,
skip_deserializing_primary_key,
)
.into_iter()
.fold(TokenStream::new(), |mut acc, tok| {
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/compact_with_serde/cake_both.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};
#[sea_orm(table_name = "cake")]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserialization)]
#[serde(skip_deserializing)]
pub id: i32,
#[sea_orm(column_type = "Text", nullable)]
pub name: Option<String> ,
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ use serde::Deserialize;
#[sea_orm(table_name = "cake")]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserialization)]
#[serde(skip_deserializing)]
pub id: i32,
#[sea_orm(column_type = "Text", nullable)]
pub name: Option<String> ,
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/expanded_with_serde/cake_both.rs
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ impl EntityName for Entity {

#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Eq, Serialize, Deserialize)]
pub struct Model {
#[serde(skip_deserialization)]
#[serde(skip_deserializing)]
pub id: i32,
pub name: Option<String> ,
}
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ impl EntityName for Entity {

#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Eq, Deserialize)]
pub struct Model {
#[serde(skip_deserialization)]
#[serde(skip_deserializing)]
pub id: i32,
pub name: Option<String> ,
}