Giter Club home page Giter Club logo

pongo's Introduction

About Pongo

Pongo is a minimal DSL that helps you to convert Java objects from one type to another.

Pongo doesn't convert objects by itself, it just organize things better. Specifying how objects have to be converted is up to the developer.

Usage

Converting an object (say x) into another (say y of type Y) is as simple as writing:

Y y = pongo.convert(x).to(Y.class);

If you want to convert an array of X elements into an array of Y elements, this is how you do it:

Y[] arrayOfY = pongo.convert(arrayOfX).toArrayOf(Y.class);

As of now Pongo helps you to convert: - arrays into arrays or lists - an array of (eterogeneus) objects into a single object - lists into lists or arrays - a list of (eterogeneus) objects into a single object - single objects into objects

When converting lists or arrays you can make use of filters like this:

Y[] arrayOfY = pongo.convert(arrayOfX).iff(Pongo.NOT_NULL).toArrayOf(Y.class);

This will tell Pongo not to attempt to convert elements of arrayOfX that are null.

Configuration

Usuallly you will have one instance of Pongo in your project, but this is not mandatory:

Pongo pongo = new Pongo();

The first step when using Pongo is declaring how objects should be converted.

pongo.addConverter(new IConverter<A,B>() {
    public B convert(A a, B b, Pongo p) {
            ....
            return b;
        }

        public Class<A> getSource() {
            return A.class;
        }

        public Class<B> getTarget() {
            return B.class;
        }
});

An instance of Pongo is passed to the convert method when conversion takes place from A to B. This way you can chain several conversion like this:

pongo.addConverter(new IConverter<A,B>() {
    public B convert(A a, B b, Pongo p) {
            ...
            b.setAttributeY(p.convert(a.getAttributeX()).to(Y.class));
            return b;
        }

        public Class<A> getSource() {
            return A.class;
        }

        public Class<B> getTarget() {
            return B.class;
        }
});

Motivations

Not that I don't appreciate existing approaches (http://stackoverflow.com/questions/1432764/any-tool-for-java-object-to-object-mapping) but:

  • yes writing conversion methods is very boring but it's also simple in most cases (at least most of those I run into)
  • avoiding reflection can improve performance
  • I wanted the compiler to clearly inform me whenever a conversion method is broken because a signature is changed
  • I personally dislike XML (and people that like it in particular)
  • I needed to find a readable/elegant way (at least more readable/elegant than writing tons of static methods) to declare how conversions should take place
  • I wanted full control over conversion methods

pongo's People

Contributors

sfragis avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.