Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement generator for impl Default #142

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

SamuelMarks
Copy link
Contributor

No description provided.

@SamuelMarks
Copy link
Contributor Author

@Wulf Something weird is happening on my latest commit:

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');
}

The update_struct.table.columns has column names which are only present in the other structs; but not the update one. Which table should I use?

Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests (see simple_table_sqlite as a example), aside from that some minor style improvements.

src/code.rs Outdated Show resolved Hide resolved
src/code.rs Outdated Show resolved Hide resolved
src/code.rs Outdated Show resolved Hide resolved
src/code.rs Outdated Show resolved Hide resolved
src/code.rs Outdated Show resolved Hide resolved
src/code.rs Outdated Show resolved Hide resolved
src/code.rs Outdated Show resolved Hide resolved
src/code.rs Outdated Show resolved Hide resolved
src/code.rs Outdated Show resolved Hide resolved
@hasezoey
Copy link
Collaborator

hasezoey commented Nov 28, 2024

btw, we dont have a CONTRIBUTING currently, but the commit style is angular/conventional-ish, see the commit history for examples.

@SamuelMarks
Copy link
Contributor Author

Good suggestions. Yeah I still need to add tests, but it should be ready now (aside from that).

Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks better, but still the tests are missing (at least one for being enabled with most common types).

Also consider using r# and multiple lines instead of \n for the format! (like other code in the file), example:

dsync/src/code.rs

Lines 497 to 515 in 2797966

r##"
/// Insert a new row into `{table_name}` with a given [`{create_struct_identifier}`]
pub{async_keyword} fn create(db: &mut ConnectionType, item: &{create_struct_identifier}) -> diesel::QueryResult<Self> {{
use {schema_path}{table_name}::dsl::*;
diesel::insert_into({table_name}).values(item).get_result::<Self>(db){await_keyword}
}}
"##
));
} else {
buffer.push_str(&format!(
r##"
/// Insert a new row into `{table_name}` with all default values
pub{async_keyword} fn create(db: &mut ConnectionType) -> diesel::QueryResult<Self> {{
use {schema_path}{table_name}::dsl::*;
diesel::insert_into({table_name}).default_values().get_result::<Self>(db){await_keyword}
}}
"##

PS: also existing test output needs to be updated if it is meant to change. (though to my understanding it shouldnt change as long as default_impl is false, which it is by default)

buffer
format!(
"impl Default for {struct_name} {{\n fn default() -> Self {{\n Self {{\n{f2d}\n }}\n }}\n}}",
struct_name = struct_name, f2d=fields_to_defaults.as_str()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
struct_name = struct_name, f2d=fields_to_defaults.as_str()
f2d=fields_to_defaults.as_str()

unless i missed something, that shouldnt be required anymore.

i think f2d should also be able to be directly embedded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants