Skip to content

Commit

Permalink
Introduce optional serde support for model code generation
Browse files Browse the repository at this point in the history
This introduces several things to optionally support automatic derive attributes for `serde::{Deserialize, Serialize}` for the generated models:
- introduces a feature flag for `sea-orm-codegen` which optionally includes the serde dependency (can be activated via `cargo build / install --features with-serde`),
- introduces a `WithSerde` enum to indicate if Serialize, Deserialize, or even both should be derived from,
- introduces a feature flag for `sea-orm-cli` which optionally activates the `sea-orm-codegen` feature flag `with-serde` (can also be activated via `cargo build / install --features with-serde`),
- adds an optional cli argument `--with-serde [none: default, serialize, deserialize, both]`
- adds test harness for both compact and expanded generation

Potential TODOs:
- Add conditional compilation for the cli argument `--with-serde`
  • Loading branch information
elbart committed Oct 11, 2021
1 parent 664afa4 commit c31fb3c
Show file tree
Hide file tree
Showing 21 changed files with 984 additions and 35 deletions.
5 changes: 4 additions & 1 deletion sea-orm-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ env_logger = { version = "^0.9" }
log = { version = "^0.4" }

[features]
default = [ "runtime-async-std-native-tls" ]
default = [ "runtime-async-std-native-tls", "with-serde" ]
runtime-actix-native-tls = [
"sqlx/runtime-actix-native-tls",
"sea-schema/runtime-actix-native-tls",
Expand All @@ -59,3 +59,6 @@ runtime-tokio-rustls = [
"sqlx/runtime-tokio-rustls",
"sea-schema/runtime-tokio-rustls",
]
with-serde = [
"sea-orm-codegen/with-serde",
]
8 changes: 8 additions & 0 deletions sea-orm-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ pub fn build_cli() -> App<'static, 'static> {
.help("Generate entity file of compact format")
.takes_value(false)
.conflicts_with("EXPANDED_FORMAT"),
)
// TODO: How to do conditional compilation for feature: "with-serde"
.arg(
Arg::with_name("WITH_SERDE")
.long("with-serde")
.help("Automatically derive serde Serialize / Deserialize traits for the entity (none, serialize, deserialize, both)")
.takes_value(true)
.default_value("none")
),
)
.setting(AppSettings::SubcommandRequiredElseHelp);
Expand Down
15 changes: 10 additions & 5 deletions sea-orm-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use clap::ArgMatches;
use dotenv::dotenv;
use log::LevelFilter;
use sea_orm_codegen::{EntityTransformer, OutputFile};
use std::{error::Error, fmt::Display, fs, io::Write, path::Path, process::Command};
use sea_orm_codegen::{EntityTransformer, OutputFile, WithSerde};
use std::{error::Error, fmt::Display, fs, io::Write, path::Path, process::Command, str::FromStr};

mod cli;

Expand All @@ -26,13 +26,17 @@ async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box<dyn Er
let url = args.value_of("DATABASE_URL").unwrap();
let output_dir = args.value_of("OUTPUT_DIR").unwrap();
let include_hidden_tables = args.is_present("INCLUDE_HIDDEN_TABLES");
let tables = args.values_of("TABLES").unwrap_or_default().collect::<Vec<_>>();
let tables = args
.values_of("TABLES")
.unwrap_or_default()
.collect::<Vec<_>>();
let expanded_format = args.is_present("EXPANDED_FORMAT");
let with_serde = args.value_of("WITH_SERDE").unwrap();
let filter_tables = |table: &str| -> bool {
if tables.len() > 0 {
return tables.contains(&table);
}

true
};
let filter_hidden_tables = |table: &str| -> bool {
Expand Down Expand Up @@ -84,7 +88,8 @@ async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box<dyn Er
panic!("This database is not supported ({})", url)
};

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

let dir = Path::new(output_dir);
fs::create_dir_all(dir)?;
Expand Down
4 changes: 4 additions & 0 deletions sea-orm-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ syn = { version = "^1", default-features = false, features = [
quote = "^1"
heck = "^0.3"
proc-macro2 = "^1"
serde = {version = "^1", optional = true }

[dev-dependencies]
pretty_assertions = { version = "^0.7" }

[features]
with-serde = [ "serde" ]
Loading

0 comments on commit c31fb3c

Please sign in to comment.