Skip to content

Commit

Permalink
fix: use consistent module and directory paths for tables
Browse files Browse the repository at this point in the history
fixes mismatch between a tables imports and the directory structure
  • Loading branch information
hasezoey committed Nov 2, 2023
1 parent ce8d41b commit b69ff2a
Show file tree
Hide file tree
Showing 19 changed files with 27 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/code.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use heck::{ToPascalCase, ToSnakeCase};
use heck::ToPascalCase;
use indoc::formatdoc;
use std::borrow::Cow;

use crate::parser::{ParsedColumnMacro, ParsedTableMacro, FILE_SIGNATURE};
use crate::{GenerationConfig, TableOptions};
use crate::{get_table_module_name, GenerationConfig, TableOptions};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum StructType {
Expand Down Expand Up @@ -606,7 +606,7 @@ fn build_imports(table: &ParsedTableMacro, config: &GenerationConfig) -> String
imports_vec.extend(table.foreign_keys.iter().map(|fk| {
format!(
"use {model_path}{foreign_table_name_model}::{singular_struct_name};",
foreign_table_name_model = fk.0.to_string().to_snake_case().to_lowercase(),
foreign_table_name_model = get_table_module_name(&fk.0.to_string()),
singular_struct_name = fk.0.to_string().to_pascal_case(),
model_path = config.model_path
)
Expand Down
19 changes: 14 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod parser;
use error::IOErrorToError;
pub use error::{Error, Result};
use file::MarkedFile;
use heck::ToSnakeCase;
use parser::ParsedTableMacro;
pub use parser::FILE_SIGNATURE;
use std::collections::HashMap;
Expand Down Expand Up @@ -350,6 +351,14 @@ impl From<&MarkedFile> for FileChange {
}
}

/// Helper function for consistent table module name generation
/// this is used for the rust module path name and for the filename
///
/// input: "tableA", output -> "table_a"
fn get_table_module_name(table_name: &str) -> String {
table_name.to_snake_case().to_lowercase()
}

/// Generate all Models for a given diesel schema file
///
/// Models are saved to disk
Expand Down Expand Up @@ -412,11 +421,12 @@ pub fn generate_files(
return Err(Error::other("Cannot have a table named \"common\" while having option \"once_common_structs\" enabled"));
}
let table_name = table.name.to_string();
let table_filename = get_table_module_name(&table_name);
let table_config = config.table(&table_name);
let table_dir = if table_config.single_model_file {
output_models_dir.to_owned()
} else {
output_models_dir.join(&table_name)
output_models_dir.join(&table_filename)
};

if !table_dir.exists() {
Expand Down Expand Up @@ -451,11 +461,12 @@ pub fn generate_files(
file_changes.push(FileChange::from(&table_mod_rs));
}

mod_rs.ensure_mod_stmt(&table.name.to_string());
mod_rs.ensure_mod_stmt(&table_filename);
}

// pass 2: delete code for removed tables
for item in std::fs::read_dir(output_models_dir).attach_path_err(output_models_dir)? {
// TODO: this does not work with "single-model-file"
let item = item.attach_path_err(output_models_dir)?;

// check if item is a directory
Expand All @@ -482,9 +493,7 @@ pub fn generate_files(
item.path()
)))?;
let found = generated.iter().find(|g| {
g.name
.to_string()
.eq_ignore_ascii_case(associated_table_name)
get_table_module_name(&g.name.to_string()).eq_ignore_ascii_case(associated_table_name)
});
if found.is_some() {
continue;
Expand Down
4 changes: 2 additions & 2 deletions test/custom_model_and_schema_path/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod tableA;
pub mod tableB;
pub mod table_a;
pub mod table_b;
4 changes: 2 additions & 2 deletions test/custom_model_path/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod tableA;
pub mod tableB;
pub mod table_a;
pub mod table_b;
File renamed without changes.
File renamed without changes.
9 changes: 6 additions & 3 deletions test/readonly/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
pub mod normal;
pub mod prefixTable;
pub mod tableSuffix;
pub mod prefixTableSuffix;



pub mod prefix_table;
pub mod table_suffix;
pub mod prefix_table_suffix;
File renamed without changes.
File renamed without changes.

0 comments on commit b69ff2a

Please sign in to comment.