Skip to content

Commit bfe293d

Browse files
committed
Moving to using merge crate to reduce code duplication
1 parent 6595674 commit bfe293d

File tree

4 files changed

+30
-46
lines changed

4 files changed

+30
-46
lines changed

sea-orm-cli/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ regex = { version = "1", default-features = false }
4848
glob = { version = "0.3", default-features = false }
4949
serde = { version = "1.0.183", features = ["derive"] }
5050
serde_json = "1.0.105"
51+
merge = "0.1.0"
5152

5253
[dev-dependencies]
5354
smol = "1.2.5"

sea-orm-cli/src/cli.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use clap::{ArgGroup, Parser, Subcommand, ValueEnum, Args};
1+
use clap::{ArgGroup, Args, Parser, Subcommand, ValueEnum};
2+
use merge::Merge;
23
use serde::Deserialize;
34

45
#[derive(Parser, Debug)]
@@ -165,7 +166,7 @@ pub enum GenerateSubcommands {
165166
Entity(GenerateSubCommandsEntity),
166167
}
167168

168-
#[derive(Args,PartialEq, Eq, Debug, Deserialize)]
169+
#[derive(Args, PartialEq, Eq, Debug, Merge, Deserialize, Default)]
169170
pub struct GenerateSubCommandsEntity {
170171
#[arg(long, help = "Generate entity file of compact format")]
171172
pub compact_format: Option<bool>,
@@ -204,11 +205,7 @@ pub struct GenerateSubCommandsEntity {
204205
)]
205206
pub max_connections: Option<u32>,
206207

207-
#[arg(
208-
short = 'o',
209-
long,
210-
help = "Entity file output directory"
211-
)]
208+
#[arg(short = 'o', long, help = "Entity file output directory")]
212209
pub output_dir: Option<String>,
213210

214211
#[arg(
@@ -287,8 +284,9 @@ pub struct GenerateSubCommandsEntity {
287284
pub seaography: Option<bool>,
288285
}
289286

290-
#[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum, Deserialize)]
287+
#[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum, Default, Deserialize)]
291288
pub enum DateTimeCrate {
289+
#[default]
292290
Chrono,
293291
Time,
294292
}

sea-orm-cli/src/commands/generate.rs

+22-37
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use merge::Merge;
12
use sea_orm_codegen::{
23
DateTimeCrate as CodegenDateTimeCrate, EntityTransformer, EntityWriterContext, OutputFile,
34
WithSerde,
@@ -8,17 +9,6 @@ use url::Url;
89

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

11-
fn merge_options<T: std::fmt::Debug>(default: Option<T>,config: Option<T>,cli: Option<T>) -> Option<T>{
12-
dbg!(&default,&config,&cli);
13-
if cli.is_some(){
14-
cli
15-
}else if config.is_some(){
16-
config
17-
}else{
18-
default
19-
}
20-
}
21-
2212
fn merge_cli_config_generate_entity(
2313
mut command: GenerateSubCommandsEntity,
2414
) -> Result<GenerateSubCommandsEntity, Box<dyn Error>> {
@@ -45,32 +35,17 @@ fn merge_cli_config_generate_entity(
4535
};
4636

4737
if let Some(ref config_path) = command.config {
48-
let config_values = parse_config::<GenerateSubCommandsEntity>(config_path.to_string())?;
38+
let mut config_values = parse_config::<GenerateSubCommandsEntity>(config_path.to_string())?;
4939
if Option::is_some(&config_values.database_url) {
5040
return Err("Database Url is set in the config which is not recommended".into());
5141
}
5242
if Option::is_some(&config_values.max_connections) {
5343
return Err("Max Connections is set in the config which is not recommended".into());
5444
}
55-
56-
command.compact_format = merge_options(default_values.compact_format,config_values.compact_format,command.compact_format);
57-
command.expanded_format = merge_options(default_values.expanded_format,config_values.expanded_format,command.expanded_format);
58-
command.include_hidden_tables = merge_options(default_values.include_hidden_tables,config_values.include_hidden_tables,command.include_hidden_tables);
59-
command.tables = merge_options(default_values.tables,config_values.tables,command.tables);
60-
command.ignore_tables = merge_options(default_values.ignore_tables,config_values.ignore_tables,command.ignore_tables);
61-
command.max_connections = merge_options(default_values.max_connections,config_values.max_connections,command.max_connections);
62-
command.output_dir = merge_options(default_values.output_dir,config_values.output_dir,command.output_dir);
63-
command.database_schema = merge_options(default_values.database_schema,config_values.database_schema,command.database_schema);
64-
command.database_url = merge_options(default_values.database_url,config_values.database_url,command.database_url);
65-
command.with_serde = merge_options(default_values.with_serde,config_values.with_serde,command.with_serde);
66-
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);
67-
command.serde_skip_hidden_column = merge_options(default_values.serde_skip_hidden_column,config_values.serde_skip_hidden_column,command.serde_skip_hidden_column);
68-
command.with_copy_enums = merge_options(default_values.with_copy_enums,config_values.with_copy_enums,command.with_copy_enums);
69-
command.date_time_crate = merge_options(default_values.date_time_crate,config_values.date_time_crate,command.date_time_crate);
70-
command.lib = merge_options(default_values.lib,config_values.lib,command.lib);
71-
command.model_extra_derives = merge_options(default_values.model_extra_derives,config_values.model_extra_derives,command.model_extra_derives);
72-
command.model_extra_attributes = merge_options(default_values.model_extra_attributes,config_values.model_extra_attributes,command.model_extra_attributes);
73-
command.seaography = merge_options(default_values.seaography ,config_values.seaography ,command.seaography );
45+
config_values.merge(default_values);
46+
command.merge(config_values);
47+
} else {
48+
command.merge(default_values);
7449
}
7550
Ok(command)
7651
}
@@ -83,7 +58,6 @@ pub async fn run_generate_command(
8358
GenerateSubcommands::Entity(command) => {
8459
let command = merge_cli_config_generate_entity(command)?;
8560
let (
86-
_,
8761
expanded_format,
8862
include_hidden_tables,
8963
tables,
@@ -102,7 +76,6 @@ pub async fn run_generate_command(
10276
model_extra_attributes,
10377
seaography,
10478
) = (
105-
command.compact_format.unwrap(),
10679
command.expanded_format.unwrap(),
10780
command.include_hidden_tables.unwrap(),
10881
command.tables.unwrap(),
@@ -365,14 +338,22 @@ mod tests {
365338
GenerateSubCommandsEntity {
366339
compact_format: Some(true),
367340
expanded_format: Some(true),
368-
config: Some(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src/config/tests/parse.json").into_os_string().into_string().unwrap()),
341+
config: Some(
342+
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
343+
.join("src/config/tests/parse.json")
344+
.into_os_string()
345+
.into_string()
346+
.unwrap()
347+
),
369348
include_hidden_tables: Some(true),
370349
tables: Some(vec!["my_tables".to_string()]),
371350
ignore_tables: Some(vec!["seaql_migrations".to_string()]),
372351
max_connections: Some(1),
373352
output_dir: Some("out".to_string()),
374353
database_schema: Some("public".to_string()),
375-
database_url: Some("postgres://root:root@localhost:3306/database".to_string()),
354+
database_url: Some(
355+
"postgres://root:root@localhost:3306/database".to_string()
356+
),
376357
with_serde: Some("none".to_string()),
377358
serde_skip_deserializing_primary_key: Some(false),
378359
serde_skip_hidden_column: Some(false),
@@ -408,7 +389,9 @@ mod tests {
408389

409390
match cli.command {
410391
Commands::Generate { command } => match command {
411-
GenerateSubcommands::Entity(command) => {let _ = merge_cli_config_generate_entity(command).unwrap();},
392+
GenerateSubcommands::Entity(command) => {
393+
let _ = merge_cli_config_generate_entity(command).unwrap();
394+
}
412395
},
413396
_ => unreachable!(),
414397
}
@@ -433,7 +416,9 @@ mod tests {
433416
]);
434417
match cli.command {
435418
Commands::Generate { command } => match command {
436-
GenerateSubcommands::Entity(command) => {let _ = merge_cli_config_generate_entity(command).unwrap();},
419+
GenerateSubcommands::Entity(command) => {
420+
let _ = merge_cli_config_generate_entity(command).unwrap();
421+
}
437422
},
438423
_ => unreachable!(),
439424
}

sea-orm-cli/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ pub mod config;
55

66
#[cfg(feature = "cli")]
77
pub use cli::*;
8-
pub use config::*;
98
pub use commands::*;
9+
pub use config::*;

0 commit comments

Comments
 (0)