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

Mapping functional #19

Closed
wants to merge 4 commits into from
Closed

Conversation

Andrushka1012
Copy link

Class Mapper Provides functionality for mapping. Uses MappingProfileProvider to perform mapping TypeA to TypeB declared in one of the MappingProfiles mappings lists.

Class MappingProfileProvider serves as a bridge between Mapper and MappingProfile. Ensures the uniqueness of the declaration for typed mapping functions. Also allows the mapping function to use nested mapping functions declared in others MappingProfile.

Abstract class MappingProfile contains a list of mappings functions.
The following example shows how to declare mapping Type Long to Type String.

class MappingExampleProfile : MappingProfile({
    createMap<Long, String> { this.toString() }
})

We may later use it for nested mappings.

createMap<Int, String> { mapper ->
        mapper.map<Long, String>(this.toLong())
    }

How to use.
Method of delivery MappingProfile to Mapper сan be any, depending on the user's implementation.

Koin way:
You must call registerMapper() in any of your koin modules. After you can declare MappingProfile in any of your modules:

val AppModule = module {
   registerMapper()
   mappingProfile(MappingExampleProfile())
}

After inject Mapper to whatever you want using koin get() or inject() functions.

Dagger 2 way:
You must declare MappingProfile at your dagger module:

@Provides
@IntoSet
fun provideMappingExampleProfile(): MappingProfile = MappingExampleProfile()

After declare Mapper and MappingProfileProvider

@Provides
@Singleton
fun provideMappingProfileProvider(
        profiles: Set<@JvmSuppressWildcards MappingProfile>
): MappingProfileProvider {
    return MappingProfileProvider
            .createProfileProvider(profiles.toList())
}

@Provides
@Singleton
fun provideMapper(mappingProfileProvider: MappingProfileProvider): Mapper {
    return Mapper(mappingProfileProvider)
}

No DI way(not recommended but one of possible):
You can manually delivery MappingProfileProvider to Mapper

val mapper = Mapper(
        MappingProfileProvider.createProfileProvider(
                listOf(
                        MappingExampleProfile()
                )
        )
)

Also you can add MappingProfile to already registered MappingProfileProvider by addProfile(profile: MappingProfile) function.

@zsmb13
Copy link
Member

zsmb13 commented Aug 30, 2020

This is something you can do for your own app, but it's not something RainbowCake wants to provide for now. Not having infrastructure in place for mappers makes it easier to get started with them.

@zsmb13 zsmb13 closed this Aug 30, 2020
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.

None yet

2 participants