ProductPromotion
Logo

Kotlin

made by https://0x3d.site

GitHub - krud-dev/shapeshift: A Kotlin/Java library for intelligent object mapping and conversion between objects.
A Kotlin/Java library for intelligent object mapping and conversion between objects. - krud-dev/shapeshift
Visit Site

GitHub - krud-dev/shapeshift: A Kotlin/Java library for intelligent object mapping and conversion between objects.

GitHub - krud-dev/shapeshift: A Kotlin/Java library for intelligent object mapping and conversion between objects.

Awesome Kotlin Maven Central CircleCI Codecov GitHub contributions welcome

Overview

ShapeShift is a Kotlin first object mapping library. We have built ShapeShift because we wanted a simple to use, minimal boiler plate mapping engine, that is also flexible and supports the most advanced use cases.

Built with Kotlin in mind, ShapeShift was designed around its ecosystem and best practices. The library has 2 main tools for mapping:

  • Annotations - Fully featured annotation based mapping, just add annotations to your objects and ShapeShift handles the rest. Including using custom field transformers, conditional mapping, advanced object decoration and much more. (Kotlin Example, Java Example)
  • Kotlin DSL - A Kotlin DSL allowing you to define the relations between objects. This allows you to map objects you can't change (or don't want to), like objects from 3rd party libraries. Additionally you can define inline transformations, conditions and decorations, enabling deep customization and very advanced mapping. (Kotlin Example)
  • Java Builder - The equivalent Java API for the Kotlin DSL. Allowing you to define the relations between objects without modifying their code, and map objects you can't change (or don't want to). (Java Example)

ShapeShift main features:

  • Auto Mapping
  • Custom field transformers
  • Default transformers
  • Deep mapping
  • Multiple mapping targets
  • Conditional mapping
  • Mapping decorators
  • Seamless spring integration
  • Native Android support

Documentation

To learn how to get started with ShapeShift, visit the official documentation website. You'll find in-depth documentation, tips and guides to help you get up and running.

Installation

Requirements

  • Minimum supported Kotlin version: 1.6.X
  • Minimum supported Java version: 1.8

Maven

<dependency>
  <groupId>dev.krud</groupId>
  <artifactId>shapeshift</artifactId>
  <version>0.8.0</version>
</dependency>

Gradle

Groovy DSL

implementation 'dev.krud:shapeshift:0.8.0'

Kotlin DSL

implementation("dev.krud:shapeshift:0.8.0")

Quickstart

Kotlin

Kotlin DSL

// Source Class
data class Source(
    val firstName: String,
    val lastName: String,
    val birthDate: LocalDate
)

// Target Class
data class Target(
    var firstName: String = "",
    var lastName: String = "",
    var birthYear: Int = 0
)

fun main() {
    /**
     * Initialize ShapeShift with a mapping definition from From to To
     */
    val shapeShift = ShapeShiftBuilder()
        .withMapping<Source, Target> {
            // Map firstName
            Source::firstName mappedTo Target::firstName
            // Map lastName
            Source::lastName mappedTo Target::lastName
            // Map birthDate to birthYear with a transformation function
            Source::birthDate mappedTo Target::birthYear withTransformer { (originalValue) ->
                originalValue?.year
            }
        }
        .build()

    // Initialize Source
    val source = Source("John", "Doe", LocalDate.of(1980, 1, 1))
    // Perform the mapping
    val result = shapeShift.map<Source, Target>(source)
    // Returns: To(firstName=John, lastName=Doe, birthYear=1980)
}

Kotlin Annotation

// Source Class
@DefaultMappingTarget(Target::class)
data class Source(
    @MappedField
    val firstName: String,
    @MappedField
    val lastName: String,
    @MappedField(mapTo = "birthYear", transformer = LocalDateToYearTransformer::class)
    val birthDate: LocalDate
)

// Target Class
data class Target(
    var firstName: String = "",
    var lastName: String = "",
    var birthYear: Int = 0
)

// Define the transformer which will transform the local date to a year
class LocalDateToYearTransformer : MappingTransformer<LocalDate, Int> {
    override fun transform(context: MappingTransformerContext<out LocalDate>): Int? {
        return context.originalValue?.year
    }
}

fun main() {
    /**
     * Initialize ShapeShift and register the transformer
     */
    val shapeShift = ShapeShiftBuilder()
        .withTransformer(LocalDateToYearTransformer())
        .build()

    // Initialize Source
    val source = Source("John", "Doe", LocalDate.of(1980, 1, 1))
    // Perform the mapping
    val result = shapeShift.map<Source, Target>(source)
    // Returns: To(firstName=John, lastName=Doe, birthYear=1980)
}

Java

Java Builder

// Source.java
class Source {
    private String firstName;
    private String lastName;
    private LocalDate birthDate;

    // Constructor
    // Getters
    // Setters
    // ToString
}

// Target.java
class Target {
    private String firstName;
    private String lastName;
    private int birthYear;

    // Constructor
    // Getters
    // Setters
    // ToString
}

// Example.java
class Example {
    public static void main(String[] args) {
        /**
         * Initialize ShapeShift with a mapping definition from From to To
         */
        ShapeShift shapeShift = new ShapeShiftBuilder()
                .withMapping(
                        new MappingDefinitionBuilder(Source.class, Target.class)
                                // Map firstName
                                .mapField("firstName", "firstName")
                                // Map lastName
                                .mapField("lastName", "lastName")
                                // Map birthDate to birthYear with a transformation function
                                .mapField("birthDate", "birthYear").withTransformer(ctx -> ((LocalDate) ctx.getOriginalValue()).getYear())
                                .build()
                )
                .build();
        // Initialize Source
        Source source = new Source("John", "Doe", LocalDate.of(1980, 1, 1));
        // Perform the mapping
        Target result = shapeShift.map(source, Target.class);
        // Returns: To(firstName=John, lastName=Doe, birthYear=1980)
    }
}

Java Annotation

// Source.java
@DefaultMappingTarget(Target.class)
class Source {
    @MappedField
    private String firstName;
    @MappedField
    private String lastName;
    @MappedField(mapTo = "birthYear", transformer = LocalDateToYearTransformer.class)
    private LocalDate birthDate;

    // Constructor
    // Getters
    // Setters
    // ToString
}

// Target.java
class Target {
    private String firstName;
    private String lastName;
    private int birthYear;

    // Constructor
    // Getters
    // Setters
    // ToString
}

// Define the transformer which will transform the local date to a year
// LocalDateToYearTransformer.java
class LocalDateToYearTransformer implements MappingTransformer<LocalDate, Integer> {
    @Override
    public Integer transform(MappingTransformerContext<? extends LocalDate> context) {
        return context.getOriginalValue().getYear();
    }
}
// Example.java
class Example {
    public static void main(String[] args) {
        /**
         * Initialize ShapeShift
         */
        ShapeShift shapeShift = new ShapeShiftBuilder()
                .withTransformer(LocalDate.class, Integer.class, new LocalDateToYearTransformer())
                .build();
        // Initialize Source
        Source source = new Source("John", "Doe", LocalDate.of(1980, 1, 1));
        // Perform the mapping
        Target result = shapeShift.map(source, Target.class);
        // Returns: To(firstName=John, lastName=Doe, birthYear=1980)
    }
}

Examples

The example directory contains several independent scenarios for common use cases of this library.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. See CONTRIBUTING.md for more information.

License

ShapeShift is licensed under the MIT license. For more information, please see the LICENSE file.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory