From 996b9fa424ec9b0b75fad2ddb0bba3909470934d Mon Sep 17 00:00:00 2001 From: aadi58002 Date: Fri, 18 Aug 2023 16:11:06 +0530 Subject: [PATCH] Moving to using merge crate to reduce code duplication --- sea-orm-cli/Cargo.toml | 1 + sea-orm-cli/src/cli.rs | 14 +++---- sea-orm-cli/src/commands/generate.rs | 59 +++++++++++----------------- sea-orm-cli/src/lib.rs | 2 +- 4 files changed, 30 insertions(+), 46 deletions(-) diff --git a/sea-orm-cli/Cargo.toml b/sea-orm-cli/Cargo.toml index 7193dfb0e..b68770351 100644 --- a/sea-orm-cli/Cargo.toml +++ b/sea-orm-cli/Cargo.toml @@ -48,6 +48,7 @@ regex = { version = "1", default-features = false } glob = { version = "0.3", default-features = false } serde = { version = "1.0.183", features = ["derive"] } serde_json = "1.0.105" +merge = "0.1.0" [dev-dependencies] smol = "1.2.5" diff --git a/sea-orm-cli/src/cli.rs b/sea-orm-cli/src/cli.rs index 9323a6eae..d0db73137 100644 --- a/sea-orm-cli/src/cli.rs +++ b/sea-orm-cli/src/cli.rs @@ -1,4 +1,5 @@ -use clap::{ArgGroup, Parser, Subcommand, ValueEnum, Args}; +use clap::{ArgGroup, Args, Parser, Subcommand, ValueEnum}; +use merge::Merge; use serde::Deserialize; #[derive(Parser, Debug)] @@ -165,7 +166,7 @@ pub enum GenerateSubcommands { Entity(GenerateSubCommandsEntity), } -#[derive(Args,PartialEq, Eq, Debug, Deserialize)] +#[derive(Args, PartialEq, Eq, Debug, Merge, Deserialize, Default)] pub struct GenerateSubCommandsEntity { #[arg(long, help = "Generate entity file of compact format")] pub compact_format: Option, @@ -204,11 +205,7 @@ pub struct GenerateSubCommandsEntity { )] pub max_connections: Option, - #[arg( - short = 'o', - long, - help = "Entity file output directory" - )] + #[arg(short = 'o', long, help = "Entity file output directory")] pub output_dir: Option, #[arg( @@ -287,8 +284,9 @@ pub struct GenerateSubCommandsEntity { pub seaography: Option, } -#[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum, Deserialize)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum, Default, Deserialize)] pub enum DateTimeCrate { + #[default] Chrono, Time, } diff --git a/sea-orm-cli/src/commands/generate.rs b/sea-orm-cli/src/commands/generate.rs index 6ab9cf6d4..8357b2b59 100644 --- a/sea-orm-cli/src/commands/generate.rs +++ b/sea-orm-cli/src/commands/generate.rs @@ -1,3 +1,4 @@ +use merge::Merge; use sea_orm_codegen::{ DateTimeCrate as CodegenDateTimeCrate, EntityTransformer, EntityWriterContext, OutputFile, WithSerde, @@ -8,17 +9,6 @@ use url::Url; use crate::{parse_config, DateTimeCrate, GenerateSubCommandsEntity, GenerateSubcommands}; -fn merge_options(default: Option,config: Option,cli: Option) -> Option{ - dbg!(&default,&config,&cli); - if cli.is_some(){ - cli - }else if config.is_some(){ - config - }else{ - default - } -} - fn merge_cli_config_generate_entity( mut command: GenerateSubCommandsEntity, ) -> Result> { @@ -45,32 +35,17 @@ fn merge_cli_config_generate_entity( }; if let Some(ref config_path) = command.config { - let config_values = parse_config::(config_path.to_string())?; + let mut config_values = parse_config::(config_path.to_string())?; if Option::is_some(&config_values.database_url) { return Err("Database Url is set in the config which is not recommended".into()); } if Option::is_some(&config_values.max_connections) { return Err("Max Connections is set in the config which is not recommended".into()); } - - command.compact_format = merge_options(default_values.compact_format,config_values.compact_format,command.compact_format); - command.expanded_format = merge_options(default_values.expanded_format,config_values.expanded_format,command.expanded_format); - command.include_hidden_tables = merge_options(default_values.include_hidden_tables,config_values.include_hidden_tables,command.include_hidden_tables); - command.tables = merge_options(default_values.tables,config_values.tables,command.tables); - command.ignore_tables = merge_options(default_values.ignore_tables,config_values.ignore_tables,command.ignore_tables); - command.max_connections = merge_options(default_values.max_connections,config_values.max_connections,command.max_connections); - command.output_dir = merge_options(default_values.output_dir,config_values.output_dir,command.output_dir); - command.database_schema = merge_options(default_values.database_schema,config_values.database_schema,command.database_schema); - command.database_url = merge_options(default_values.database_url,config_values.database_url,command.database_url); - command.with_serde = merge_options(default_values.with_serde,config_values.with_serde,command.with_serde); - command.serde_skip_deserializing_primary_key = merge_options(default_values.serde_skip_deserializing_primary_key,config_values.serde_skip_deserializing_primary_key,command.serde_skip_deserializing_primary_key); - command.serde_skip_hidden_column = merge_options(default_values.serde_skip_hidden_column,config_values.serde_skip_hidden_column,command.serde_skip_hidden_column); - command.with_copy_enums = merge_options(default_values.with_copy_enums,config_values.with_copy_enums,command.with_copy_enums); - command.date_time_crate = merge_options(default_values.date_time_crate,config_values.date_time_crate,command.date_time_crate); - command.lib = merge_options(default_values.lib,config_values.lib,command.lib); - command.model_extra_derives = merge_options(default_values.model_extra_derives,config_values.model_extra_derives,command.model_extra_derives); - command.model_extra_attributes = merge_options(default_values.model_extra_attributes,config_values.model_extra_attributes,command.model_extra_attributes); - command.seaography = merge_options(default_values.seaography ,config_values.seaography ,command.seaography ); + config_values.merge(default_values); + command.merge(config_values); + } else { + command.merge(default_values); } Ok(command) } @@ -83,7 +58,6 @@ pub async fn run_generate_command( GenerateSubcommands::Entity(command) => { let command = merge_cli_config_generate_entity(command)?; let ( - _, expanded_format, include_hidden_tables, tables, @@ -102,7 +76,6 @@ pub async fn run_generate_command( model_extra_attributes, seaography, ) = ( - command.compact_format.unwrap(), command.expanded_format.unwrap(), command.include_hidden_tables.unwrap(), command.tables.unwrap(), @@ -365,14 +338,22 @@ mod tests { GenerateSubCommandsEntity { compact_format: Some(true), expanded_format: Some(true), - config: Some(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src/config/tests/parse.json").into_os_string().into_string().unwrap()), + config: Some( + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("src/config/tests/parse.json") + .into_os_string() + .into_string() + .unwrap() + ), include_hidden_tables: Some(true), tables: Some(vec!["my_tables".to_string()]), ignore_tables: Some(vec!["seaql_migrations".to_string()]), max_connections: Some(1), output_dir: Some("out".to_string()), database_schema: Some("public".to_string()), - database_url: Some("postgres://root:root@localhost:3306/database".to_string()), + database_url: Some( + "postgres://root:root@localhost:3306/database".to_string() + ), with_serde: Some("none".to_string()), serde_skip_deserializing_primary_key: Some(false), serde_skip_hidden_column: Some(false), @@ -408,7 +389,9 @@ mod tests { match cli.command { Commands::Generate { command } => match command { - GenerateSubcommands::Entity(command) => {let _ = merge_cli_config_generate_entity(command).unwrap();}, + GenerateSubcommands::Entity(command) => { + let _ = merge_cli_config_generate_entity(command).unwrap(); + } }, _ => unreachable!(), } @@ -433,7 +416,9 @@ mod tests { ]); match cli.command { Commands::Generate { command } => match command { - GenerateSubcommands::Entity(command) => {let _ = merge_cli_config_generate_entity(command).unwrap();}, + GenerateSubcommands::Entity(command) => { + let _ = merge_cli_config_generate_entity(command).unwrap(); + } }, _ => unreachable!(), } diff --git a/sea-orm-cli/src/lib.rs b/sea-orm-cli/src/lib.rs index 097c8bd18..156d7d9fe 100644 --- a/sea-orm-cli/src/lib.rs +++ b/sea-orm-cli/src/lib.rs @@ -5,5 +5,5 @@ pub mod config; #[cfg(feature = "cli")] pub use cli::*; -pub use config::*; pub use commands::*; +pub use config::*;