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

Support copying classes that don't have direct dependency #82

Open
mariobat88 opened this issue Feb 7, 2023 · 4 comments
Open

Support copying classes that don't have direct dependency #82

mariobat88 opened this issue Feb 7, 2023 · 4 comments

Comments

@mariobat88
Copy link

mariobat88 commented Feb 7, 2023

Not sure if this is planned in some future release, but let me explain my idea.
Usually when I set up my projects the database Entity classes (database module) and the Domain classes (repo module) don't have direct to each other so there is no way for me to annotate the classes with @copy. The way I copy objects is that I write mappers in my datasource layer which has dependency to both database and repositories. This is how I map my objects in a datasource:

fun UserEntity.toUserModel() = UserModel(
   id = id,
)

Is there a way to do that with this library, or is it planned to support something similar?

@serras
Copy link
Collaborator

serras commented Feb 19, 2023

Thanks for the feedback! We could definitely think of something, the main issue is figuring out where to attach the annotations that describe what to create. @mariobat88 do you have some idea of how you would like it to look like?

@mariobat88
Copy link
Author

mariobat88 commented Feb 27, 2023

Hi @serras sorry for the late reply. Not sure if that's possible, but maybe by creating an abstract class and annotate the function with a @Copy annotation? I was thinking something like this:

abstract class UserMapper{
    
    @Copy
    abstract fun toEntity(userResponse: UserResponse) : UserEntity
    
    @Copy
    abstract fun toModel(userEntity: UserEntity) : UserModel
}

I'm also not sure if the we need to pass in a parameter into the @Copy annotation since we could probably figure it out from the function parameters and return type.

@serras
Copy link
Collaborator

serras commented Feb 28, 2023

I see... the only problem is where would we generate the implementations. KSP is quite restrictive, so we cannot add it to an existing class, for example.

@serras
Copy link
Collaborator

serras commented Feb 28, 2023

What about having them as file annotations? So you would do:

@file:Copy("toEntity", UserResponse::class, UserEntity::class)

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

No branches or pull requests

2 participants