Skip to content

Adds support custom name resolver in codegen#680

Closed
negezor wants to merge 176 commits intoSeaQL:masterfrom
negezor:resolve-module-names
Closed

Adds support custom name resolver in codegen#680
negezor wants to merge 176 commits intoSeaQL:masterfrom
negezor:resolve-module-names

Conversation

@negezor
Copy link
Copy Markdown
Contributor

@negezor negezor commented Apr 22, 2022

I added the NameResolver trait for better maintainability. I haven't touched columns and other parts that don't depend on module names yet, and I haven't deleted the getter code either.

Adds

  • Adds a new argument to sea-orm-cli --singularize, which generates singular module names.
  • Create your own name transformations in sea-orm-codegen

Breaking Changes

  • Requires one more argument in EntityTransformer::transform

Comment on lines +45 to +57
impl NameResolver for SingularNameResolver {
fn resolve_module_name(&self, name: &str) -> String {
singular(name.to_snake_case())
}

fn resolve_entity_name(&self, name: &str) -> String {
singular(name.to_camel_case())
}

fn resolve_relation_name(&self, name: &str) -> String {
singular(name.to_camel_case())
}
} No newline at end of file
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Having a name resolver is a good idea :)
Can we supply the output of NameResolver's default implementation to singular method?

Copy link
Copy Markdown
Contributor Author

@negezor negezor Apr 22, 2022

Choose a reason for hiding this comment

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

@billy1624 of course. Then under what flag to leave the current behavior? 🤔

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

i don't understand this statement either

Copy link
Copy Markdown
Member

@tyt2y3 tyt2y3 left a comment

Choose a reason for hiding this comment

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

Actually, we don't need a trait here. Everything can be done by a struct NameResolver and it only needs to have a state singularize: bool which can only be configured in the constructor NameResolver::new()

This is better that the last PR indeed

@negezor negezor requested a review from tyt2y3 April 28, 2022 10:28
Comment on lines 154 to 157
let name_resolver = NameResolver::new(singularize);

let output = EntityTransformer::transform(table_stmts, name_resolver)?
.generate(expanded_format, WithSerde::from_str(with_serde).unwrap());
Copy link
Copy Markdown
Member

@billy1624 billy1624 Apr 28, 2022

Choose a reason for hiding this comment

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

Hey @negezor, thanks for the updates! I think the semantic isn't seems right. The transformer, EntityTransformer::transform(table_stmts, name_resolver), don't need name resolver. Instead, we could have introduce a new struct called EntityWriterContext which contains three things...

pub struct EntityWriterContext {
  pub(crate) expanded_format: bool,
  pub(crate) with_serde: WithSerde,
  pub(crate) name_resolver: NameResolver,
}

Then, EntityWriter::generate method would take an EntityWriterContext. Thoughts?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree indeed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@billy1624 In the case where we discussed about saving a context info on which datetime crate the user chooses in the --date-time-crate flag for sea-orm-cli, we'd basically write:

pub struct EntityWriterContext {
  pub(crate) expanded_format: bool,
  pub(crate) with_serde: WithSerde,
  pub(crate) name_resolver: NameResolver,
  pub(crate) date_time_crate: &str,
}

and pass this context all the way down to Column.get_rs_type() for resolving the Rust types based on chrono vs time? :)

Comment on lines +162 to +182
pub fn resolve_conjunct_relations_via_module_name(&self, name_resolver: &NameResolver) -> Vec<Ident> {
self.conjunct_relations
.iter()
.map(|con_rel| con_rel.resolve_via_module_name(name_resolver))
.collect()
}

pub fn resolve_conjunct_relations_to_module_name(&self, name_resolver: &NameResolver) -> Vec<Ident> {
self.conjunct_relations
.iter()
.map(|con_rel| con_rel.resolve_to_module_name(name_resolver))
.collect()
}

pub fn resolve_conjunct_relations_to_relation_name(&self, name_resolver: &NameResolver) -> Vec<Ident> {
self.conjunct_relations
.iter()
.map(|con_rel| con_rel.resolve_to_relation_name(name_resolver))
.collect()
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

All these resolve_* methods (here, and in other files except those in name_resolver.rs) seems just a duplicate of the original methods few line below, e.g. resolve_conjunct_relations_via_module_name method is just get_conjunct_relations_via_snake_case method with name resolve applied. We should change the logic in the original method, i.e. applying name resolver in it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I didn't touch them originally, as that would have required more changes. Also, they are now more than just getters. Perhaps this one could be changed by later refactoring. 🤔

Copy link
Copy Markdown
Member

@tyt2y3 tyt2y3 left a comment

Choose a reason for hiding this comment

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

As suggested by @billy1624

@nahuakang
Copy link
Copy Markdown
Contributor

Also, since in both of our PRs we're using EntityWriterContext, maybe we could merge this PR and PR#724 sequentially? I'm happy to help with the context update, too.

@billy1624
Copy link
Copy Markdown
Member

Hey @negezor, any updates? And do you need any help? :)

@billy1624
Copy link
Copy Markdown
Member

Also, since in both of our PRs we're using EntityWriterContext, maybe we could merge this PR and PR#724 sequentially? I'm happy to help with the context update, too.

Good suggestions! We could consolidate all flags into EntityWriterContext.

pmnxis and others added 24 commits August 14, 2022 21:09
* introduce rustfmt

* Cargofmt on all sea-orm crates & examples

* Revert testing script

* cargo fmt --manifest-path sea-orm-rocket/Cargo.toml --all

* add a script for formatting

* add nightly component for formatting

* set timeout and manual nightly install  in github action

* add flag for manifest-path

Co-authored-by: Billy Chan <[email protected]>
* Fix clippy warnings

* cargo fmt

* Fix clippy warnings

* cargo fmt
@negezor
Copy link
Copy Markdown
Contributor Author

negezor commented Aug 14, 2022

I think I did the rebase wrong somehow 😅

Comment on lines +16 to +44
pub fn resolve_module_name(&self, name: &str) -> String {
let name = name.to_snake_case();

if self.singularize {
singular(name)
} else {
name
}
}

pub fn resolve_entity_name(&self, name: &str) -> String {
let name = name.to_camel_case();

if self.singularize {
singular(name)
} else {
name
}
}

pub fn resolve_relation_name(&self, name: &str) -> String {
let name = name.to_camel_case();

if self.singularize {
singular(name)
} else {
name
}
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hey @negezor, thanks for the hard rebase (it's hard as this PR is far behind from master).

I wonder why we need three different methods? I think we can merge it and have two methods? One for snake_case and the other for camel_case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

A fair remark, but I proceeded from the clear goals of the method. I can make changes if necessary.

@negezor negezor closed this Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.