Skip to content

Commit

Permalink
Add serde skip options for hidden columns to the CLI (#1171)
Browse files Browse the repository at this point in the history
* 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 <ccw.billy.123@gmail.com>
  • Loading branch information
trueb2 and billy1624 authored Dec 19, 2022
1 parent 1f27837 commit 09075f5
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 64 deletions.
8 changes: 8 additions & 0 deletions sea-orm-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@ pub enum GenerateSubcommands {
)]
with_serde: String,

#[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_columns: bool,

#[clap(
action,
long,
Expand Down
8 changes: 5 additions & 3 deletions sea-orm-cli/src/commands/generate.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use sea_orm_codegen::{
DateTimeCrate as CodegenDateTimeCrate, EntityTransformer, EntityWriterContext, OutputFile,
WithSerde,
SerdeDeriveOptions,
};
use std::{error::Error, fs, io::Write, path::Path, process::Command, str::FromStr};
use std::{error::Error, fs, io::Write, path::Path, process::Command};
use tracing_subscriber::{prelude::*, EnvFilter};
use url::Url;

Expand All @@ -24,6 +24,7 @@ pub async fn run_generate_command(
database_schema,
database_url,
with_serde,
serde_skip_hidden_columns,
with_copy_enums,
date_time_crate,
lib,
Expand Down Expand Up @@ -159,7 +160,8 @@ pub async fn run_generate_command(

let writer_context = EntityWriterContext::new(
expanded_format,
WithSerde::from_str(&with_serde).unwrap(),
SerdeDeriveOptions::from_options(&with_serde, serde_skip_hidden_columns)
.expect("Serde derive options must be valid"),
with_copy_enums,
date_time_crate.into(),
schema_name,
Expand Down
12 changes: 8 additions & 4 deletions sea-orm-codegen/src/entity/active_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use sea_query::DynIden;

use crate::WithSerde;
use crate::SerdeDeriveOptions;

#[derive(Clone, Debug)]
pub struct ActiveEnum {
Expand All @@ -12,7 +12,11 @@ pub struct ActiveEnum {
}

impl ActiveEnum {
pub fn impl_active_enum(&self, with_serde: &WithSerde, with_copy_enums: bool) -> TokenStream {
pub fn impl_active_enum(
&self,
serde_options: &SerdeDeriveOptions,
with_copy_enums: bool,
) -> TokenStream {
let enum_name = &self.enum_name.to_string();
let enum_iden = format_ident!("{}", enum_name.to_camel_case());
let values: Vec<String> = self.values.iter().map(|v| v.to_string()).collect();
Expand All @@ -24,7 +28,7 @@ impl ActiveEnum {
}
});

let extra_derive = with_serde.extra_derive();
let extra_derive = serde_options.extra_derive();
let copy_derive = if with_copy_enums {
quote! { , Copy }
} else {
Expand Down Expand Up @@ -72,7 +76,7 @@ mod tests {
.map(|variant| Alias::new(variant).into_iden())
.collect(),
}
.impl_active_enum(&WithSerde::None, true)
.impl_active_enum(&SerdeDeriveOptions::None, true)
.to_string(),
quote!(
#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Copy)]
Expand Down
Loading

0 comments on commit 09075f5

Please sign in to comment.