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

Flattening without Default #15

Open
utomdht opened this issue Aug 22, 2024 · 2 comments
Open

Flattening without Default #15

utomdht opened this issue Aug 22, 2024 · 2 comments

Comments

@utomdht
Copy link

utomdht commented Aug 22, 2024

Hi,

Thanks for this great project, it save me a lot of time mapping DTOs/BOs !

I was wondering if there could be a way to generate Into with struct flattening without relying on Default, ex:

// Source, built from my API, and is missing some ids extracted from auth or other sources
struct EntityNew {
    name: String
    description: String
}

// this struct is built within the Domain layer just for o2o mapping, wrapping the API object and extracted fields
#[owned_into(EntityNewBO)]
struct EntityNewWrapper {
   #[flatten]
    wrapped: EntityNew
    id: Uuid
    user_id: Uuid    
    computed_field: Int
}

// Target object. A default on this doesn't make much sense and bear the risk of missing compilation errors when adding fields
struct EntityNewBO {
    id: Uuid
    user_id: Uuid    
    computed_field: Int
    name: String
    description: String
}
@Artem-Romanenia
Copy link
Owner

Hi, thanks for such high evaluation of o2o!

The problem with theoretic #[flatten] attribute is that there is no way for proc macro to know what fields wrapped: EntityNew has. It may even not be a struct. So you have to somehow let o2o know about those fields. Since you need to do Into, you can do this:

#[owned_into(EntityNewBO)]
#[ghosts(name: { @.wrapped.name }, description: { @.wrapped.description })]
struct EntityNewWrapper {
    #[ghost]
    wrapped: EntityNew,
    id: Uuid,
    user_id: Uuid,  
    computed_field: Int,
}

If there is an option to put o2o macro on the other side of a mapping, you can also do this:

#[from_owned(EntityNewWrapper)]
struct EntityNewBO {
    id: Uuid,
    user_id: Uuid, 
    computed_field: Int,
    #[child(wrapped)] name: String,
    #[child(wrapped)] description: String,
}

Please post here or create new issue if you have any other questions!

@Artem-Romanenia
Copy link
Owner

Actually I had an idea that can make things a little better in such cases, so I`ll keep this one open)

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

No branches or pull requests

2 participants