Skip to content

Commit 797b323

Browse files
committed
[src/code.rs] Generate impl Default for CRUD structs. BUG: column names don't match struct
1 parent 25161ca commit 797b323

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src/code.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -778,25 +778,24 @@ fn default_for_type(typ: String) -> &'static str {
778778
}
779779

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

795794
buffer.push_str(" Self {\n");
796795
let fields_to_defaults = column_name_type_nullable
797796
.map(|(name, typ, nullable)| {
798797
format!(
799-
" param_{name}: {typ_default}",
798+
" {name}: {typ_default}",
800799
name = name,
801800
typ_default = if nullable {
802801
"None"
@@ -815,6 +814,7 @@ fn build_default_impl_fn(table: &ParsedTableMacro) -> String {
815814
/// Generate a full file for a given diesel table
816815
pub fn generate_for_table(table: &ParsedTableMacro, config: &GenerationConfig) -> String {
817816
// early to ensure the table options are set for the current table
817+
let struct_name = table.struct_name.to_string();
818818
let table_options = config.table(&table.name.to_string());
819819

820820
let mut ret_buffer = format!("{FILE_SIGNATURE}\n\n");
@@ -831,13 +831,35 @@ pub fn generate_for_table(table: &ParsedTableMacro, config: &GenerationConfig) -
831831
if create_struct.has_code() {
832832
ret_buffer.push('\n');
833833
ret_buffer.push_str(create_struct.code());
834+
if config.options.default_impl {
835+
ret_buffer.push('\n');
836+
ret_buffer.push_str(
837+
build_default_impl_fn(
838+
&format!("Create{struct_name}"),
839+
&create_struct.table.columns,
840+
)
841+
.as_str(),
842+
);
843+
}
844+
ret_buffer.push('\n');
834845
}
835846

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

838849
if update_struct.has_code() {
839850
ret_buffer.push('\n');
840851
ret_buffer.push_str(update_struct.code());
852+
if config.options.default_impl {
853+
ret_buffer.push('\n');
854+
ret_buffer.push_str(
855+
build_default_impl_fn(
856+
&format!("Update{struct_name}"),
857+
&update_struct.table.columns,
858+
)
859+
.as_str(),
860+
);
861+
}
862+
ret_buffer.push('\n');
841863
}
842864

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

849871
if config.options.default_impl {
850872
ret_buffer.push('\n');
851-
ret_buffer.push_str(build_default_impl_fn(table).as_str());
873+
ret_buffer.push_str(build_default_impl_fn(&struct_name, &table.columns).as_str());
852874
ret_buffer.push('\n');
853875
}
854876

0 commit comments

Comments
 (0)