Skip to content

Commit

Permalink
[src/code.rs] Generate impl Default for CRUD structs. BUG: column n…
Browse files Browse the repository at this point in the history
…ames don't match struct
  • Loading branch information
SamuelMarks committed Nov 28, 2024
1 parent 25161ca commit 797b323
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -778,25 +778,24 @@ fn default_for_type(typ: String) -> &'static str {
}

/// Generate default (insides of the `impl Default for StructName { fn default() -> Self {} }`)
fn build_default_impl_fn(table: &ParsedTableMacro) -> String {
let mut buffer = String::with_capacity(table.struct_name.len() + table.columns.len() * 4);
fn build_default_impl_fn(struct_name: &str, columns: &Vec<ParsedColumnMacro>) -> String {
let mut buffer = String::with_capacity(struct_name.len() + columns.len() * 4);
buffer.push_str(&format!(
"impl Default for {struct_name} {{\n fn default() -> Self {{\n",
struct_name = table.struct_name.as_str()
struct_name = struct_name
));
let column_name_type_nullable: Map<
Iter<ParsedColumnMacro>,
fn(&ParsedColumnMacro) -> (String, String, bool),
> = table
.columns
> = columns
.iter()
.map(|col| (col.name.to_string(), col.ty.to_string(), col.is_nullable));

buffer.push_str(" Self {\n");
let fields_to_defaults = column_name_type_nullable
.map(|(name, typ, nullable)| {
format!(
" param_{name}: {typ_default}",
" {name}: {typ_default}",
name = name,
typ_default = if nullable {
"None"
Expand All @@ -815,6 +814,7 @@ fn build_default_impl_fn(table: &ParsedTableMacro) -> String {
/// Generate a full file for a given diesel table
pub fn generate_for_table(table: &ParsedTableMacro, config: &GenerationConfig) -> String {
// early to ensure the table options are set for the current table
let struct_name = table.struct_name.to_string();
let table_options = config.table(&table.name.to_string());

let mut ret_buffer = format!("{FILE_SIGNATURE}\n\n");
Expand All @@ -831,13 +831,35 @@ pub fn generate_for_table(table: &ParsedTableMacro, config: &GenerationConfig) -
if create_struct.has_code() {
ret_buffer.push('\n');
ret_buffer.push_str(create_struct.code());
if config.options.default_impl {
ret_buffer.push('\n');
ret_buffer.push_str(
build_default_impl_fn(
&format!("Create{struct_name}"),
&create_struct.table.columns,
)
.as_str(),
);
}
ret_buffer.push('\n');
}

let update_struct = Struct::new(StructType::Update, table, config);

if update_struct.has_code() {
ret_buffer.push('\n');
ret_buffer.push_str(update_struct.code());
if config.options.default_impl {
ret_buffer.push('\n');
ret_buffer.push_str(
build_default_impl_fn(
&format!("Update{struct_name}"),
&update_struct.table.columns,
)
.as_str(),
);
}
ret_buffer.push('\n');
}

// third, push functions - if enabled
Expand All @@ -848,7 +870,7 @@ pub fn generate_for_table(table: &ParsedTableMacro, config: &GenerationConfig) -

if config.options.default_impl {
ret_buffer.push('\n');
ret_buffer.push_str(build_default_impl_fn(table).as_str());
ret_buffer.push_str(build_default_impl_fn(&struct_name, &table.columns).as_str());
ret_buffer.push('\n');
}

Expand Down

0 comments on commit 797b323

Please sign in to comment.