Skip to content

Commit

Permalink
Moving to using merge crate to reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
aadi58002 committed Aug 18, 2023
1 parent 5959ad8 commit 996b9fa
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 46 deletions.
1 change: 1 addition & 0 deletions sea-orm-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 6 additions & 8 deletions sea-orm-cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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<bool>,
Expand Down Expand Up @@ -204,11 +205,7 @@ pub struct GenerateSubCommandsEntity {
)]
pub max_connections: Option<u32>,

#[arg(
short = 'o',
long,
help = "Entity file output directory"
)]
#[arg(short = 'o', long, help = "Entity file output directory")]
pub output_dir: Option<String>,

#[arg(
Expand Down Expand Up @@ -287,8 +284,9 @@ pub struct GenerateSubCommandsEntity {
pub seaography: Option<bool>,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum, Deserialize)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum, Default, Deserialize)]
pub enum DateTimeCrate {
#[default]
Chrono,
Time,
}
59 changes: 22 additions & 37 deletions sea-orm-cli/src/commands/generate.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use merge::Merge;
use sea_orm_codegen::{
DateTimeCrate as CodegenDateTimeCrate, EntityTransformer, EntityWriterContext, OutputFile,
WithSerde,
Expand All @@ -8,17 +9,6 @@ use url::Url;

use crate::{parse_config, DateTimeCrate, GenerateSubCommandsEntity, GenerateSubcommands};

fn merge_options<T: std::fmt::Debug>(default: Option<T>,config: Option<T>,cli: Option<T>) -> Option<T>{
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<GenerateSubCommandsEntity, Box<dyn Error>> {
Expand All @@ -45,32 +35,17 @@ fn merge_cli_config_generate_entity(
};

if let Some(ref config_path) = command.config {
let config_values = parse_config::<GenerateSubCommandsEntity>(config_path.to_string())?;
let mut config_values = parse_config::<GenerateSubCommandsEntity>(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)
}
Expand All @@ -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,
Expand All @@ -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(),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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!(),
}
Expand All @@ -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!(),
}
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ pub mod config;

#[cfg(feature = "cli")]
pub use cli::*;
pub use config::*;
pub use commands::*;
pub use config::*;

0 comments on commit 996b9fa

Please sign in to comment.